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
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;
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace 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;
}