Can anyone spot my error in this loop? After i read in the reply I seem to be stuck in the If statements. Also, putting break in to exit the loop without the condition being true seems to throw an error: "Expected primary expression before else"
Code:
while (rowNum > (FC_Row))
{
cout << "That row is not located in our first class section. Would you like to change your class so you can sit in that row (Y/N)?" << endl;
cin >> reply;
reply = toupper(reply);
while (reply != 'Y' && reply !='N')
{
cin.clear();
while(cin.get()!='\n');
cout << "Please indicate your answer with Y (yes) or N (no)." << endl;
cout << "try again:" << endl;
cin >> reply;
reply = toupper(reply);
}
if (reply = 'Y')
ticketType = 'E';
break; // I want this to exit开发者_StackOverflow中文版 the while loop, I get a syntax error from this break.
else
cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
cin >> rowNum;
}
You need braces, and to replace =
with ==
in the if
condition:
if (reply == 'Y')
{
ticketType = 'E';
break;
}
else
{
cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
cin >> rowNum;
}
You're lacking {}
and you're using =
where you want ==
if (reply == 'Y') {
ticketType = 'E';
break;
} else {
cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
cin >> rowNum;
}
Turn up your compiler warning level, and make sure your code compiles warning-free. The if (reply = 'Y')
issue would've been spotted and warned about by most compilers.
if (reply = 'Y')
{
ticketType = 'E';
break; // I want this to exit the while loop, I get a syntax error from this break.
}
else
{
cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
cin >> rowNum;
}
You need curly brackets around the statements near the 'if'
if (reply = 'Y')
{
ticketType = 'E';
break; // I want this to exit the while loop, I get a syntax error from this break.
}
else
{
cout << "Then choose a row numbered 1-" << (FC_Row) << endl;
cin >> rowNum;
}
What do you think if (reply = 'Y')
means? If you're not sure, think about what reply = 'Y'
means, then consider that it means the same thing inside an if
.
You are doing
if (reply = 'Y') //Should be reply=='Y'
ticketType = 'E';
reply='Y' would always going to be true thus you would always enter in if () condition body.
if (reply = 'Y') //assignment
This is doing assignment in if
. Make it ==
.
if (reply == 'Y') //comparison!
I can see three problems with your code.
First off, you're using if (reply = 'y')
. =
is the assignment operator in C++, not the comparison operator. You want if (reply == 'y')
.
Second, your indentation and braces are screwed up. Remember that the correct form is this:
if (condition)
{
// Stuff
}
Unless you're only doing one thing inside the statement (which you're not).
Third, you have a semicolon in while(cin.get()!='\n');
. Kill it. You should only have a semicolon after while
if it is part of a do while
loop.
A good trick to avoid the = when you want == error and you compare to a literal constant is to invert the expression to
'Y' == reply
If you use only = it will give a compile error
精彩评论