![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: Nov 2008
Posts: 5
| 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;
}
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 | |
| | #2 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
Quote:
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 | |
| | #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 | |
| | #4 | |
| Registered User Join Date: Oct 2008
Posts: 452
| Quote:
. | |
| EVOEx is offline | |
| | #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;
}
Note: Lol, i thought creating an array of [10] was 11(since i thought it included 0) |
| Movingforward is offline | |
| | #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); 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 | |
| | #7 | |||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
Quote:
Quote:
__________________ 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 | |
| | #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;
}
|
| Movingforward is offline | |
| | #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 | |
| | #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 | |
![]() |
| Tags |
| display, random, user-input |
| Thread Tools | |
| Display Modes | |
|