C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-22-2008, 02:32 PM   #1
Registered User
 
Join Date: Nov 2008
Posts: 5
Question Having the computer ask a random questrion, and storing a variable with the answer

I am new to using C++, just started learning a little while ago. So, what I've been trying to do[I didnt think it would be to difficult], was to have the computer randomly choose one of the questions I have set up, and then depending on what question was chosen, store the answer in the create variable. So far, this is what i have.

Code:
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{

	string name,birthday,favColor,country,petName,birthPlace,favBand, SendTo;
	int age, favNumber, IQ, selected, i;

	
	string question[10] [10];
	question[0][0] = "What is your name?";;
	question[1][0] = "When is your birthday?";
	question[2][0] = "What is your favourite color?";
	question[3][0] = "What country do you live in?";
	question[4][0] = "How old are you?";
	question[5][0] = "What is your favourite number?";
	question[6][0] = "How many friends do you have?";
	question[7][0] = "Do you have any pets?";
	question[8][0] = "Where were you born?";
	question[9][0] = "What is your I.Q?";
	question[10][0] = "What is your favourite band?";

	if (i < 10)
	{
	     selected =  (rand() % (10-1+1))+1;
	     switch(selected)
	     {
	
	          case 0:SendTo = name;	  
	          case 1:SendTo = birthday;
	          case 2:SendTo = favColor;
	          case 3:SendTo = country;
	          case 4:SendTo = age;		
	          case 5:SendTo = favNumber;	  
	     }
	     cout << question[selected][0];
	     cin >> SendTo;   
	}	
	return 0;

}
A few notes:
1) I wasnt entirely sure on how to make a random number, so I googled it...and I dont know how that works exactly.

2) Also, the problem is that this code is NOT displaying anything. It will run, but, its not displaying anything.

3) Not sure if its important[I dont think it is], but im using Quincy 2005 to write this.
Movingforward is offline   Reply With Quote
Old 11-22-2008, 02:37 PM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by Movingforward
1) I wasnt entirely sure on how to make a random number, so I googled it...and I dont know how that works exactly.
You are on the right track. You could also read Prelude's article on Using rand() for more information.

Quote:
Originally Posted by Movingforward
2) Also, the problem is that this code is NOT displaying anything. It will run, but, its not displaying anything.
Notice that your variable i is not initialised. In fact, you should try and declare your variables near the first time they are used.

Incidentally, why create an array of 10 arrays of 10 strings when you only need 10 strings?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-22-2008, 02:39 PM   #3
Registered User
 
Join Date: Nov 2008
Posts: 5
Thank you!

The reason I had used the [10] [10] array was because I was testing something, but forgot to change it back lol. So, I'll initialize them, and look at the example/tutorial to see whats going on Thanks again
Movingforward is offline   Reply With Quote
Old 11-22-2008, 02:43 PM   #4
Registered User
 
Join Date: Oct 2008
Posts: 452
Quote:
Originally Posted by Movingforward View Post
Thank you!

The reason I had used the [10] [10] array was because I was testing something, but forgot to change it back lol. So, I'll initialize them, and look at the example/tutorial to see whats going on Thanks again
Also, you create an array with 10 items max, and you used 11 elements. And you forgot break instructions in the switch .
EVOEx is offline   Reply With Quote
Old 11-22-2008, 02:50 PM   #5
Registered User
 
Join Date: Nov 2008
Posts: 5
Alright. It works, it asks a random question, kinda. It is always the same though, I mean whenever you start it up, its in the same order.[Always starts with how many friends...]

Other than the lack of randomness, theres a few more things that I want to get working.

1) I want it so save into the correct variable, depending on the question.

2) I want it to go again, and again, until all the questions have been asked.

Code:
int main()
{

	string name, birthday, favColor, country, petName, birthPlace, favBand, SendTo;
	int age, favNumber, IQ, selected, i;
	i = 0;
	
	string question[11];
	question[0] = "What is your name?";;
	question[1] = "When is your birthday?";
	question[2] = "What is your favourite color?";
	question[3] = "What country do you live in?";
	question[4] = "How old are you?";
	question[5] = "What is your favourite number?";
	question[6] = "How many friends do you have?";
	question[7] = "Do you have any pets?";
	question[8] = "Where were you born?";
	question[9] = "What is your I.Q?";
	question[10] = "What is your favourite band?";
	
	
	

	if (i < 10)
	{
	selected = 1+ ( rand() % (10-1));
	
	
	cout << question[selected];
	cin >> SendTo;   
	
	switch(selected)
	{
	
	case 0:name = SendTo;break;	    
	case 1:birthday = SendTo;break;
	case 2:favColor = SendTo;break;
	case 3:country = SendTo;break;
	case 4:age = SendTo;break;
	case 5:favNumber = SendTo;break;	
	
	}
		
	
	}
	

	
	return 0;

}
Its giving me the error that It cannot save a string into an int. Any ways around this?

Note: Lol, i thought creating an array of [10] was 11(since i thought it included 0)
Movingforward is offline   Reply With Quote
Old 11-22-2008, 02:53 PM   #6
Registered User
 
Join Date: Jan 2005
Posts: 7,137
If you don't want to ask the same question twice you can use a different technique for randomizing the questions. Call random_shuffle to shuffle the questions in your array, then just loop through the array.

If questions is a string[10] then the code is:
Code:
std::random_shuffle(questions, questions+10);
You have to #include <algorithm>.

Also, on some platforms, you need to seed rand() before calling random_shuffle. It doesn't look like you've done that in your current program, so I'll let you figure it out. Just know that if you get the same random numbers every time then it's because you need to seed the random number generator with srand() properly.
Daved is online now   Reply With Quote
Old 11-22-2008, 02:55 PM   #7
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
Quote:
Originally Posted by Movingforward
Alright. It works, it asks a random question, kinda. It is always the same though, I mean whenever you start it up, its in the same order.[Always starts with how many friends...]
You should seed the pseudo-random number generator. The article I linked to has an explanation of how to do this.

Quote:
Originally Posted by Movingforward
1) I want it so save into the correct variable, depending on the question.
You probably only need to generate the correct array index (but see my answer to your next question). However, EVOEx makes a good point: you still are accessing the array out of bounds because you have 11 questions, not 10.

Quote:
Originally Posted by Movingforward
2) I want it to go again, and again, until all the questions have been asked.
Use std::random_shuffle() from the <algorithm> standard header and then just loop through the shuffled array once.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 11-22-2008, 03:36 PM   #8
Registered User
 
Join Date: Nov 2008
Posts: 5
Alright. Well, i looked at another example as well, and manage to get it working. What I ended up with was:
Code:
#include <string>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;



// Random number generator
int Random(int n)
{
    return rand() % n ;
}



int main()
{

	int selected, i;
	i = 0;
	
	//Seeding random number generator
    std::srand(std::time(0));
	
	
	
	string question[11];
	string answer[11];
	
	question[0] = "What is your name?";;
		answer[0] = "";
	
	question[1] = "When is your birthday?";
		answer[1] = "";
	
	question[2] = "What is your favourite color?";
		answer[2] = "";
	
	question[3] = "What country do you live in?";
		answer[3] = "";
	
	question[4] = "How old are you?";
		answer[4] = "";
	
	question[5] = "What is your favourite number?";
		answer[5] = "";
	
	question[6] = "How many friends do you have?";
		answer[6] = "";
	
	question[7] = "Do you have any pets?";
		answer[7] = "";
	
	question[8] = "Where were you born?";
		answer[8] = "";
	
	question[9] = "What is your I.Q?";
		answer[9] = "";
		
	question[10] = "What is your favourite band?";
		answer[10] = "";
	
	//Shuffling the numbers
    std::random_shuffle(question, question + 10, Random);
	

	if (i < 10)
	{
		for (selected = 0;selected < 10;selected++)
		{
		cout << endl << question[selected] << " ";
		getline(cin, answer[selected]);
		cout << "You said: " <<answer[selected] << endl;
		}
	}
	 
	return 0;

}
Thanks for the help guys.
Movingforward is offline   Reply With Quote
Old 11-22-2008, 04:20 PM   #9
Registered User
 
Join Date: Jan 2005
Posts: 7,137
Note that you don't actually need the Random function. If you call random_shuffle the way I showed you it should work just as well and possibly better.

Also, note that you are asking 10 questions instead of 11, so there will always be one question that doesn't get asked.

Finally, the if (i < 10) part is useless. Try to figure out what you were trying to do and why it doesn't help here.
Daved is online now   Reply With Quote
Old 11-23-2008, 07:12 PM   #10
Registered User
 
Join Date: Nov 2008
Posts: 5
AH, the if(i<10) doesnt help since it doesnt increment, so it will always be below 10. Anyways, it works
Movingforward is offline   Reply With Quote
Reply

Tags
display, random, user-input

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 01:52 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22