Dumb'n'Noobie question: Switch into While
Jan 19, 2018 at 3:09am UTC
Hello, World! (no offence!)
I'm studying C++11 from the Stroustrup but I have already study the C, then I know the basics of this type of programming languages (no offence again!).
I have this very simple code with a Switch into a While:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <iostream>
using namespace std;
int main()
{
int tries=1;
while (tries<4) {
cout << "Do you want to procees (y or n)?\n" ;
char answer = 0;
cin >> answer;
switch (answer) {
case 'y' :
return true ;
case 'n' :
return false ;
default :
cout << "Sorry, I don't understand that.\n" ;
++tries;
}
}
cout << "I'll take it for a no\n" ;
return false ;
}
It works (of course)! But my logic imposes me a question: If I put 'y' or 'n' like answer, why the program ends if "tries" is less than 4?
Thank you very much for your answers and sorry for my stupid question.
Last edited on Jan 19, 2018 at 3:13am UTC
Jan 19, 2018 at 3:32am UTC
If answer is 'y' or 'n', we return from main() (lines 14, 16)
Returning from main() ends the program.
Jan 19, 2018 at 3:53am UTC
That's Right. Thank you :)
Topic archived. No new replies allowed.