adding up multiple numbers

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;
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
still does not display the results of the numbers
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()
still doesn't work for me. The user is supposed to enter 0 for the program to add up and display all the numbers.
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
Topic archived. No new replies allowed.