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>
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).
#include <iostream>
usingnamespace 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;
}
elseif (n == 1) {
cout << "Input the character: ";
cin >> a;
cout << "The character \"" << a << "\" has the number \"" << (int)a << "\" in the ASCII system." << endl;
}
elseif (n == 2){
cout << "Input the value: ";
cin >> x;
cout << "The number \"" << x << "\" corresponds to the character \"" << (char)x << "\" in the ASCII system." << endl;
}
elseif (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.