Question about For Loops

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
using namespace 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;
}
Last edited on
1
2
3
4
5
6
7
cout << "Please input ten numbers that you would like summed.\n";

for(i = 0; i < 10; ++userNum) {
    cin >> userNum;
    
    // do your sum here
}
Last edited on
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.
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
27
 #include <iostream>
using namespace 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;
}
Last edited on
Line 8 isn't necessary.

 
for(int i = 0; i < 10; ++userNum)
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.
Topic archived. No new replies allowed.