I have to build a C++ code that accepts 4 separate Hexadecimal inputs and returns in English the human readable for a set of machine code instructions. I can either get my program to accept characters or integers. At this time I have my program set to work on integers from 0-9. That leaves me with A,B,C,D,E and F that will not work in my program. Is there a way to convert a user entered (cin) char to an integer?

Any help would be greatly appreciated.

John

>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', ... ?

>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', ... ?

I did use characters at first. But when I use the switch statement, I need to compare the numbers for various case statements. I rely heavily on the arithmetic operations >,<,=,!=. If I can not use these operations my program will really be large. I was hoping that there was another way.

John

>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;
    }
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.