Help with switch statements! And possibly other things...

Hello, I have been trying to create a program that will allow the user to input a product type and quantity sold however many times they would like, and use a sentinel in order to stop the program.

I've approached it from a ton of different directions, however my output either fails to loop, or infinitely loops on a certain section. I'll post the directions for the program and my code below.

I'm a beginner at Computer Programming (obviously) so the simpler the suggestions, the better.

Thank you!
Alex

-----------------------------------------------------------------
The assignment: (I don't expect anybody to do this for me, i just need
some suggestions on what to fix!)
A mail order company, Mail Mart, sells 3 products, A, B, and C. The prices for these products are $1.99, $2.99, and $3.99 respectively. Using the guidelines discussed in class, 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.

Sample output:
Enter product type and quantity sold (enter Z to stop): A 2
Enter product type and quantity sold (enter Z to stop): A 1
Enter product type and quantity sold (enter Z to stop): B 3
Enter product type and quantity sold (enter Z to stop): C 2
Enter product type and quantity sold (enter Z to stop): B 2
Enter product type and quantity sold (enter Z to stop): Z

Total sales of product A = $5.97
Total sales of product B = $14.95
Total sales of product C = $7.98
Total sales of all products = $28.90

------------------------------------------------------------------

[code]
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
//variables
char prodType;
int qSold;
double priceA = 1.99, priceB = 2.99, priceC = 3.99,
totalA, totalB, totalC,
totalSum;

cout << "Hello! Welcome to Mail Mart.\n\n";


cout << "Please select either A, B, or C to purchase. Enter 0 to exit: ";
cin >> prodType; //aquire product type


if (prodType = 0)
{

cout << "Thank you for shopping with us!" << endl;
cout << "*******************************" << endl;
}
else
{
cout << "\nNow please enter the ";
cout << "quantity you would like to purchase: ";
cin >> qSold; //determine how many they would like

switch (prodType) //use switch to determine which product they w
{
case 'a':
case 'A':
totalA = qSold * priceA;
break;

case 'b':
case 'B':
totalB = qSold * priceB;
break;

case 'c':
case 'C':
totalC = qSold * priceC;
break;

default: cout << "You must enter either A, B, or C." << endl;
}

cout << "\nThank you.\n\n";
cout << "Please enter product type, 0 to exit: ";
cin >> prodType;
}
totalSum = totalA + totalB + totalC;
cout << "The total sales for product A = " << totalA << endl;
cout << "The total sales for product B = " << totalB << endl;
cout << "The total sales for product C = " << totalC << endl;
cout << "The total sales for all products = " << totalSum << endl;


return 0;
}
This is like my 4th day of learning c++ so please let me know if there's a better way to do this.
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

char prodType;
int qSold;

double priceA = 1.99;
double priceB = 2.99;
double priceC = 3.99;
double totalA = 0;
double totalB = 0;
double totalC = 0;
double totalSum;
double qSoldA = 0;
double qSoldB = 0;
double qSoldC = 0;

int mainFunct(){ //created a function because if the user types 0 then the while loop will exit and skip to the total sales

    cout << "Hello! Welcome to Mail Mart.\n\n";

        while(prodType != '0'){ //checks if the char is 0, and if it is, we exit the loop of them entering products -- also, you were checking if product type == 0 , but you must use '0' because it is a char not an int

            cout << "Please select either A, B, or C to purchase. Enter 0 to exit: ";
            cin >> prodType;

            if(prodType != '0'){ //double checks if the char is 0 to make sure it does not cout this part if they entered 0
                cout << "\nNow please enter the ";
                cout << "quantity you would like to purchase: ";
                cin >> qSold;

            }

                switch (prodType){
                case 'a':
                case 'A':
                qSoldA += qSold;
                totalA = qSoldA * priceA;
                    break;

                case 'b':
                case 'B':
                qSoldB += qSold;
                totalB = qSoldB * priceB;
                    break;

                case 'c':
                case 'C':
                qSoldC += qSold;
                totalC = qSoldC * priceC;
                    break;

                case '0': //again, checking if char == '0', not 0
                    return 1; //we use this to end the function if they type 0. We had to create a function to exit the while loop successfully because we are using a switch loop within a while loop

                default: cout << "You must enter either A, B, or C." << endl;
                }
        }

}

int main()
{

mainFunct();//calling the function we created

totalSum = totalA + totalB + totalC; //this stuff is after we called mainFunct(); because we want them to keep entering the products until they type 0

cout << "The total sales for product A = " << totalA << endl;
cout << "The total sales for product B = " << totalB << endl;
cout << "The total sales for product C = " << totalC << endl;
cout << "The total sales for all products = " << totalSum << endl;
cout << "Thank you for shopping with us!" << endl;

return 0;
}


Last edited on
Topic archived. No new replies allowed.