Storing variable values while in 'do' loop

Good afternoon people,

This will probably lead to a simple solution but I figured I'd seek clarifcation on how exactly to do it rather than keep using the trust 'trial and error' method.

So I'm just following an exercise that originally wanted to process 1 shirt order and a discount if you order over 5 shirts, no worries. Next exercise I needed to implement a loop for multiple shirt orders. I've got the loop running seemingly smooth but I want to be able to save the different variables to eventually add them up and give me a total. I've left how I thought it may work and given variables named: order1 - small, order2 - medium, order3 - large, order4 - xlarge. I thought perhaps I needed to declare the different variables in the switch statement, or perhaps per letter case within the switch statement i need if an else statements.

Here's the code anyway:


/*
* Program: Cash Register
* File: Register.cpp
* Description: This program is an exercise in using operators
* Author: Jamie Richardson
* Version: 1.00 21/3/2012
*/
// iostream must be included for the cout statement.
#include <iostream>

using namespace std;
//contant float unit prices of the shirt sizes

const float small = 25.0;
const float medium = 27.50;
const float large = 30.00;
const float xlarge = 32.50;
/*
* The main function is the starting point for the program. It contains all of the statements in this program.
*/
int main()
{
char status; //variables
char answer;
float total = 0.0;
float order = 0.0;
float order1 = 0.0;
float order2 = 0.0;
float order3 = 0.0;
float order4 = 0.0;
float unitCost;
float qty = 0;
float discount = .10;
float dprice = 0;
float dprice1= 0;

//begginning of program

cout << "<------------------------------------------------->\n";
cout << "Shirt purchase order\n\n";
cout << "Shirt sizes and characters for ordering purposes\n\n";

cout << "Small - S Medium - M Large - L Extra Large - X\n\n";
do //do loop
{ //opening of loop

cout << "Type in the character of the shirt size you wish to purchase ";
cin >> status;


cout << "Shirt you have ordered is ";

switch (status) //first shirt switch
{
case 's':
cout << "Small. Cost per unit is $" << small << endl;
unitCost = small;
order1 = order;

break;
case 'm':
cout << "Medium. Cost per unit is $" << medium << endl;
unitCost = medium;
order2 = order;
break;
case 'l':
cout << "Large. Cost per unit is $" << large << endl;
unitCost = large;
order3 = order;

break;
case 'x':
cout << "Extra Large. Cost per unit is $" << xlarge << endl;
unitCost = xlarge;
order4 = order;

break;
default:
cout << status << " is not a valid entry";
return 0;
} //close switch block

cout << "How many shirts would you like to order \n";
cin >> qty; //Collectiing how many shirts the customer wants to order

if (qty >= 5) //If customer is ordering more than 5
{ //open if loop
dprice = (unitCost * qty) * discount; //discount calculation
cout << "You receive a discount for bulk ordering of $" << dprice << endl;
order = (unitCost * qty) - dprice; //Total cost minus the discount
cout << "The price for your " << status << " is " << order << endl << endl;
} //close if loop

else if (qty < 5) //if the order is less than 5
{ //open else if loop
order1 = unitCost * qty;
cout << "The price for your is " << order << endl;
} //close else if
cout << "Would you like to purchase any other shirt size? Y = Yes N = No \n";
cin >> answer;
}//close do loop

while(answer != 'n');
total = order1 + order2 + order3 + order4; //calculation of multiple orders

cout << "Your purchase comes to a total of $" << total << endl;
cout << "<------------------------------------------------->\n";

} //close main function
Last edited on
Any feedback on this would be great on this issue. I've searched over the internet and haven't found any good clean sources.

Should I use an array or what to store multiple shirt orders?
I'd use std::vector, but an array could work.
Topic archived. No new replies allowed.