Displaying an array

I need help displaying an array. This program is supposed to ask the user to display number 1-100 using a loop and break out of the loop when the user inputs -99. Then, the program is supposed to display the values back. My problem is around line 18, it's not displaying anything.

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
 int main() {
	int x;
	int result = 0;

	const int size = 100;
	int y[size] = {};

	
		cout << "Enter values between 0-100 and enter -99 to stop" << endl;
		for (int x = 0;x < 100;x++) {
			cin >> x;
			if (x == -99) {
				break;
			}
			if (x < 0 && x < 100)
				cin >> y[size];
		}
		for (int i = 0; i < size; i++) {
			y[i] = i;
		}
		for (int i = 0; i <size; i++) {
			cout << y[i];
			cin >> x;
		}
			
}


Last edited on
Your for loop is incorrect.

It should be something like for(int i = 0; i < size; i++)

Use a while for user input, if number is not between 1 and 100 keep asking for input.

If the number is -99, break
Line 18 doesn't look like it's meant to display anything. Or line 19. If you want to display something, use cout.
There is a typo on line 15: x has to be greater than 0, but you wrote x < 0

Modify your for loop to this:
1
2
3
4
5
for(int index = 0; index < size; ++index)
{
    /*And then the rest of your code as you wrote it*/
}

Next, instead of on line 16 asking for input again, you need y[index] = x
With the for loop on line 18, you're overwriting the input values with values 1, 2, 3, ... 100

In the last for loop, remove the cin >> x statement. Why are you asking for input when outputting your array?
I don't understand what you're saying on line 16? Do you mean a different line?
No, on line 16, you receive input again to y[index], but looking at your code, I suppose you meant to store the value you read in x in the array.
So I've changed the code around, but now I'm having a problem with the last for loop. It's displaying all the elements of the array, but I only want to show the values from the user input. What can I do to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
        const int SIZE = 100;
	int y[SIZE] = {};

	for (int i = 0; i < 100;++i){
		cout << "Enter a value between 1-100 (Press -99 to stop)" << endl;
		cin >> y[i];

		if (y[i] == -99)
			break;
	}

	for (int i = 0; i < 100; ++i)
		cout << y[i] << endl;

	return 0;
}
Last edited on
@hh98265

One way would be to change line 13 and 14 to..
1
2
3
4
5
6
int i = 0;
while(y[i] != -99)
{
  cout << y[i] << endl;
  i++;
}
Last edited on
Topic archived. No new replies allowed.