Need help finding the sum of user input values

Write your question here.
The program I am needing to write is one that prompts the user for 4 random integers, assuming that the user will not throw in decimals or other uncommon punctuation. Add the 4 integers together and produce a sum for the user. I've spend a couple hours watching videos and browsing the forums but didn't have any success finding what I needed. Any help would be extremely appreciated! I'm a freshman at college and this is my very first Computer science class I'm sorry if I made any stupid mistakes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string alpha;
	
	cout << "Enter four integers (separated by white space): ";
	cin >> alpha;

		return 0;
}
Last edited on
Your on the right track with one error that I see so far.
Yes you can enter the numbers as a string, but keep in mind a string is like text.
You can not easily add text, so you would be better off to change line 8 to a INT.
If you want decimals then use a DOUBLE.
Next you should enter the numbers one at a time if that is possible and add the total as you go.
Since you need 4 numbers, you'll need a simple loop.

I took what I understood out of your message and continued scouring the internet and researching I came up with this. I had the upmost confidence when I ran it, but unfortunately instead of it producing the sum of those 4 integers it just closed. Any advice?!?! Anything and everything is very much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
	int firstNumber, secondNumber, thirdNumber, fourthNumber, sumOfFourNumbers;

	cout << "Enter four integers: ";
	cin >> firstNumber >> secondNumber >> thirdNumber >> fourthNumber;

	sumOfFourNumbers = firstNumber + secondNumber + thirdNumber + fourthNumber;

	cout << firstNumber << " + " << secondNumber << "+ " << thirdNumber << " + " << fourthNumber << " = " << sumOfFourNumbers;

		return 0;
}

Last edited on
Your code runs fine:
Enter four integers: 1 2 3 4
1 + 2+ 3 + 4 = 10


Read this thread:
http://www.cplusplus.com/forum/beginner/1988/#msg7262

Remember the code tags, please.
Last edited on
Just a little tinkering was all she needed. Thanks so much to everyone that gave me some help and pointed me in the right direction. This is only the second week of class so I'm sure I'll be posting a ton. Really appreciate the help everyone!!!
Topic archived. No new replies allowed.