CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2008
    Location
    Grand Rapids, MI USA
    Posts
    4

    Strange Input (keyboard) behavior and resetting cin

    I am trying to populate an array of ten elements with ONLY integers and then return the highest Integer from the array. It is a school assignment an the three functions is a requirement. Everything is workin great except two minor annoyances:

    1. When i press the "Enter" key without entering a character of any kind the cursor moves to a new line although it still waits for input. Is there an easy to to stop this?

    2. This is more of an annoyance. When I enter input with a decimal I need to press the "Enter" key twice to get my input prompt back. This is the issue I really would like to resolve.

    Thanks,
    John

    #include <iostream>
    #include <stdlib.h>
    #include <limits>

    using namespace std;

    int max_of(int numMax, int numNew)
    {
    // define and initialize variables
    int numOut = 0;

    // compare function members and return largest value
    // back to calling function
    if (numNew > numMax)
    {
    numOut = numNew;
    }
    else if (numMax >= numNew)
    {
    numOut = numMax;
    }

    return numOut;
    }

    int max_of_array(int numIn[])
    {
    // define and initialize variables
    int j = 0;
    int numMax = numIn[0];

    // loop through function elements and pass
    // then to another function for comparison
    for (j = 0; j < 10; j++)
    {
    numMax = max_of(numMax, numIn[j]);
    }

    return numMax;
    }

    int main ()
    {
    // define and initialize variables
    int numTest;
    int numIn[10];
    int i = 0;

    // initialize variable array
    for (i = 0; i < 10; i++)
    {
    numIn[i] = 0;
    }

    i = 0;

    // prompt user for input, validate and store in variable array
    while (i < 10)
    {
    cout << "Please enter an integer between -2147483648 and 2147483648 [" << (i+1) << "]: ";
    cin >> numTest;
    cin.ignore(numeric_limits<int>::max(), '\n');

    if (!cin || cin.gcount() != 1)
    {
    // clear and reset cin
    cin.clear();
    cin.ignore(numeric_limits<int>::max(), '\n');
    // reprompt user in case of bad input
    cout << endl;
    cout << "You entered bad data. Please try again." << endl;
    cout << endl;
    }
    else
    {
    //cin >> numTest;
    numIn[i] = numTest;
    i++;
    }
    }

    // pass input to another function and output the return
    cout << "The largest number you entered was " << max_of_array(numIn) << "." << endl;

    system("PAUSE");
    return 0;
    }

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Strange Input (keyboard) behavior and resetting cin

    • Please use code tags.
    • Quote Originally Posted by jcrouse
      1. When i press the "Enter" key without entering a character of any kind the cursor moves to a new line although it still waits for input. Is there an easy to to stop this?
      There probably is but it would be an ugly hack. The question is, why do you want to stop this? It's normal behaviour for a console window. Run cmd and press enter and see what happens.
    • Quote Originally Posted by jcrouse
      2. This is more of an annoyance. When I enter input with a decimal I need to press the "Enter" key twice to get my input prompt back. This is the issue I really would like to resolve.
      If you want to get a decimal number as input, you have to make your array an array of doubles or floats.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Feb 2008
    Location
    Grand Rapids, MI USA
    Posts
    4

    Re: Strange Input (keyboard) behavior and resetting cin

    Maybe I wasn't clear on the second point. I do want only interger input but if the users enters something such as 2.2 I then presses "Enter" I get a blinking cursor and nothing else. It is not until they press "Enter" the second time that my prompt comes up saying "invalid input, please try again."

    I am thinking that maybe I should somehow limit the character input to only numbers and the negative sign. Does that sound like a better method?

    Thnaks for the reply,
    John

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured