Arrays, print multiple user input?
Jul 16, 2011 at 5:59pm UTC
I'm trying to prompt the use to enter up to ten integers, then print them back out on the screen. If the user enters -1, the program should only print what the user entered, and then will go on to the next step. Right now, this code runs, but if I try to use -1, it still prints 10 numbers on the screen, but anything after that is just random numbers. Can anyone help me?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <iomanip>
using namespace std;
int minimum(const int *values, size_t numValues);
int maximum(const int *values, size_t numValues);
int main()
{
const size_t maxValues = 10;
size_t maxMinValues[ maxValues ];
size_t numValues;
int value = 0;
int counter = 0;
int values;
cout << "Enter up to 10 integers (-1 to stop): " ;
do
{
cin >> numValues;
maxMinValues[ value ] = numValues;
value++;
counter++;
}
while ( counter < 10 && numValues != -1 );
{
cout << "Values entered: " ;
for ( int i = 0; i < maxValues; i++)
cout << maxMinValues[ i ] << " " ;
}
cout << endl;
system("pause" );
}
Jul 17, 2011 at 4:45am UTC
This loop:
1 2
for ( int i = 0; i < maxValues; i++)
cout << maxMinValues[ i ] << " " ;
says "for each array index 0 through maxValues - 1 (9 in other words), print out the value at that index.
You want it to say:
"while the array index is less than 10 and the value stored at that position in the array is not -1, print out the value at that index"
Topic archived. No new replies allowed.