jcrouse
February 10th, 2008, 04:17 PM
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;
}
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;
}