Adding total cins entered?

If I entered multiple cin say for variable x, how do I total them all up at the end?

Example: x being the variable.
cin 45
cin 50
cin 22
The total would be 117 but how would I write that in c++?.
#include <iostream>

using namespace std;

int main ()
{
int x, x1, x2, x3;
cout << "Enter first number for x: \n";
cin >> x1;
cout << "Enter second number for x: \n";
cin >> x2;
cout << "Enter third number for x: \n";
cin >> x3;
x = x1 + x2 + x3
cout << x;
cin.get();
return 0
}

I think you can cin an array and then put it into x too.
Last edited on
My example wasn't clear, sorry. I cin the number for variable ordered say twice. Two different numbers each time. The outfile below displays both numbers I put in for ordered (54 and 90) . I need to be able to add those numbers up.
Customer Report

Customer Number Ordered Paid Balance Result

1 $78.00 $54.00 $24.00 Amount owed

2 $89.00 $90.00 $-1.00 Credit









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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <fstream>
using namespace std;


ofstream outfile;

int main()
{
    
    int custid,numcust = 0;
    double ordered,paid,balance,totalord,totalpaid,totalbal;
    
    outfile.open("Customer Report");
    
    cout.setf(ios::fixed,ios::floatfield);
    cout.precision(2);
    outfile.setf(ios::fixed,ios::floatfield);
    outfile.precision(2);
    
    
    outfile << "\t\t\t\t\tCustomer Report" << endl << endl;
    outfile << "Customer Number\t\tOrdered\t\tPaid\t\tBalance\t\tResult" 
    << endl<< endl;
    cout << "Please enter the customer number" << endl << endl;
    cin >> custid;
    cout << endl;
    while (custid >=0){
          cout << "Please enter the amount the customer's order is" << endl <<
          endl;
          cin >> ordered;
          cout << endl;
          cout << "Please enter how much the customer has paid" << endl << endl;
          cin >> paid;
          cout << endl;
          balance = ordered - paid;
          
          cout << "Customer #" << custid << " ordered $" << ordered 
          << " worth of goods" << " and has paid $" << paid 
          << " with a balance of $" << balance << endl << endl;
          
          outfile << "\t" << custid << "\t\t" << "$" << ordered << "\t\t" 
          << "$" << paid << "\t\t" << "$" << balance;
          if (balance < 0)
             outfile << "\t\tCredit";
          else
             outfile << "\t\tAmount owed";
             outfile << endl << endl;
          cout << "Please enter the customer number" << endl << endl;
          cin >> custid;
          numcust++;
    }
    
    cout << "We processed " << numcust << " customer(s)" << endl;
    totalord = ordered + 
    system("PAUSE");
    return 0;
}
I may be missing something here, and really the explanation isn't very clear. This seems to be a case of simply keeping a running total. Initialise totalord and others to zero, and keep adding to them as you go along.
For example, after

cin >> ordered;

add

totalord += ordered;

and so on...
Topic archived. No new replies allowed.