Storing input numbers in an array and displaying them

I need to prompt the user for an arbitrary number of integers while using zero as a sentinel. This program accommodates up to 100 numbers. When the user enters the sentinel (0), it should output the number of user inputs along with the user's number list. I'm new to C++ and am having trouble getting my program to display the number list properly.

#include <iostream>
using namespace std;

int main()
{
const int x = 100;
int list[x];
cout << "Please provide some integers (up to 100). Enter 0 to exit." << endl;
for (int counter = 1; counter < x; counter++)
{
cout << "Enter number " << counter - 1 << ":" << endl;
cin >> list[counter];
if (list[counter] == 0)
{
cout << "You entered " << counter - 1 << " numbers." << endl;
cout << "Here they are:" << endl;
for (int counter = 0; counter < x; counter++)
{
cout << "Index " << counter << ": " << list[counter] << endl;
//Need to display the entire "list[counter]" from the previous input above
//but it only displays one line of code. I would like for it
//to display the complete list rather than just the first line.

return 0;
}
}
else
{
cout;
}
}
}


This is an example of what the program should output.

Please provide some integers (up to 100). Enter 0 to exit.
Enter number 0:
Enter number 1:
Enter number 2:
Enter number 3:
Enter number 4:
Enter number 5:
Enter number 6:
Enter number 7:
Enter number 8:
Enter number 9:
Enter number 10:
Enter number 11:
Enter number 12:
Enter number 13:
Enter number 14:
Enter number 15:
Enter number 16:
Enter number 17:
Enter number 18:
Enter number 19:
Enter number 20:
You entered 20 numbers.
Here they are:
Index 0: 19 // this would be the first number that the user inputs
Index 1: 18
Index 2: 2
Index 3: -5
Index 4: 4
Index 5: 9
Index 6: -9
Index 7: 6
Index 8: 7
Index 9: 1
Index 10: 2
Index 11: 1
Index 12: 5
Index 13: 4
Index 14: -6
Index 15: 9
Index 16: -5
Index 17: 2
Index 18: 6
Index 19: 2 //this would be the last number the user entered before entering zero
// for index/number 20, 0 was entered so the program stops telling the user to enter a number and then displays all of the user's previous input.


Last edited on
@redscout

You have your for loop, written wrong. It should be:
1
2
3
4
for (int i = 0; i< counter; i++) // Or any other variable besides an i, except for x, as it is a const
{
cout << "Index " << i << ": " << list[i] << endl;
}

not
1
2
3
4
5
for (int counter = 0; counter < x; counter++)
{
cout << "Index " << counter << ": " << list[counter] << endl;
return 0; // This will make program end after first cout printed. It shouldn't be in the loop 
}
Last edited on
Topic archived. No new replies allowed.