case problem

Hi :)
I'm having really weird problem. I have this script (or code if you will...)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 :/
Last edited on
> but if I enter MORE than one character for example three letters, it runs the cycle three times

1
2
3
4
5
6
7
8
9
10
        // ...
  
	default:
		cout << "Only a, b or c\n";

                cout.ignore( 2048, '\n' ) ; // throw away the rest of the characters
                // http://www.cplusplus.com/reference/iostream/istream/ignore/

		break;
        // ... 
Thanks, but :)
I have an error: Method 'ignore' could not be resolved
:/
I supect cin.ignore is what was intended.
>> 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;
        // ...  


Ok it's working now, but :/
If I enter "bb" or more "b"s, the output is:
1
2
3
4
5
a) Create new user
b) Log in
c) Quit
bb
Login: Password: 


why is that?
and it should ask for Login, and then for password :/
Last edited on
> why is that?

If you enter bbbb:

1. the first character b is accepted as 'reply'

2. bbb remains in the input buffer, which is then accepted as 'login'

3. it tries to read 'pass', but the buffer is now empty, so it waits for additional input.



oh :) got it now. So how to avoid that to happen? Is it possible? Should I clear "reply" variable?
> So how to avoid that to happen?

You already know that.

What does std::cin.ignore( 2048, '\n' ) ; do?
hmmm I'll try :P

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?
no :D with zero, it's back where I was at the beginning :P
> 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.
Last edited on
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? :)
> If user enters that "bbbb" for example, I want program to return it as bad input!

Read the input into a std::string And examine the string.

1
2
3
4
5
6
7
8
9
std::string reply_string ;
std::cin >> reply_string ;
if( reply_string.size() != 1 ) { /* error in input */ }
else
{
     char c  = reply_string[0] ;
     // check if c == 'a' etc.
     // ... 
}
AWESOME!
Thanks a lot! Working nice and flawless :)))
Topic archived. No new replies allowed.