Thread: Processing Char and Int with the input

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Processing Char and Int with the input

    I can't seem to process the input correctly. This program here works fine, but I need to add the ability to be able to capture either a single INT variable, or a CHAR then an INT, and I'm not sure how to do that. Here's a sample program of what I'm trying to do.

    Code:
    #include <iostream>
    using namespace std;
    
    int get_input( )
    {
        char input[30];
        char *end_ptr;
        int var;
    
        for ( ; ; )
        {
            fflush(stdout);
            fgets(input, sizeof input, stdin);        
            errno = 0;
            var = strtol(input, &end_ptr, 0);
                
            if ( end_ptr == input )
               cout << "Not a valid numeric input." << endl;
            else if ( input[0] == '\n' || ERANGE == errno )
               cout << "Not a valid number." << endl;
            else
                break;
        }
        
        return var;
    }
    int main( )
    {
        int x, y;
        int MyTemp = 0;
        int MyValue[10] = {};
    
        for ( x = 1; x <= 10; x++ )
        {
            system( "cls" );
            cout << "Please enter the Temperature." << endl;
            cout << "Then enter 10 more Values." << endl << endl;
            cout << " TEMP: " << MyTemp << endl;
            for ( y = 1; y <= 10; y++ )
              cout << y << ": " << MyValue[y] << endl;
            
            if ( MyTemp == 0 ) // Temp cannot equal 0
            {
              cout << endl << "TEMP: ";
              MyTemp = get_input();
              --x;
              continue;
            }
            
            cout << endl << x << ": ";
            MyValue[x] = get_input();
        }
        
        return EXIT_SUCCESS;
    }
    You are first asked for a temperature, then 10 values. The part I cannot figure out is, in the get_input function I need a way to analyze the incoming input and see if it is just a numeric value, if so, "return var" just like normal so it can be entered in. But I need two commands that can be entered at any time, "Temp ###". Example...

    Please enter the Temperature.
    Then enter 10 more Values.

    TEMP: 100
    1: 1056
    2: 2010
    3: 1021
    4: 4019
    5: 0
    6: 0
    7: 0
    8: 0
    9: 0
    10: 0

    5: temp 109
    When you at entering the 5th value, you can go back, change the value of the temperature, and continue along like nothing ever happened. I've tried a few different examples, but most ended up looking something like

    cin >> variable1 >> variable2
    And that doesn't work very well when for the most part you are entering single numbers. Any help out there please!

  2. #2
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    I'm not sure if I fully understand what you want, but if you want the user to be able to go back to a previous value, perhaps you could ask what value they want to change every time or just at the end. Something like:

    Input the value you would like to change: 5
    Input the value for 5: 26

    And, to my knowledge, there's no easy way to check weather the user entered a char or a numerical character. I did a little test, and when you input a character when an int is expected, the computer thinks you pressed a very large number (30000+), so you could check that the number inputed is in a given range. Alternatively, you could complicate things even more with the getche() function.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I would use getline to read in a line at a time, and use sscanf or a similar function on it for simple parsing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. text processing assignment
    By nellosmomishot in forum C Programming
    Replies: 28
    Last Post: 11-25-2008, 03:56 PM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. Replies: 2
    Last Post: 03-24-2006, 08:36 PM