While(???)

I have the following thing written:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;



void show_random()
{
int seed, test;


// 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.

Thank you very much before hand - Ako
Last edited on
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <ctime>
using namespace 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;
}
Last edited on
Oh so do while works only while the whole thing is true?
So I didn't have to seed it with a cin from the user?
Generally, seeding the RNG with user input isn't a very good idea since if everyone inputs "0", then everyone will get the same "random" number.

On loops: http://www.cplusplus.com/doc/tutorial/control/
Can't I make a while(true) work instead of a do while and also I don't understand why that cin.ignore().get(); before return does AlphaBravo
Last edited on
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.
Ah I see thanks for that also anyone that can point out why that cin.ignore() bit is there I don't seem to understand what it's doing there at all.
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.
Heres the link
http://www.cplusplus.com/forum/articles/11153/

heres the other link to console closing down
http://www.cplusplus.com/forum/beginner/1988/
Last edited on
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
Last edited on
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.
Last edited on
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";
}
Oh wouldn't the break; cause an error?

Because I thought it was only used for switch
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.

eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
char ch = 'a';

switch(ch)
{
   case 'a':
      ...// perform functionA();
   case 'b':
      ... // perform functionB();
   case 'c': 
      ...//perform functionC();
   default:
      // functionA();
}


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char ch = 'a';

switch(ch)
{
   case 'a':
      ...// perform functionA();
      break;
   case 'b':
      ... // perform functionB();
      break;
   case 'c': 
      ...//perform functionC();
      break;
   default:
      // functionA();
}
Last edited on
@ sohguanh

But then you have duplicate code. Like in your example, you have to print the statement twice.
Topic archived. No new replies allowed.