|
I ask the user to enter if he wants to continue, compare the input to 'n' || 'N', then to 'y' || 'Y'. Yet it executes both...
My code:
#include <iostream.h>
#include <string.h> //for stccpy
#include <stdlib.h> //for random randomize
#include <time.h> //for randomize
int playagain = 0;
int c = 0;
int cgoal;
int userguess;
int rightanswer = 4;
int maxn = 50, minn = 0;
char compareoutput [9];
char againinput;
int main ()
{
do //playagain
{
randomize();
cgoal = random(50);
do //rightanswer
{
c++;
cout << "Enter a number between " << minn << " and " << maxn << ": ";
cin >> userguess;
clrscr();
if (userguess == cgoal) {strcpy (compareoutput, "correct!"); rightanswer = 0;}
else
if (userguess < cgoal) {strcpy (compareoutput, "too small"); rightanswer = 1;}
else
if (userguess > cgoal) {strcpy (compareoutput, "too big"); rightanswer = 2;};
switch(rightanswer)
{
case 1: if(minn < userguess) {minn = userguess;} break;
case 2: if(maxn > userguess) {maxn = userguess;} break;
} //switch
cout << "You entered " << userguess << ". This is " << compareoutput << "\n";
} while (rightanswer != 0);
cout << "You guessed the right number (" << cgoal << ") in " << c << " times";
cout << "\n\nPress any key to continue";
getch();
clrscr();
cout << "Play again? (y/n): ";
cin >> againinput;
if (againinput == 'n' || 'N')
{
playagain = 2;
cout << "\n\nPress any key to exit...";
};
if (againinput == 'y' || 'Y')
{
playagain = 1;
cout << "\nHave fun!\n\n";
maxn=50;
minn=0;
userguess=51;
};
} while (playagain <= 1);
return 0;
} //main
The dosbox output, no matter the input:
+---------------------------------
|Play again? (y/n): n
|
|
|Press any key to exit...
|Have fun!
|
|Enter a number between 0 and 50:
+----------------------------------
What am I doing wrong?
Major thanks in advance
<Added>
the typed in 'n' in the output would vary of course
<Added>
btw, I'm using Borland C++ v5.02
|
|
|
I don't see why you are using 2 seperate variables to do this...
int playagain and char againinput
you don't need the int playagain, you can initialize char againinput to 'w' and then set your while statement to
while (againinput=='y' || againinput=='Y')
|
|
|
The problem that you are looking to fix is the following lines should be changed from:
if (againinput == 'n' || 'N')
if (againinput == 'y' || 'Y')
to
if (againinput == 'n' || againinput == 'N')
if (againinput == 'y' || againinput == 'Y')
I would agree that you really don't need two variables to maintain your play again state. I would also avoid do-while loops (use while(){} instead).
|
|
|
I usually choose what loop to used based on the application, or need.
If I need to check the condition before it runs the first instance of the loop, then I'd just use a while loop, but if I want it to run the first instance of the loop before checking then the do-while loop supports it
<Added>
Also, you don't need to check if againinput is set to 'N' or 'n', 'Y' and 'y' would do just fine because you initialize it as 'x' which is != 'Y' or 'y', therefor it wouldn't execute the loop again.
|
|
|
|
|
|
|
// |