How to display sum of values in forloop

Hi, I have to write a program accepts a varying number of prices (determined by the user) and runs a few calculations based on those prices. I am required to use a controlled for-loop. This is how far I was able to get. I have no idea what variable holds the sum of all the prices or how I would go about displaying it. Can someone lend me a hand? Note that I left the calculation function blank because I don't know where the value is stored. As is, the program compiles and accepts the right prices, but nothing more.

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

// Function prototypes
void getNumber(double);
double prices();
double calculate();
void displayResult();

// Main function executes all other functions
int main()
{
  double sold;
  double prices;
  double sum;
  double calculate();
  cout << "==========================================================" << endl;
  getNumber(sold);
  //  prices();
  calculate();
  void disaplayResult();
}

// Ask user for number of items in the order and price of each item
void getNumber(double)
{
  double num, value, sum=0;
  int counter;
  cout << "How many items were purchased? ";
  cin >> num;
  for (counter=1; counter <= num; counter++)
    {
      cout << "Enter an item price: ";
      cin >> value;
      sum +=value;
    }
}

// Calculate subtotal, tax, and order total
double calculate()
{
}

// Display the final order information
void displayResult(double& sum)
{
  cout << setprecision(2) << fixed;
  cout << left; // Align text to the left side
  cout << setw(19) << "Subtotal: " << endl;
  cout << setw(19) << "Sales Tax: " << endl;
  cout << setw(19) << "Total: " << endl;
}
Is this right?
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
# include <iostream>
# include <iomanip>
using namespace std;

// Function prototypes
void getNumber(double);
double prices();
double calculate();
void displayResult();

// Main function executes all other functions
int main()
{
  double sold;
  double prices;
  double sum;
  double calculate();
  cout << "==========================================================" << endl;
  getNumber(sold);
  //  prices();
  calculate();
  void disaplayResult();
}

// Ask user for number of items in the order and price of each item
void getNumber(double)
{
  double num, value, sum=0;
  int counter;
  cout << "How many items were purchased? ";
  cin >> num;
  for (counter=1; counter <= num; counter++)
    {
      cout << "Enter an item price: ";
      cin >> value;
      sum +=value;
    }
    cout<<"Sum of: "<<sum;
}

// Calculate subtotal, tax, and order total
double calculate()
{
}

// Display the final order information
void displayResult(double& sum)
{
  cout << setprecision(2) << fixed;
  cout << left; // Align text to the left side
  cout << setw(19) << "Subtotal: " << endl;
  cout << setw(19) << "Sales Tax: " << endl;
  cout << setw(19) << "Total: " << endl;
}
@quantumleap-
line 16: You don't initialize sum.
line 26: You need to give the argument a name. In this case it should be sum and you need to pass it by reference.
Line 28: Remove sum as a local variable.

Line 41: calculate needs to do something.

Lines 49-53: These lines don't print any values.

@jam47: No. See above.
Topic archived. No new replies allowed.