cout << "Hit q then enter to quit." << endl;
char input;
cin >> input;
if(input == 'q') {} //what is this line saying here?
// please I know this is stupid but it's confusing me at the moment
Printable View
cout << "Hit q then enter to quit." << endl;
char input;
cin >> input;
if(input == 'q') {} //what is this line saying here?
// please I know this is stupid but it's confusing me at the moment
if(input == 'q')
{
return 0;
}
Uh...If you are going to do this, you will need to make the main() function an integer.
I was just wondering because I saw it on somebody else's codes and I just want to see, know what it means, because I am a little bit confused, I mean I know what it's doing but how would you say it in human terms, for xample " as long as.......then...."
If the variable p has the value 'q' stored in it, then execute the code in the brackets.
ER,
You mean
'if the variable input has the value 'q' then...
cout << "Hit q then enter to quit." << endl;Quote:
Originally posted by elchulo2002
cout << "Hit q then enter to quit." << endl;
char input;
cin >> input;
if(input == 'q') {} //what is this line saying here?
// please I know this is stupid but it's confusing me at the moment
displays quoted message on screen and, advances display one line, sets cursor to beginning of line
char input;
declares variable named input of type char
cin >> input;
reads in one character that user types and stores it in char variable input
if(input == 'q') {}
checks if user typed letter q, and does nothing if the user did.
Why do that? I'd guess the program, or some part of it, ends right after that. The input probably pauses the program so that it doesn't flash by too quickly to see. It wouldn't really matter what the user typed, and they don't actually need the if statement; the program waits for the cin to occur, then continues. (It would check the if statement, but since it does nothing, just zips through it).
so if the user doesn't input q then it keeps looping right?
it doesnt matter what the user types..
if there is no more coding to the program then the progrom would end no matter what was typed..
but what if nothing was typed would it keep looping?
Console programming is sequental based. Basically what akira said sums it up:
When you don't type in anything it doesn't loop, it just sits there, waiting for input.Quote:
if there is no more coding to the program then the progrom would end
elchulo2002
The statement is not in a loop so it will not "keep on looping"
The cin will wait until something is input from the kb (+return).
The next statement is an IF statement, which will execute the instructions within the braces if the expression within the brackets is true, if not it will not execute the instructions within the brackets.
Since there are no instructions within the brackets, the effect will be the same no matter what character is typed.
The overall effect of this piece of code is that it will wait until the user types something and presses return (or just presses return for that matter), then continues with the program whatever that might be.Quote:
if there is no more coding to the program then the progrom would end