Thread: Having the computer ask a random questrion, and storing a variable with the answer

  1. #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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #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

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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 .

  5. #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() &#37; (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)

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #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.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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.

  10. #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

Popular pages Recent additions subscribe to a feed

Tags for this Thread