Hi, I'd like to make a single input through a cin, and depending on if the input is a char or an int have the corresponding code happen.
Thanks in advance! :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
int main(){
int n = 0;
char a = 0;
cout << "Input a number to learn the ASCII character of that number, or a character to learn the number of that character: ";
//want one input to result in the corresponding statement based on the fact that it's an int or a char
cin >> n;
if (cin){
cout << "The character is: " << (char)n << endl;
}
else {
n = a;
cout <<"The number is: " << (int)a << endl;
}
return 0;
}
Accept input as a string and then examine the string. Do not ignore the possibility of the input being neither a character nor an integer. Don't forget the user can end the input stream at any time and your program's attempts to read input will fail.
Furthermore, only small numbers are ASCII codes and some of valid codes are not printable characters.
You did not ask a question, but I'll still note that
1. if line 11 fails, then line 10 has already failed and the content of 'n' is standard-dependent.
2. C++ has an elaborate cast syntax; you use the less controllable C syntax.
That will not work, as I'm intending to be able to input a number to get the symbol, as well as the other way around, otherwise that would be the correct solution, yes. :)
@LB
How would I examine the content? (any links would be helpful)
I was thinking about it, but then converting it seemed like too much of a hassle :/
@keskiverto
Yeah, I know the first 13 or so will not print anything, however that's not an issue as this is mostly for me copy-pasting characters or inputting numbers for other programs
EDIT: about the line 10/11, that's why I'd like to input the cin into both the char and the int, if the int fails go to the char, if the char fails return error :)
Reading a string from a stream always succeeds (unless you've reached the end of the stream), so you can take advantage of that and let temporary stringstream objects be placed into error states instead of your global input stream.
#include <iostream>
#include <cstdlib>
usingnamespace std;
int main(){
string string_to_convert = "";
cout << "Input a number to learn the ASCII character of that number, or a character to learn the number of that character: ";
//want one input to result in the corresponding statement based on the fact that it's an int or a char
cin >> string_to_convert;
if (string_to_convert[0] >= '0' && string_to_convert[0]<='9')
{
cout << (char) atoi(string_to_convert.c_str());
}
else
{
cout << (int) string_to_convert[0];
}
return 0;
}
#include <iostream>
#include <cctype>
#include <limits>
#include <locale>
#include <stdexcept>
int main()
{
/************************************************************
try
{
std::cin.imbue( std::locale( "en_US.UTF-8" ) ) ; // note: locale name is implementation specific
}
catch( const std::runtime_error& )
{
std::cerr << "*** error: failed to imbue locale with ASCII encoding\n" ;
return 1 ;
}
***************************************************************/
std::cout << "Input a number to learn the ASCII character of that number,\n"" or a character to learn the encoding of that character: ";
using int_type = std::char_traits<char>::int_type ;
int_type peek ;
if( ( peek = std::cin.peek() ) != std::char_traits<char>::eof() ) // if something was entered
{
if( std::isdigit(peek) || peek == '+' || peek == '-' )
{
int_type number ;
if( std::cin >> number )
{
if( number > std::numeric_limits<char>::max() || number < std::numeric_limits<char>::min() )
std::cout << "There is no character with encoding " << number << '\n' ;
elseif( std::isprint(number) )
std::cout << "The character with encoding " << number << " is: '" << char(number) << "'\n" ;
else
std::cout << "The character with encoding " << number << " is not printable\n" ;
if( number >= 0 && number < 10 )
std::cout << "The encoding for character '" << number << "' is: " << '0' + number << '\n' ;
}
else std::cout << "The encoding for character '" << char(peek) << "' is: " << peek << '\n' ;
}
else
{
std::cout << "The encoding " ;
if( std::isprint(peek) ) std::cout << " for character '" << char(peek) << "' " ;
std::cout << "is: " << peek << '\n' ;
}
}
}
What do you mean this is not an appropriate exercise?
I'd be fine if I only had to do one of the two parts (ie character to number, or the other way around), just curious if I could successfully create a program that did both.