It won't work

So the program is suppose to read three number hat are separated by commas and output the sum of the three numbers for example, Input: 1,2,3 SUM = 6

Instead it make me input one more number and comma than needed that displays the all the inputs in a row like: SUM = 1,2,3,4


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main() {

    string c1, c2, c3, sum;
  
    cout << "Enter three whole numbers seperated by a comma: ";
  
    cin >> c1;
    cin.ignore(1, ',');
    cin >> c2;
    cin.ignore(1, ',');
    cin >> c3;
        
    sum = c1 + c2 + c3;
  
    cout << "SUM = " << sum;
    
   return 0;

}
Last edited on
You have std::string instead of int for c1, c2, c3, and sum.
Last edited on
I did that but, its still making me put in four numbers instead of three, and now its outputting large numbers that are over 100 or 1000 when the sum is supposes to be something small like 6
How exactly are you entering the values?

After changing the type to int the program seems to work properly for me.
I entered 3,6,5 and I got the expected output of 14.
Topic archived. No new replies allowed.