codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  continue y/n problem  Warrior of the Light at 09:17 on Monday, October 31, 2005
 

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

  Re: continue y/n problem  80Degrees at 19:03 on Thursday, March 30, 2006
 

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')

  Re: continue y/n problem  INeedHelp2 at 00:10 on Friday, March 31, 2006
 

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

  Re: continue y/n problem  80Degrees at 02:39 on Friday, March 31, 2006
 

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.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  perl script help needed
•  onChange issue
•  perl remote execution
•  Chat application
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime
•  help me


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007