PDA

View Full Version : While Loop


3li773
08-26-2003, 11:48 AM
Hi I was making a simple number guess game this morning and I cant get the loop to work at all can someone tell me what is wrong
(And no laughing because a newb can't get a loop to work)

#define MAX_RANGE 100
#define NUM 89

#include <iostream.h>
int main()
{
int counter=0;
int cmd;

cout<<"Guess a number between 1-"<<MAX_RANGE<<endl;
while (cmd!=NUM);
{

scanf("%i",cmd);
counter++;

if (cmd==NUM)
{
cout<<"Good job"<<endl;
cout<<"It took you"<< counter <<"tries"<<endl;
cin.get();
system("EXIT");
}

else if (cmd<NUM)
{
cout<<"Guess higher"<<endl;

}

else if (cmd>NUM)
{
cout<<"Guess lower"<<endl;


}
return 0;
}
}

any help would be good

_underdog
08-26-2003, 12:36 PM
does not look like you are initializing cmd before you enter your while loop the first time.

DNAunion2000
08-26-2003, 08:07 PM
/*DNAunion*/ Several stylistic points.

1) In C++, the old C function scanf() is not usually used: the C++ cin is.


cin >> cmd;


2) In C++, it is considered "bad style" by some to use #DEFINE; they prefer the use of const.


const int MAX_RANGE = 100;
const int NUM = 89;


3) Using system("EXIT") is not the best way of ending a program. You could use


exit(0);


instead, but best of all would be to modify your code so that flow makes it all the way to the end of main, only "exiting" then. This is in keeping with structured programming.

4) Finally, to get your code indentations to be retained in your post, wrap your code in code tags. For example


if (cmd==NUM)
{
cout<<"Good job"<<endl;
cout<<"It took you"<< counter <<"tries"<<endl;
cin.get();
system("EXIT");
}


is done using:

[ code ]
if (cmd==NUM)
{
cout<<"Good job"<<endl;
cout<<"It took you"<< counter <<"tries"<<endl;
cin.get();
system("EXIT");
}
[ /code ]

but without the spaces inside the tags (they are there here so that the tags aren't parsed, so that you can see them).

3li773
08-27-2003, 12:50 PM
Ya i uselly use cin >> Variable but i want sure which one to use

thanks for all the tips

gish
08-27-2003, 02:29 PM
EDIT:
never mind...sorry.....my mistake
:P