Write your question here.
I have no idea how to get the subtotal with the loop. This is still not Finish btw
Cashier 2.0 is a C++ program that displays on the screen item codes with corresponding item description and price (at least 10 items). It asks the user to enter the code of the item purchased by the customer. If the user enters a code that is not in the choices, it asks again until he/she enters a valid code. Then, its description and price are displayed on the screen. Also, it asks for the quantity of the particular item code entered. Again, if the user enters a zero or negative value, it asks again for the quantity until he or she enters a valid value. Afterwards, it displays the subtotal amount. It will keep on asking for an item code, and will process the same until such time that the user enters ‘0’. When the user enters ‘0’, the total amount due is displayed. Then, it asks the user to tender the amount of cash of the customer. If the amount tendered is less than the amount due, it asks again for the amount until he or she enters a valid amount. Then, it prints the change due to the customer. Finally, it asks the user if the user wants to process more transactions.
...whitespace is your friend. That is one of the beauties of C++. You can put in as much whitespace as you want (assuming in non-string cases here) with no repercussions, compared to whitespace-dependent languages. It won't affect the end result of the code, but at least it'll be legible... next time when you go to look at your code, be a bit less stringent on your uses of the tab (which should be default 4 spaces, simply because it looks better that way) and enter key. It'll help you spot errors more easily when all of your code isn't crammed into eye-gouging boxes.
As for your actual issue... well, for one, I recommend using a switch statement, simply because long changes of if-else statements with integers looks messy in comparison to a nice, concise switch statement. About loops... I recommend that you take a look at the documentation on this site, which explains the beauty of the while loop. In several cases here, while loops would be ideal. Hell, just setting up a function for calculating the subtotal would be a good idea. That, compared to re-stating it each time. However, that's a bit beyond this program, so for now, just look at the documentation. You should also find some info on those aforementioned switches.
Made some changes to your program. Cleaned up some unnecessary statements. Also made it easier through switches incase you have yet to use switch statements.
#include <iostream>
#include <conio.h>
usingnamespace std;
int main()
{
char response;
double price,subt;
int code,quantity;
cout<<"Do you have a customer (Y/N)";
cin>>response;
while(response=='y || response == 'Y')
{
cout<<"=========Welcome to Market========"<<endl;
cout<<"Code Product Price"<<endl;
cout<<"101 Apple 20.00"<<endl;
cout<<"102 Orange 25.25"<<endl;
cout<<"103 Grapes 30.00"<<endl;
cout<<"104 W.Melon 35.25"<<endl;
cout<<"105 Mango 30.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<<"Apple 20.00"<<endl;
cout<<"Enter Quantity: ";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=20.00;
subt=(quantity*price);
cout<<"$"<<subt<<endl;
break; //breaks from case to continue in function
case 102:
cout<<"Orange 25.25"<<endl;
cout<<"Enter Quantity;";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=25.25;
subt=(quantity*price);
cout<<"$"<<subt<<endl;
break;
case 103:
cout<<"Grapes 30.00"<<endl;
cout<<"Enter Quantity;";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=30.00;
subt=(quantity*price);
cout<<"$"<<subt<<endl;
break;
case 104:
cout<<"W.Melon 35.25"<<endl;
cout<<"Enter Quantity;";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity; }
price=35.25;
subt=(quantity*price);
cout<<"$"<<subt<<endl;
break;
case 105:
cout<<"Mango 30.00"<<endl;
cout<<"Enter Quantity:";
cin>>quantity;
if(quantity<=0)
{cout<<"Invalid Quantity"<<endl;
cin>>quantity;}
price=30.00;
subt=(quantity*price);
cout<<"$"<<subt<<endl;
break;
case 0://Sentinal value that gives subtotal (because no tax) then ends your function.
cout<<"Subtotal: "<<total<<endl;
break;
default:
cout<<"Invalid Code!"<<endl;
break;
}
total+=subt; //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<<"Do you have anymore customers?(Y/N)";
cin>>response;
}
if you want to make the quantity keep looping till the cashier enters in a acceptable quantity you can nest a while loop in the quantity statement
which would look like
1 2 3 4 5 6 7 8 9 10 11
case 105:
cout<<"Mango 30.00"<<endl;
cout<<"Enter Quantity:";
cin>>quantity;
while(quantity<=0)
{cout<<"Invalid Quantity Please Enter In Valid Quantity 1+"<<endl;
cin>>quantity;}
price=30.00;
subt=(quantity*price);
cout<<"$"<<subt<<endl;
break;
if you want a cash tendered portion I will give you a quick skeleton.
you would need two variables added. And this would be inserted into the case 0: portion since that is your sentinal value and values are isolated to the loop/function they are part of.
this works. lack of comments in the source code, but at least it studies a little.
should be fully responsive to the track that you provide. I am allowed to add the number of daily sales and daily receipts of the sale.
I lost a bit of time to do some embroidery.
I wanted to post it the other day but I had problems with the internet. I took the opportunity to do some testing.
#include <iostream>
usingnamespace std;
float totale, totalDay, subTotale;
int numCodice=0;
bool test=true;
int numSale=0;
void startScreen()
{
cout<<"+------------------------------------------------------------------------+"<<'\n';
cout<<"| company: PUSHER FRUIT 2013 |"<<'\n';
cout<<"+------------------------------------------------------------------------+"<<'\n';
cout<<"| COD: 1 = Orange; | COD: 2 = Apple; | COD: 3 = Uva; | COD: 4 = Angurie; |"<<'\n';
cout<<"+------------------------------------------------------------------------+"<<'\n';
cout<<"| COD: 0 = end Sum; | COD: 9 = Exit; |"<<'\n';
cout<<"+------------------------------------------------------------------------+"<<'\n';
}
void miniscreen()
{
cout<<"+------------------------------------------------------------------------+"<<'\n';
cout<<"| COD: 1 = Orange; | COD: 2 = Apple; | COD: 3 = Uva; | COD: 4 = Angurie; |"<<'\n';
cout<<"+------------------------------------------------------------------------+"<<'\n';
cout<<"| COD: 0 = end Sum; | COD: 9 = Exit; |"<<'\n';
cout<<"+------------------------------------------------------------------------+"<<'\n';
}
float calcTotal(float cUnit)
{
float quantita =0;
cout<<" ********** input weigth: "<<'\n';
cin>>quantita;
subTotale = quantita * cUnit;
totale+=subTotale;
totalDay+=totale;
cout<<" ### totale per prodotto= "<<subTotale<<" euro. ### totale parziale= "<<totale<<" euro "<<'\n';
return totale;
}
float prodotto(int n_Codice)
{
float costoUnitario=0;
while(test)
{
switch (n_Codice)
{
case 1:
{
costoUnitario =1.50;
cout<<" Arance di Sicilia "<<costoUnitario<<" euro/kg"<<'\n';
break;
}
case 2:
{
costoUnitario =2.50;
cout<<" Mele dell' Alto Adige "<<costoUnitario<<" euro/kg"<<'\n';
break;
}
case 3:
{
costoUnitario =3.00;
cout<<" Uva del Salento "<<costoUnitario<<" euro/kg"<<'\n';
break;
}
case 4:
{
costoUnitario =0.50;
cout<<" Anguria del Salento "<<costoUnitario<<" euro/kg"<<'\n';
break;
}
case 0:
{
cout<<" end products "<<'\n';
break;
}
case 9:
{
cout<<" closed programm "<<'\n';
test=false;
break;
}
default:
{
cout<<" prodotto sconosciuto "<<'\n';
//test=false;
break;
}
}
if((test==true)&&((n_Codice > 0)&&(n_Codice<=4)))
{
calcTotal(costoUnitario);
cout<<"+-------------------------+"<<'\n';
cout<<"| input code product: |"<<'\n';
cout<<"+-------------------------+"<<'\n';
cin>>n_Codice;
if (n_Codice=='9')
numCodice=n_Codice;
}
else
test=false;
}
return totalDay;
}
int monetaResto()
{
float resto=0;
int modPay=0;
float contanti=0,subSaldo=0;
if (totale>0)
{
cout<<"+----------------------------------+"<<'\n';
cout<<"| totale dovuto: "<<totale<<" euro |"<<'\n';
cout<<"| scegli la modalità di pagamento: |"<<'\n';
cout<<"+----------------------------------+"<<'\n';
cout<<"| 1- Bancomat; |"<<'\n';
cout<<"| 2- Cash; |"<<'\n';
cout<<"| 3- Cash + Bancomat; |"<<'\n';
cout<<"+----------------------------------+"<<'\n';
cin>>modPay;
switch(modPay)
{
case 2:
{
cout<<" inserisci somma versata: "<<'\n';
cin>>contanti;
resto = contanti - totale;
cout<<" resto euro: "<<resto<<'\n';
numSale++;
cout<<" ticket n°: "<<numSale<<'\n';
cout<<" ***** **** *** thanks and goodbye *** **** ***** "<<'\n';
break;
}
case 3:
{
cout<<" inserisci la somma parziale in contanti "<<'\n';
cin>>contanti;
subSaldo = totale - contanti;
cout<<" saldo da versare tramite bancomat: "<<subSaldo<<'\n';
// fall trough;
}
case 1:
{
if (subTotale>0)
{
cout<<"+-----------------------+"<<'\n';
cout<<"| inserire PagoBancomat |"<<'\n';
cout<<"| inserire PIN code: |"<<'\n';
cout<<"| ....wait pls..... |"<<'\n';
cout<<"| transazione conclusa |"<<'\n';
cout<<"+-----------------------+"<<'\n';
numSale++;
cout<<" ticket n°: 00"<<numSale<<'\n';
cout<<" ***** **** *** thanks and goodbye *** **** ***** "<<'\n';
}
break;
}
default :
{
cout<<"+-------------------------+"<<'\n';
cout<<"| input cash: |"<<'\n';
cout<<"+-------------------------+"<<'\n';
cin>>contanti;
resto = contanti - totale;
cout<<" resto euro: "<<resto<<'\n';
numSale++;
cout<<" ticket n°: 00"<<numSale<<'\n';
cout<<" ***** **** *** thanks and goodbye *** **** ***** "<<'\n';
break;
}
}
}
return resto;
}
int main()
{
totalDay =0;
while (test)
{
bool otherSales = 0;
startScreen();
while (test)
{
cout<<"+-------------------------+"<<'\n';
cout<<"| input code product: |"<<'\n';
cout<<"+-------------------------+"<<'\n';
cin>>numCodice;
if (numCodice=='9')
break;
prodotto(numCodice);
miniscreen();
}
if (numCodice =='9')
break;
monetaResto();
cout<<" continue with sales? (1 = yes, 0 = not) ";
cin>>otherSales;
if (otherSales == 1)
{
test=true;
totale=0;
}
else
test=false;
}
cout<<"+-------------------------------+"<<'\n';
cout<<"| dayli receipts: euro | "<<totalDay<<'\n';
cout<<"| ticket n°: | 00"<<numSale<<'\n';
cout<<"+-------------------------------+"<<'\n';
return 0;
}
ps-there are only cases impossible in reality, for example, if you pay in cash, of course, the cash will always be greater than the cost of the fruit (otherwise the fruit, do not deliver the goods);)))