I'm having trouble with my C++ programming assignment.
A mail order company sells 3 products, A, B, and C. The prices for these products are $1.99, $2.99, and $3.99. Write a program to read the product type and the quantity sold. Use a switch statement to determine the total sales for each transaction. Use a sentinel controlled loop to determine when the program should stop and display the final results.
I keep getting an error message: "The variable 'charges' is being used without being initialized."
So far I have:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int A = 1, B = 2, C = 3, Z = 4;
const int A_PRODUCT = 1, B_PRODUCT = 2, C_PRODUCT = 3, QUIT_PRODUCT = 4;
const double PRODUCT_A = 1.99, PRODUCT_B = 2.99, PRODUCT_C = 3.99;
int product;
int quantity;
double charges;
cout << setprecision(2) << fixed << showpoint;
cout << "Enter product type and quantity sold (enter Z to stop): ";
cin >> product >> quantity;
while (product != QUIT_PRODUCT)
{
cout << "Enter product type and quantity sold (enter Z to stop): ";
cin >> product >> quantity;
switch (product)
{
case A_PRODUCT:
charges = quantity * PRODUCT_A;
break;
case B_PRODUCT:
charges = quantity * PRODUCT_B;
break;
case C_PRODUCT:
charges = quantity * PRODUCT_C;
break;
}
cout << "Total sales of all products = " << charges << "\n";
} while (product != QUIT_PRODUCT)
return 0;
}
2) It's possible for your switch statement to fall through with none of the cases being hit. This means that, when you output the value of charges, no value will ever have been assigned to it.
As a general rule, it's sensible to initialise your variables with a sensible default.