Help with Sum of Input

I have an assignment due tonight for a beginner programming class. My instructions are:

(sum) Create a program that will output the sum of all of the numbers from 1 to 100.

I used an old program I had to do as a template, but my code makes the total number of inputs print out at the end, instead of the sum of however many numbers are input. How can I correct this?

I know it's a simple little mistake, and were my assignment not due tonight I wouldn't be asking.

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>
#include <cstdlib>

using namespace std;

int main()
{
	float inputNumber, totalSum=0;
	
	cout<<"Please input a number, or enter 0 to stop."<<endl;
	cout<<endl;
	cin>>inputNumber;
	if (inputNumber !=0)
	{
		while (inputNumber !=0)
		{
		totalSum++;
		cin>>inputNumber;
	}
}
	
	totalSum += inputNumber;	
	cout<<"The sum of all numbers entered is:"<<totalSum<<endl;
	
	system ("pause");
	return 0;
}
(sum) Create a program that will output the sum of all of the numbers from 1 to 100.

Sum thus equals 1+2+3+..+100

Use a for-loop and on every iteration add to the sum.


What does line 17 do?

When is a float exactly 0?

Only the line 22 adds inputNumber to the sum, but by the conditions on lines 13 and 15 the value of inputNumber is 0.
Topic archived. No new replies allowed.