// get a number from the user
cout << "Give me a number from 1 - 10 to try and guess mine: ";
cin >> seed;
// use that number to "seed" the random generator
srand(seed);
// now you can get a random number with the function rand()
test = rand() % 10 + 1;
if (test == 1)
cout <<" You got it right it was " << test <<endl;
else if (test != 1)
cout<<" Nope wrong number it was " << test <<endl;
if (test == 2)
cout <<" You got it right it was " << test <<endl;
else if (test != 2)
cout<<" Nope wrong number it was " << test <<endl;
if (test == 3)
cout <<" You got it right it was " << test <<endl;
{
if (test != 3)
cout<<" Nope wrong number it was " << test <<endl;
}
if (test == 4)
cout <<" You got it right it was " << test <<endl;
{
if (test != 4)
cout<<" Nope wrong number it was " << test <<endl;
}
if (test == 5)
cout <<" You got it right it was " << test <<endl;
{
if (test != 5)
cout<<" Nope wrong number it was " << test <<endl;
}
if (test == 6)
cout <<" You got it right it was " << test <<endl;
{
if (test != 6)
cout<<" Nope wrong number it was " << test <<endl;
}
if (test == 7)
cout <<" You got it right it was " << test <<endl;
{
if(test != 7)
cout<<" Nope wrong number it was " << test <<endl;
}
if (test == 8)
cout <<" You got it right it was " << test <<endl;
{
if (test != 8)
cout<<" Nope wrong number it was " << test <<endl;
}
if (test == 9)
cout <<" You got it right it was " << test <<endl;
{
if (test != 9)
cout<<" Nope wrong number it was " << test <<endl;
}
if (test == 10)
cout <<" You got it right it was " << test <<endl;
{
if (test != 10)
cout<<" Nope wrong number it was " << test <<endl;
}
}
int main( )
{
char choice;
show_random();
// ask the user if they want to repeat
cout << "Want to play again? (y/n) => ";
cin >> choice;
// check if the choice is not 'y' or 'Y'
if (choice != 'y' && choice != 'Y')
{
// stop if the choice is not 'y' or 'Y'
cout << "Bye-bye now ........ " << endl;
}
// otherwise, we loop back up to the top
cout << "Okay, one more time >>>>>>>>>>>>>>>> " << endl;
return 0;
}
Sorry on how it looks it isn't finished as you can see but I'm trying to make the whole thing loop till someone types in n or N but I just can't get it to work I have put while (true) before "Want to play again? (y/n)" thing but as an output I just get the exact same thing non stop can I get some help or explanation as to where it goes?
Also on the whole if statements I know they are wrong don't stress over it but if you can figure as to why when I cin a 2 it doesn't give me the if == 2 bit part I've seen it instead give me the if == 5 part.
Also I have no idea if all the # are needed since I'm new to the whole C++ thing.
Please tell me why on new ways to add things I want to come out of this learning my mistakes and how it is done correctly...try not to make it too hard for me to understand.
The following code does all that you are trying to do above.
1. Use a do-while loop (refer to code lines 10, 11 and 38 below).
2. srand (time(NULL)); is needed only once at start of int main. (Instead of seeding it put #include <ctime> as
a header at top.
3. None of the other headers are really needed for this.
#include <iostream>
#include <ctime>
usingnamespace std;
int main()
{
srand(time(NULL));
char yesorno;
do
{
int random = (rand()%10+1);
int choice;
cout<<"Give me a number from 1 - 10 to try and guess mine: \n";
cin>> choice;
if (random==choice)
{
cout<< " You got it right! Number was "<< random <<endl;
}
if (random != choice)
{
cout<< " Nope, wrong number! Number was "<< random <<endl;
}
cout<< "Want to play again? (y/n)\n";
cin >> yesorno;
if ((yesorno != 'y') && (yesorno != 'Y'))
{
cout <<"Bye now. Press enter to continue...";
}
}while ((yesorno == 'y') || (yesorno == 'Y'));
cin.ignore().get();
return 0;
}
You could make it work if you wanted to. I think a do while is more what you want as you want to loop while a condition is true, making sure it happens at least once.
I always automatically type in cin.ignore().get(); instead of using system ("pause"); when I first set up a empty project. It stops the console window disappearing when the program reaches the end, but probably not needed here with the do-while loop, but I always leave it there anyway...a good habit I guess.
I think there's an specific article on the evils of using 'system', in the ARTICLES section of this site.
Ah I think I got a glimpse at those evil all I saw that it was a security risk or something of the sort thanks I might as well pick up on that good habit now it would be helpful. Thanks again for making things clear for me everyone. I'll be sticking around with more questions as the days go by hopefully I'll get enlighten more.
Oh nice I never noticed that part I never really used system ( "pause" ) but I was aware that it just paused the program for sure ill pick up your habit very helpful after reading on that.
Yeah I first saw that on the console closing down topic when I joined
Can't I make a while(true) work instead of a do while
Yeppers:
1 2 3 4 5 6 7 8 9 10 11 12 13
char answer='y';
while (true)
{
... do some stuff
...
...
cout << "Press q to quit or Enter to continue.\n";
cin >> answer;
if (answer == 'q' || answer == 'Q')
break;
}
You can make lots of things work. Play around with it.
I would write in below manner because cin >> answer this statement can actually throw some error and putting it in the condition check ensure my program behave "normally".
1 2 3 4 5 6 7 8 9 10 11 12 13
char answer='y';
cout << "Press q to quit or Enter to continue.\n";
while (cin >> answer)
{
if (answer == 'q' || answer == 'Q')
break;
... do some stuff
...
...
cout << "Press q to quit or Enter to continue.\n";
}
No, break; simply breaks out of the current loop, whatever that is.
The reason break;'s are used in switch statements is to stop every case being performed.
The above code would cause every function to be called. FunctionA() would be called twice!
If ch was c, then functionC() and functionA() would be called, the functionA() that's in default.
This is perfectly acceptable, except sometimes we only want to do specific things.