Advertisement

Some Random Question Help

Started by February 20, 2003 07:06 PM
10 comments, last by Surg AKA Kunark 21 years, 8 months ago
First before i can ask this question, how can i make the white boxes to place my code in them. Its alot to read without that.
      
//This is Practice Program I am making

//binary(my_random_number); this is used to show the random binary

#include <iostream>										// Include the standard
#include <windows.h>									// We need to include this for "console" functions.
#include <string>										
// Allows us to use the "string" type

#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <vector>

using namespace std;									// Use the standard namespace (blah blah, not important to understand right now)


string userName;  //The variable for there name

int Age; //The variable for there age

int Play; //the variable to determine if they want to play or not

int Answer; //variable for answers with numbers

string stranswer;
int score = 0; //variable that keeps track of there score

int total = 0; //variable that keeps track of there number of questions

string Engelbart = "Engelbart";
string engelbart = "engelbart";
int Trivia();
void binary(int);
int question;
int Kilobyte();
int age();
int binary();
int entername();
int bios();
int RAM();
int nibble();
int mouse();
int ending();

// Here is our function that draw's a color string in a DOS window.

// We just pass in the string, the X and Y position, then the color we desire.

// The color can be a background color AND a foreground color.  Here are some color examples:

// FOREGROUND_GREEN; FOREGROUND_RED; FOREGROUND_BLUE; - BACKGROUND_RED; BACKGROUND_BLUE; BACKGROUND_GREEN;

// You can mix and match these to create other colors.  Look in "Main()" to see how  


void DrawColorString(string szText, int X, int Y, WORD color)
{	
	HANDLE OutputH;										// This will be used for our handle to the output (basically, holds permission to change the output settings)

	COORD position = {X, Y};							// Create a COORD and set it's x and y to the X and Y passed in.


	OutputH = GetStdHandle(STD_OUTPUT_HANDLE);			// Get a OUTPUT handle to our screen.


	SetConsoleTextAttribute(OutputH, color);			// Set the text attribute to what ever color that was passed in.

														// The function SetConsoleTextAttribute takes (the handle to the output, and the color);

	SetConsoleCursorPosition(OutputH, position);		// Set the cursor position to the desire position passed in.


	cout << szText;										// Now print out what we wanted to at the position that was passed in.


}

////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////

//OK, trying out this vector thing, since i have never used them before i //

//I do this right.  Im assuming i want the vector near the top, IF not    //

//Could someone show me te place it should go, thanx,.                    //

////////////////////////////////////////////////////////////////////////////

vector<void (*)(void)> questions;

questions.push_back(&Kilobyte);
questions.push_back(&age);
questions.push_back(&binary);
questions.push_back(&RAM);
questions.push_back(&entername);
questions.push_back(&bios);
questions.push_back(&nibble);
questions.push_back(&mouse);

random_shuffle(questions.begin(), questions.end());


////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////


int main()
{
	srand(time(0)) ;
	int maximum_desired_number = 100 ;
	int my_random_number = rand() % maximum_desired_number + 1 ;
	/////////////////////////////////////////////////////////////

	/////////////////////////////////////////////////////////////

	
	cout << "\t\t    Welcome to my Practice Program!";
	cout << endl << endl <<"\t\tHi my name is Falto the Computer Program.\n"; // The welcome message to my program

	cout << "\t\t\tWhat is your name?\n\t\t\t   "; //asks them what there name is 

	cin >> userName; //requires user input, up to 20 characters long 

	cout << "\n\n\t\tWelcome " << userName << " to my practice program!\n"; //Displays there name and says message

	cout << "\t\tI was born on September 14 2002\n\t\tSo that means you can calculate my age.\n\t\tWhat is your age?\t";
	cin >> Age; //requires user input for there age

	cout << "\n\t\tYou are " << Age << " years old, and your name is " << userName << ".\n";
	cout << "\t\tWould you like to play my computer trivia game?\n\t\tPlease Enter 1 for yes, 2 for no!\t";
	cin >> Play; //requires user input, 1 for yes, 2 for no.

	if(Play==1) //This starts up trivia

	{
	Trivia();
	}
	else if(Play==2) //This is if they dont want to play and it quits

	{
	cout << "\n\n\t\tYou dont want to play my Trivia Game!\n";
	cout << "\t\tWell I am sorry about that.\n\t\tYour lost, HAHAHA\n";
	return 0;
	}
	else //This is if they don't enter yes or no

	{
		cout << "\t\tI'm sorry but that wasn't a valid entry.\n";
		cout << "\t\tShutting Down Program\n";
	}
	
	return 0; //returns the value of zero to your program

}

///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////


int Trivia()

{
	srand(time(0));
	int maximum_desired_number = 100 ;
	int my_random_number = rand() % maximum_desired_number + 1 ;

	srand(time(0));
	int question_number = 8;
	int random_question_number = rand() % question_number + 1;


	cout << "\n\n\n\t\tWelcome to Computer Trivia!\n\t\tPrepare for your First Question.\n";
	

	switch (random_question_number) {
	case 1:Kilobyte();
		break;
	case 2:age();
		break;
	case 3:binary();
		break;
	case 4:RAM();
		break;
	case 5:entername();
		break;
	case 6:bios();
		break;
	case 7:nibble();
		break;
	case 8:mouse();
		break;
	}

	
	
	return 0;
}

	
int Kilobyte()
{
	question = question + 1;
	cout << "\n\n\n\n\n\n\n\t\tQuestion " << question << ":\n\t\tHow many bytes are in a Kilobyte?\n";
	cout << "\t\tPlease type your answer here.\t";
	total = total + 1;
	cin >> Answer;
	if(Answer==1024)
	{
		
		cout << "\n\n\tThat is a correct answer, your doing good, keep up the good work!\n";
		
		cout << "\t\tLet's move onto the next question!\n";
		score = score + 1;
	}
	else
	{
		cout << "\n\t\t\tSorry, that's the wrong answer.\n";
		cout << "\tThe correct answer is that there is 1024 bytes in a kilobyte\n";
	}

	return 0;
}

int age()
{
	question = question + 1;
	cout << "\n\n\n\n\n\n\t\tQuestion " << question << ":\n\tWhat did you enter your age as at the beginning of this program?\n";
	cout << "\t\t\t\t";
	total = total + 1;
	cin >> Answer;
	if (Answer==Age)
	{
		cout << "\n\tAwesome, that's a correct answer, you got good memory!\n";
		cout << "\t\tOn with the next question as they say\n";
		score = score + 1;
	}	
	else
	{
		cout << "\n\t\t\tSorry, that's the wrong answer.\n";
		cout << "\tYou entered your age as "<< Age << " at the beginning of the game\n";
	}

	return 0;
}

int binary()
{
	question = question + 1;
	srand(time(0));
	int maximum_desired_number = 100 ;
	int my_random_number = rand() % maximum_desired_number + 1 ;
	cout << "\n\n\n\n\n\n\t\tQuestion " << question << ":\n\t";
	cout << "\tWhat is the decimal for binary " ;
	binary(my_random_number);
	cout << "\t";
	total = total + 1;
	cin >> Answer;
	if (Answer==my_random_number)
	{
		cout << "\n\t\tWow, trick questions dont fool you, do they\n";
		score = score + 1;
	}
	else
	{
		cout << "\n\t\t\tSorry, that's the wrong answer.\n";
		cout << "\t\tThe correct answer is " << my_random_number << "!\n";	 
	}

	return 0;
}

int RAM()
{
	question = question + 1;
	cout << "\n\n\n\n\n\n\t\tQuestion " << question << ":\n\t";
	cout << "\tWhat does RAM stand for\n";
	cout << "\t\tPlease enter the number of the multi-choice\n";
	cout << "\n\t\t1) Real Access Memory";
	cout << "\n\t\t2) Random Area Message";
	cout << "\n\t\t3) Random Access Memory";
	cout << "\n\t\t4) Radio Active Machine\t\t";
	total = total + 1;
	cin >> Answer;
	if (Answer==3)
	{
		cout << "\n\t\tExcellent, you got the first multi-choice right";
		score = score + 1;
	}
	else
	{
		cout << "\n\t\t\tSorry, that's the wrong answer.\n";
		cout << "\t\tThe correct answer is 3)Random Access Memory.";	  
	}

	return 0;
}

int entername()
{
	question = question + 1;
	cout << "\n\n\n\n\n\n\t\tQuestion " << question << ":\n\t";
	cout << "\tWhat did you enter your name as\n\t\tat the beginning of this program\n\t\t\t";
	total = total + 1;
	cin >> stranswer;
	if (stranswer==userName)
	{
		cout << "\n\t\tThat's a correct answer\n\t\tI'm surprised you can remember your name.";
		score = score + 1;
	}
	else
	{
		cout << "\n\t\t\tMan your stupid, can't even remember your own name\n\t\t\tShame on you.";
	}

	return 0;
}

int bios()
{
	question = question + 1;
	cout << "\n\n\n\n\n\n\t\tQuestion " << question << ":\n\t";
	cout << "\tWhat type of memory is the BIOS stored in?\n\t\tPlease use abbreviations\n\t\t\t";
	total = total + 1;
	cin >> stranswer;
	if (stranswer== "ROM") /* or "Read Only Memory" or "read only memory" or "rom"*/
	{
		cout << "\n\t\t\tThat's awesome, add another point to your score.";
		score = score + 1;
	}
	else
	{
		cout << "\n\t\t\tSorry that's the wrong answer, the correct one is\n\t\t\tROM or Read Only Memory\n";
	}

	return 0;
}

int nibble()
{
	question = question + 1;
	cout << "\n\n\n\n\n\n\t\tQuestion " << question << ":\n\t";
	cout << "\tHow many bits are in a nybble/nibble?\n\t\t\t\t";
	total = total + 1;
	cin >> Answer;
	if (Answer == 4) /* or "Read Only Memory" or "read only memory" or "rom"*/
	{
		cout << "\n\t\tThat's awesome, tally up another point for your score.";
		score = score + 1;
	}
	else
	{
		cout << "\n\t\t\tSorry that's the wrong answer, the correct one is\n\t\t\t\tFour Bits\n";
	}

	return 0;
}

int mouse()
{
	question = question + 1;
	cout << "\n\n\n\n\n\n\t\tQuestion " << question << ":\n\t";
	cout << "\tWho invented the mouse, last name only?\n\t\t\t\t";
	total = total + 1;
	cin >> stranswer;

	if (stranswer == Engelbart) 
	{
		cout << "\n\t\t\tThat's awesome, tally up another point for your score.";
		score = score + 1;
	}
	else if (stranswer == engelbart)
	{
		cout << "\n\t\tThat's awesome, tally up another point for your score.";
		score = score + 1;
	}
	else
	{
		cout << "\n\t\t\tSorry that's the wrong answer, the correct one is\n\t\t\t\tEngelbart\n";
	}

	return 0;
}

int ending()
{
	cout << "\n\n\n\n\t\tYou score was " << score << "/" << total;
	return 0;
}

	
void binary(int my_random_number) {
	int remainder;

	if(my_random_number <= 1) {
		cout << my_random_number;
		return;
	}

	remainder = my_random_number%2;
	binary(my_random_number >> 1);    
	cout << remainder;
}



      
What i need to do is, i want the questions to come in a random order, but without repeating themeselves once they have come. Im just a beginner so if someone can help me please also explain how it works if you can. P.S This is C++, using Microsoft Visual C++. [edited by - Surg AKA Kunark on February 20, 2003 8:27:14 PM] [edited by - Surg AKA Kunark on February 22, 2003 11:23:19 AM] [edited by - Surg AKA Kunark on February 22, 2003 11:24:54 AM]
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
[source]
code here
[/source]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
TY for your help Smart_idiot
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
quote:

  switch (random_question_number) {  case 1:Kilobyte();  case 2:age();  case 3:binary();  case 4:RAM();	  case 5:entername();  case 6:bios();  case 7:nibble();  case 8:mouse();  break; }  


In case you don''t know, you need a break at the end of each case or it is going to go on to the next one.

Anyway. . . since you''re using cout, I''ll assume this is C++ and that STL is available to you. It appears that all the functions have the same signature (take nothing, and return nothing), so lets us a vector of function pointers:


  vector<void (*)(void)> questions;  


Now fill it with the functions:


  questions.push_back(&Kilobyte);questions.push_back(&age);questions.push_back(&binary);// and so on.  


That is the official way to do it, although most compilers will let you cheat and do this instead:


  questions.push_back(Kilobyte);questions.push_back(age);questions.push_back(binary);// and so on.  


Next, scramble the vector:


  random_shuffle(questions.begin(), questions.end());  


And now call each function:

  for(int c = 0; c < questions.size(); ++c) (*questions[c])();  


Again, that is the right way, but your compiler may let you do this instead:


  for(int c = 0; c < questions.size(); ++c) questions[c]();  


Finally, random_shuffle is in the header ''algorithm'', and vector is declared in the header ''vector''. Hope that works, I haven''t tried it.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Actually, for some reason all the question functions return int. Since you don't actually use what they return you change them to return void, or you can change the declaration of questions to be vector<int (*)(void)> questions; instead.

[edited by - smart_idiot on February 21, 2003 10:15:28 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Thanx for the help, now just for me to try this. Hope it works to, ill get back to you if it does.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Advertisement
OK, i''ve updated my code on the first part, but now i get somethin like 36 errors. I know its jsut me doin somethin wrong cause i forgot to mention i have never used vectors yet.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Your functions all have the signature ''int ()(void)'', where as the vector wants the signature ''void ()(void)''.

To fix it you can change all the question functions to return void, or you can change the vector to use ''int (*)(void)'' instead.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
OK, well i tried both of those, the void one makes me come out with 56 errors, so i kinda got rid of that, and the changing it to int in the vector doesn''t cahnge any of the errors, im sure i musta just placeed it in the wrong place, maybe i was suppose to have the calling where i had the switch.
~~Johnathan~~LTM my new fad, know it, use it, love it, LAUGHING TO MYSELF
Uh, that code I posted was supposed to replace most of what was in the Trivia functon, you can''t put it outside of a function and have it work.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement