>Is there a way to convert a user entered (cin) char to an integer?
Why convert it, why not just use the chars '0', '1', ... 'A', 'B', ... ?
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>Is there a way to convert a user entered (cin) char to an integer?
If you want to read each digit separately then it's a lot harder. Your best bet would be to read the entire number as a string and then parse it:
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
if ( getline ( cin, s ) ) {
static string hex_digits ( "0123456789ABCDEF" );
string::const_iterator it = s.begin();
string::const_iterator end = s.end();
for ( ; it != end; it++ ) {
string::size_type val = hex_digits.find ( toupper ( *it ) );
if ( val != string::npos )
cout<<"The value of "<< *it <<" is "<< val <<endl;
}
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401