don't click here

Simple C++ Question

Discussion in 'Technical Discussion' started by Alriightyman, Oct 23, 2009.

  1. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    357
    11
    18
    Somewhere in hot, death Florida
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    how would you use an if statement to test whether a variable is a hexadecimal? When I use the following code, it works if it is hex, but jumps into an infinite loop if it isn't. Also I want it to prompt the user for input again for iNum.
    Code (Text):
    1. do
    2. {
    3.   if ( !(cin >> hex >> iNum))   // is iNum not hex?
    4.     // loop
    5.   else
    6.    // don't loop
    7. }while(stuff here);
    Any help would be appreciated. Thanks.
     
  2. SegaLoco

    SegaLoco

    W)(at did you say? Banned
    Well, with what you are doing there, I am not entirely sure. Storing it as hex doesn't change anything I think, it just converts whatever the next byte is to a hexadecimal number and then stores it as decimal in iNum or something. You would still have to call it as << hex << before you do stuff with it, I think. There really isn't a specific hexadecimal datatype, so this can be pretty difficult.
     
  3. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    1
    0
    Writing my own MD/Genesis sound driver :D
    cin>>hex>>iNum doesn't quite do that. cin>>hex will just read characters until the first invalid character, then return the number, or 0 if no valid number, into iNum, then returning the cin object.

    You want to check to see if a number is completely hex? You will need to read the number in as a string:

    Code (Text):
    1. string s;
    2. cin >> s; // if one word
    3.  
    4. getline(s, cin); // if a whole line
    Then you want to check s to see if it is a hex number by checking the characters. I'll leave that up to you.
     
  4. Ultima

    Ultima

    Games Publisher Tech Member
    2,398
    1
    18
    London, England
    Publishing mobile games!
    To check if you input is a valid hex value, use:

    Code (Text):
    1. bool IsAllHex (char* must_be_hex)
    2. {
    3. &nbsp;&nbsp;char copy_of_param[64];
    4.  
    5. &nbsp;&nbsp;return (strtok(strcpy(copy_of_param, must_be_hex), "0123456789ABCDEFabcdef") == NULL);
    6. }
    To convert a string to a C-style char* string, use c_str(), for example:

    string stringName = "hello";
    char cString[64] = stringName.c_str();
     
  5. Alriightyman

    Alriightyman

    I am back... from the dead! Tech Member
    357
    11
    18
    Somewhere in hot, death Florida
    0101001101101111011011100110100101100011 00000010: 0101001100000011 01000101011001000110100101110100011010010110111101101110
    Ok, thanks got it working.
     
  6. Andlabs

    Andlabs

    「いっきまーす」 Wiki Sysop
    2,175
    1
    0
    Writing my own MD/Genesis sound driver :D
    You can't do that. You have to strcpy the output of c_str(). Or you can
    Code (Text):
    1. const char *cString = stringName.c_str();
    but of course you can't modify that.

    I see Alrightyman already has his solution though.
     
  7. Ultima

    Ultima

    Games Publisher Tech Member
    2,398
    1
    18
    London, England
    Publishing mobile games!
    I very rarely find the need to use C-style strings, so I'm no expert with them. :P
     
  8. SegaLoco

    SegaLoco

    W)(at did you say? Banned
    Hah, I am backwards. I am so used to c-strings that c++ strings throw me for a loop.