adding up multiple numbers

Oct 1, 2015 at 2:17am
Hello i am writing a simple program that will take in numbers from a user and add them all up. When the user enters 0 the program will stop taking input from the user and display the result. This is what i have so far,it compiles but is not showing the result.

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
  #include <iostream>
using namespace std;

int main(int argc, char** argv) 

{
	
	cout<<"enter a number to add up"<<endl;
	cout<<"enter 0 when you are done"<<endl;
	int x;
	int result;
	while( x!=0)
	{
		cin>>x; 
		result= x+x+x;
	}
	while(x=0){
		
		
		cout<<result<<endl;
	
	
	}
	
	
	return 0;
Oct 1, 2015 at 2:24am
How's this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using std::cout; using std::cin;
 
int main() 
{
	int sum = 0, num = 0;
 
	cout << "Enter in some numbers:\n";
	while (cin >> num)
		sum += num;
 
	cout << "Sum: " << sum << endl;
 
	return 0;
}
Last edited on Oct 1, 2015 at 2:24am
Oct 1, 2015 at 2:28am
still does not display the results of the numbers
Oct 1, 2015 at 2:32am
That's probably because the console window is closing down after execution.
Add this to the end of your code:
1
2
cout << "Press enter to exit...";
cin.get()
Oct 1, 2015 at 2:46am
still doesn't work for me. The user is supposed to enter 0 for the program to add up and display all the numbers.
Oct 1, 2015 at 3:03am
Then change the code so that it works? If you understand the code you should be able to fix it yourself. It's not that hard...

edit:
Here you go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using std::cout; using std::cin; using std::endl;

int main() 
{
	int sum = 0, num = 0;
	
	cout << "Enter in some numbers:\n";
	do
	{
		cin >> num;
		sum += num;
	}while (num != 0);
	
	cout << "Sum: " << sum << endl;
	
	return 0;
}
Last edited on Oct 1, 2015 at 3:08am
Topic archived. No new replies allowed.