-
Please!I need some help!
hi,how are you?
I wanna understand in the code listed below what does 3 things mean?
1:char Exit='N';
What does it mean?
2:while(toupper(Exit)!='Y')
What does it mean??
3:somewhere in the code we write: while(9>0)
i just wanna know why do we put this?
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
int main()
{
char Exit='N';
cout<<" *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_ *_*_*_*_*_*_*_*_*\n";
cout<<" *_*_*_*_*_*_*_*_*_*_*_*_Welcome to the Number machine*_*_*_*_*_*_*_\n";
cout<<" *_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_ *_*_*_*_*_*_*_*_*\n";
while(toupper(Exit)!='Y')
{
int Answer;
int Count=1;
int Number;
srand((unsigned)time(NULL));
Number=(rand()%99)+1;
cout<<" \n";
cout<<"\n";
cout<<"\n";
cout<<"1. I'm thinking of a number between 1 and 100\n";
cout<<"What is my Number?";
cin>>Answer;
while (9>0)
{
if ((Answer<1) ||(Answer>100))
{
cout<<"Nope,i said a number between 1 and 100\n";
Count=Count+1;
}
else if (Answer==Number)
{
cout<<"Good Job!"<<endl
<<"You won in "<< Count <<" tries!!"<<endl;
break;
}
//too high
else if (Answer>Number)
{
cout<<"Too High!!\n";
Count=Count+1;
}
//too low
else if (Answer <Number)
{
cout<<"Too Low!!\n";
Count=Count+1;
}
cout<<"What is my Number?";
cin>>Answer;
}
cout<<"Now press 'Y' to quit or 'N' to play again:";
cin>>Exit;
}
}
-
char Exit='N';
this creates a character object called Exit and sets it to 'N'. and
while
is a loop, and i think
while(toupper(Exit)!='Y')
means "loop while the variable Exit is Y"
-
1) you are right about that.
2) not quite.... toupper makes a char upper case and != means not equal to so:-
while(toupper(Exit)!='Y')
means keep doing this while the upper case version of the value of exit is not equal to 'Y'
3)while (9>0)...... this is always going to be true so this means loop forever.... alternatively it could have been written as while(1)