|
-
February 10th, 2008, 05:17 PM
#1
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;
}
-
February 10th, 2008, 10:18 PM
#2
Re: Strange Input (keyboard) behavior and resetting cin
- Please use code tags.
 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.
 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.
-
February 11th, 2008, 07:58 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|