Cin to two different variables

Pages: 12
What should the program do, if the user writes "50cent"?
If it is in a loop print all the lines, otherwise just print the first line.

If the user writes "50cent" or "+50cent":
The character with encoding 50 is:  .... / is not printable. 
The encoding for character 'c' is: ...
The encoding for character 'e' is: ...
The encoding for character 'n' is: ...
The encoding for character 't' is: ...
(optional) The encoding is: <encoding for newline>


If the user writes "-50+":
The character with encoding -50 is:  .... / is not printable. 
The encoding for character '+' is: ...
(optional) The encoding is: <encoding for newline>


If the user hits enter without typing anything else:
(mandatory) The encoding is: <encoding for newline>

etc.
Okay, so I ended up doing this instead, however I'd like to know if the !cin works, and if it doesn't, how do I fix it? (I want it so if anything else then an integer is entered the same thing that happens when an integer other then 1, 2 or 3 is entered happens).

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
#include <iostream>
using namespace std;

int main(){
	int n = 0;
	int x = 0;
	char a = 0;
	
	while (n != 3){
		cout << "What would you like to do?\n1.Convert Char to Value\t 2.Convert Value to Char  3.Quit" << endl;
		cin >> n;
		if (!cin || n!= 1 && n != 2 && n!= 3){	//Does the !cin work?
			cout << "Error: Invalid Input!" << endl;
		}
		else if (n == 1) {
			cout << "Input the character: ";
			cin >> a;
			cout << "The character \"" << a << "\" has the number \"" << (int)a << "\" in the ASCII system." << endl;
		}
		else if (n == 2){
			cout << "Input the value: ";
			cin >> x;
			cout << "The number \"" << x << "\" corresponds to the character \"" << (char)x << "\" in the ASCII system." << endl;
		}
		else if (n == 3){
			cout << "Goodbye" << endl;
			break;
		}
	}

	return 0;
} 
Yes, !std::cin implicitly calls std::istream::operator bool on std::cin and then applies the ! operator on the returned boolean to negate it.

If the standard input stream is placed into a state of error you have to clear the error state and then also decide what to do with the data that is still waiting to be read and which caused the error in the first place. This is why it is recommended to read a string first and then defer that nonsense to a stringstream instead of pouring sludge into your one and only global input stream.
> !std::cin implicitly calls std::istream::operator bool on std::cin and then applies the ! operator on the returned boolean to negate it.

See: http://en.cppreference.com/w/cpp/io/basic_ios/operator!
Ah, I didn't know that it overloaded that operator - is there some reason for it to?
There is a minor difference between the two:
operator bool returns !fail() whereas
operator! returns fail() || bad()

Don't think it is of much significance in practice, other than that !std::cin is a bool in any context; std::cin is a bool only in a boolean context.
Topic archived. No new replies allowed.
Pages: 12