Using a for loop, i need to write a program that gets 10 integers from the user, computes their sum, and prints the result to the screen. the only thing i don't understand is what to put in the for loop and how to sum those numbers. i need help, it's due tomorrow at 11:59 p.m.
#include <iostream>
usingnamespace std;
int main() {
int userNum = 0;
int sum = 0;
int i = 0;
cout << "Please input ten numbers that you would like summed.\n";
cin >> userNum;
for(i = 0; i < 10; ++userNum){
cout << "Your total is: " << sum;
return 0;
}
so the sum will be equal to sum + userNum right?
now i have this, but the sum does not execute, for example i assigned 1 through 9 to usernum and it does not give an answer.
#include <iostream>
usingnamespace std;
int main() {
int userNum = 0;
int sum = 0;
int i = 0;
cout << "Please input ten numbers that you would like summed.\n";
for(int i = 0; i < 10; ++userNum){
cin >> userNum;
sum = sum + userNum;
}
cout << "Your total is: " << sum;
return 0;
}
1. Declare an int (num) for your input and for the total (sum). Initialize to 0
2. for loop should be: for(size_t i = 0; i != 10; ++i)
3. Get the user input: std::cin >> num;
4. Add to the total: sum += num;
5. ignore the newline for the next read std::cin.ignore();
6. Loop ends here. Now just display the sum and you're done.