Kinda new to C++, and i dont know how to get this right. I need to prompt the user for an arbitrary number integers and use zero as a sentinel. The program must accommodate up to 100 numbers. When the users enters the sentinel, output the number of total numbers the user input along with the users' number list.
heres what i got so far, any help would be appreciated! thanks...
#include <iostream>
using namespace std;
int main()
{
//i am using 5 for testing purposes so i dont have to type in 100 numbers.
int numList[5];
cout << "Please provide some integers (up to 100). Enter 0 to exit.\n";
for (int x = 0; x < 5; x++)
{
cout << "Enter number " << x << ":";
cin >> numList[x];
}
}
So what exactly is your problem? What's left to do?
If I understood right, you are suposed to print the entire list of numbers plus the sum of all the numbers.
You've used a for cycle to input data to your array (and that's what you should have used), all you need to do know is to use a similar strategie to sum all the nums and to print them.
You can use 3 fors:
for-1:
input all numbers and assign them to numList.
for-2:
sum all numbers on numList and assign it to a variable.
for-3:
print numList. (You just need to change cout to cin)
Once you get this done try to get rid of the 2nd for as you can merge it to the first or third one.
this is what the program is supposed to 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: -7 // this would be the first number that the user input
Index 1: -9
Index 2: -9
Index 3: -5
Index 4: -10
Index 5: -5
Index 6: -9
Index 7: -8
Index 8: -1
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: -8
Index 18: 6
Index 19: 6 //this would be the last number the user entered
// for index/number 20, 0(zero) was entered so the input stops, then counts
//how many numbers total the user input then outputs the index's contents.
where in each array you will type in however many numbers (under100), 0 is entered, then the program will count the numbers that were input, then display them in there corresponding index.
Where are you having trouble exactly? the steps you need to perform are fairly basic;
1) Request input from the user for the # of integers to store.
2) Create an array or vector using that value (are you coming unstuck here?).
3) Loop from start to end of the array while requesting more input.
4) Check the input is not 0, if it is break from the loop, otherwise assign the value to your array member.
5) Another loop to output the values entered previously..
You don't seem to be lost, because you seem to have no knowledge to be lost in. What you are trying to acomplish is simple, go though the tutorial on this website. That should solve all our problems, if not come here again with a specific question and not:
"How can I do this?"
(sloppy9 has actually provided you a way to "how to do this", it's up to you to write it in c++ now.)