Adding Functions in C++ ?

// Need help revising and/or adding functions to print order summary,
// including the products, the quantities, tax and the total price for each product.
// Calculate tax and print the total price for the order.
// Please add comments to suggested revisions and/or the addition of
// functions, thanks

#include <cstdlib>
#include <iostream>
#include <conio.h>

using namespace std;
int main()
{
char response;
double price,subtl;
int code,quantity;

cout<<"Welcome to the Creative Edge!\n";
cout<<"Specializing in Homemade, Vintage and Restored Items \n";
cout<<"Would you like to choose a product and enter the quantity (Y/N)\n";
cin>>response;

while(response=='y' || response == 'Y')
{
cout<<"=========Welcome to the Creative Edge Online Store========"<<endl;
cout<<"Code Product Price"<<endl;
cout<<"=========================================================="<<endl;
cout<<"101 Handmade Graphic Tshirts 22.00"<<endl;
cout<<"202 Handmade Knitted Sweaters 39.25"<<endl;
cout<<"303 Vintage Glass Vases 37.75"<<endl;
cout<<"404 Vintage Ceramic Plates 25.50"<<endl;
cout<<"505 Restored Tables 55.50"<<endl;
cout<<"606 Restored Metal Containers 48.00"<<endl;

double total = 0; //allows you to use += to calculate produce subtotal since no tax.
//Will also reset total to 0 when you want to serve a new customer.
do
{
cout<<"\nEnter Code:";
cin>>code;
switch(code)//the () tells what variable to use and the case are what value will cause that to occur.
{

case 101:
cout<<"Handmade Graphic Tshirts 22.00"<<endl;
cout<<"Enter Quantity: ";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=22.00;
subtl=(quantity*price);
cout<<"Subtotal $\n"<<subtl<<endl;
break; //breaks from case to continue in function

case 202:
cout<<"Handmade Knitted Sweaters 39.25"<<endl;
cout<<"Enter Quantity;";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=39.25;
subtl=(quantity*price);
cout<<"Subtotal $\n"<<subtl<<endl;
break;

case 303:
cout<<"Vintage Glass Vases 37.75"<<endl;
cout<<"Enter Quantity;";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=37.75;
subtl=(quantity*price);
cout<<"Subtotal $\n"<<subtl<<endl;
break;

case 404:
cout<<"Vintage Ceramic Plates 25.50"<<endl;
cout<<"Enter Quantity;";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity; }
price=25.50;
subtl=(quantity*price);
cout<<"Subtotal $\n"<<subtl<<endl;
break;

case 505:
cout<<"Restored Tables 55.50"<<endl;
cout<<"Enter Quantity:";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=55.50;
subtl=(quantity*price);
cout<<"Subtotal $\n"<<subtl<<endl;
break;

case 606:
cout<<"Restored Metal Containers 48.00"<<endl;
cout<<"Enter Quantity:";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=48.00;
subtl=(quantity*price);
cout<<"Subtotal $\n"<<subtl<<endl;
break;

case 0://Sentinal value that gives subtotal (because no tax) then ends your function.
cout<<"Subtotal: \n"<<total<<endl;
break;

default:
cout<<"Invalid Code!"<<endl;
break;
}

total+=subtl; //will add all the produce values up. is read as total=total+subt
}
while (code!=0);//checks if code = 0 at the end of function wont end till it is.
}
cout<<"Would you like to continue shopping?(Y/N)";
cin>>response;
}
Last edited on
You have a lot of repeating code. Code that stays the same should not be in your case statements; it should only be typed out once, within your while loop. Use function calls for data that varies.


Topic archived. No new replies allowed.