int main() {
char reply;
string login, pass;
check_pass_file();
while (true) {
cout << "a) Create new user\n";
cout << "b) Log in\n";
cout << "c) Quit\n";
cin >> reply;
switch (reply){
case'a':
cout << "User name: ";
cin >> login;
cout << "Password: ";
cin >> pass;
cout << "Creating user "<<login<<"...\n";
//creates user...
break;
case'b':
while (true){
cout << "Login: ";
cin >> login;
cout << "Password: ";
cin >> pass;
//checks for login and pass...
break;
}
}
break;
case'c':
return 0;
break;
default:
cout << "Only a, b or c\n";
break;
}
}
return 0;
}
basically what it does, it's that if I enter a ONE character like 'a' it does what it should. If I enter wrong character (not a, b or c) it also does what it should... but if I enter MORE than one character for example three letters, it runs the cycle three times... like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
a) Create new user
b) Log in
c) Quit
def<--here I entered three letters... and look...
Only a, b or c
a) Create new user
b) Log in
c) Quit
Only a, b or c
a) Create new user
b) Log in
c) Quit
Only a, b or c
a) Create new user
b) Log in
c) Quit
How to stop this?? And it's not working with "string". Maybe it is :) but I can't get it work :/
>> I have an error: Method 'ignore' could not be resolved
Mea culpa!
1 2 3 4 5 6 7 8 9 10 11
// ...
default:
cout << "Only a, b or c\n";
//cout.ignore( 2048, '\n' ) ; // throw away the rest of the characters
std::cin.ignore( 2048, '\n' ) ; // throw away the rest of the characters
// http://www.cplusplus.com/reference/iostream/istream/ignore/break;
// ...
it ignores characters after the first one that has been "cin-ed" to variable until it hits new line. And the number stands for length of stream I guess? :P
so I should change it to zero?
> it ignores characters after the first one that has been "cin-ed"
It discards characters that are remaining in the input buffer. Period.
> And the number stands for length of stream I guess? :P
The number is an upper limit on the number of characters that are to be extracted and discarded.
If the input buffer contained bbb\n, any number greater than or equal to four would clear the buffer (four characters would be extracted and thrown away). 2048 is used as a reasonably 'large enough' number.
> so I should change it to zero?
No. If you change it to zero, nothing will be extracted; if you change it to one, at most one character will be extracted etc.
Damn it :D I'm such a mess... I was putting it to bad place in code :/ Now I put it at the beginning where I ask for reply :P I was putting it in last case statement :/ ok I'm sorry :D but I've learned something new :) thanks :P
anyway :)
I want to do other thing too. If user enters that "bbbb" for example, I want program to return it as bad input! Like if he had entered "k" for example (and he had to choose only from a, b or c )
How to do that? :)