This is code is like a shopping list. I feel like it's finished. My professor advised us to use this code "cin.getline" and "cin.ignore" i do not know how to use them. And maybe brush up this program a bit too.
i am suppose to make this program to make:
1. A void-function for user input of the three values.
The user will type in these features, and they should be saved using the new data type (Item)
2. A value-returning function that will calculate the cost (quantity times price) of the purchase of each item.
3. A void-function that will display the output of the user’s purchase, that is, the item purchased, the quantity purchased the price of one unit of the time, and the cost of the purchase.
4. A function that keeps track of the subtotal, which will be the total after all the items are purchased.
5. A function that adds 6% sales tax to the final total and prints it out to two decimal places.
6. The main function will contain variable declarations, function calls, and a single while loop which will continue asking for new items as long as the user types ‘y’ or ‘Y’.
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 80
|
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <string>
#include <istream>
using namespace std;
struct items
{
string name;
int quantity;
double price;
};
void getinput(items&);
void printoutput(items&);
void get_totalpurchase(items&);
double total(int quantity,double price);
double includetax(double tax,double total);
int main()
{
items sale;
char flag='y'; //yes loop
while(flag=='y')
{
getinput(sale);
get_totalpurchase(sale);
printoutput(sale);
cout<< "Do you have anything else?(y,n)";
cin>>flag;
if (flag!='y')
cout<<"Final purchase, including tax is: "<<includetax;
else
exit(EXIT_SUCCESS);
}
system("pause");
return 0;
}
void getinput(items&item)
{
cout<< "what will you buy?" ;
cin.getline(item.name)>>item.name; cout<<endl;
cout<<"How many will you buy?";
cin>>item.quantity ; cout<<endl;
cout<<"Input the price of item";
cin>>item.price; cout<<endl;
cin.ignore();
}
double total(int quantity,double price)
{
return total=quantity*price;
}
double includetax(double tax,double total)
{
tax=.06;
includetax=tax*total;
return includetax;
}
void get_totalpurchase(items&total) //get subtotal
{
cout<<quantity<<""<<total.name;
cout<<" subtotal is "<<total.total;
}
void printoutput(items&output)
{
cout<<"Item: "<<output.name;
cout<<"You have bought "<<output.quantity;
cout<<"Each is "<<output.price;
cout<<"The total would be "<<output.total;
}
|
I believe i did all the steps right but not a very good job.