Arrays, print user input?

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");
	}
Looks familiar.
but if I try to use -1, it still prints 10 numbers on the screen, but anything after that is just random numbers.
1
2
3
const size_t maxValues = 10;
for ( int i = 0; i < maxValues; i++)
	cout << maxMinValues[ i ] << " ";
You are looping through all the array. if you don't initialise a variable, then it will hold garbage.
but anything after that is just random numbers.

What does this mean? garbage or spurious memory addresses are output?
what happens if after line 25 you write if(numValues==-1)break;
EDIT: by itself this does nothing.
size_t is defined in the <cstring> header however am not familiar with why it is required.
If the declaration of numValues is given a type int , the break code above is included and counter is substituted for maxValues in line 33 and the <cstring> header is included the program seems to behave. You may be able to establish why.

You are looping through all the array. if you don't initialise a variable, then it will hold garbage.



I don't understand what this means. Forgive me if I sound dumb, I am fairly new at this and extremely confused. What varialbe needs to be initialized? I
Topic archived. No new replies allowed.