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