Hi, there. I have some troubles with my code... so.., you can see below the code.. If I type 'l' anything is working good. The message "Wait" Appear.. but, if I type 'q' it's showing me again the "wait" message...
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int main() {
char lq;
cout << "Welcome to ######## v 0.0.1. If you have a key, type ''l'', else, if you don't have a key, type ''q''." << endl;
cin >> lq;
if (lq == 'l', 'L') {
cout << "WAIT" << endl;
}
elseif (lq == 'q', 'Q') {
cout << "This program will pe stopped! " << endl;
system("pause");
exit(0);
};
return 0;
}
The problem is in the if statements. The comma operator is not working the way you are thinking. The if statement should be written as: if (lq == 'l' || lq == 'L'). Notice the logical or || in the condition. When comparing two different items they need separated with the logical && of ||.
Last part:
1 2 3 4 5 6
elseif (lq == 'q', 'Q') {
cout << "This program will be stopped! " << endl;
system("pause");
exit(0);
};
would look better as:
1 2 3 4 5 6 7
elseif (lq == 'q', 'Q')
{
cout << "This program will be stopped! " << endl;
system("pause");
exit(0);
};
Notice how the {}s line up in the same column while the code is indented. With your code I missed the {}s at first.
And I would avoid using "system" anything as it can be a problem and not everyone can use it. try using: std::cin.get(); in its place. This will allow you to press Enter or any letter or number + Enter.
On line 10 key, type ''l''. Is that the letter "L" in lower case or the number 1. It may be better to use upper case letters. Then you could use the upper case letter or change it to lower case with the function lq = std::tolower(lq);. Requires header file "<cctype>".
In the if statement for a single line the {}s are not needed. Only if there will be more than one line or you are planning ahead to add more lines later.