how to convert int to char, char to int after get an input

前端 未结 1 1740
陌清茗
陌清茗 2021-01-28 13:09

I just jumped in studying cpp and don\'t know much so far.

I learnt \'if\' grammar recently and just made a homework for myself. You get an input from user. It can eithe

相关标签:
1条回答
  • 2021-01-28 13:55

    OK, I think I understand what you are wanting to do without a loop. Though you may need to remove the limit I have placed on integer input to insure a single-digit is input to translate to ASCII '0'->'9'. If I follow your question, you want to be able to take either a number or a character, validate it is in the range of [A-Za-z] or [0-9], and output the corresponding ASCII character and ASCII character value (or vice versa).

    For Example if the user enters the following, you want similar output to:

        input   output
        -----   -------------------------------------------
           5    '5' 5
         121    'y' 121
           y    'y' 121
         200    error: integer out of range
           =    error: character no within requested range
           P    'P' 80
    

    In order to accomplish this task, you essentially need to attempt a read of an integer, validate whether an unrecoverabel cin.eof() || cin.bad() occurs and exit with error. Otherwise cin.fail() occurs and the input was non-integer and remains in stdin where you can cin.clear() the failbit and then attempt a character read.

    With the character read, you have the same basic validations, except there is no reason to clear the failbit as your attempt to read an integer and then character has failed at that point.

    On a good read of either int or char, you simply need to perform the wanted validation to insure the int or char was within range and then format your output as desired.

    A short example with necessary validations could be as follows:

    #include <iostream>
    
    using namespace std;
    
    int main (void)
    {
        cout << "Enter a digit or upper or lower case character : ";
        int in_int;
        char in_char;
    
        if (!(cin >> in_int) ) {    /* attempt read of int */
            /* if eof() or bad() return error */
            if (cin.eof() || cin.bad()) {
                cerr << "(user canceled or unreconverable error)\n";
                return 1;
            }
            cin.clear();            /* clear failbit */
        }
        else {  /* good integer read */
            if (in_int >= 0 && in_int <= 9) {        // checking ASCII [0-9]
                cout << "'" << (char)(in_int + '0') << "' " << in_int << endl;
            }
            else if (in_int >= 'A' && in_int <= 'z') // checking ASCII [A-Za-z]
                cout << "'" << (char)in_int << "' " << in_int << endl;
            else {
                cerr << "error: integer input out of range.\n";
                return 1;   /* return failure */
            }
            return 0;       /* return success */
        }
    
        if (!(cin >> in_char) ) {    /* attempt read of char */
            /* if eof() or bad() return error */
            if (cin.eof() || cin.bad()) {
                cerr << "(user canceled or unreconverable error)\n";
                return 1;
            }
            else if (cin.fail())    /* if failbit */
                cerr << "error: invalid input.\n";
    
            return 1;
        }
        else {  /* good character input */
            if (in_char >= 'A' && in_char <= 'z') // checking ASCII [A-Za-z]
                cout << "'" << in_char << "' " 
                        << static_cast<int>(in_char) << endl;
            else
                cerr << "error: character not within requested range.\n";
            return 1;
        }
    }
    

    Example Use/Output

    $ ./bin/cinonechar
    Enter a digit or upper or lower case character : 5
    '5' 5
    
    $ ./bin/cinonechar
    Enter a digit or upper or lower case character : 121
    'y' 121
    
    $ ./bin/cinonechar
    Enter a digit or upper or lower case character : y
    'y' 121
    
    $ ./bin/cinonechar
    Enter a digit or upper or lower case character : 200
    error: integer input out of range.
    
    $ ./bin/cinonechar
    Enter a digit or upper or lower case character : =
    error: character not within requested range.
    
    $ ./bin/cinonechar
    Enter a digit or upper or lower case character : P
    'P' 80
    

    Look things over and let me know if you have further questions. You can adjust the test of int value range to correct any misunderstanding I have on the range you intend with your question.

    0 讨论(0)
提交回复
热议问题