Jump to content

Cashier Programming HELP!

Go to solution Solved by colonel_mortis,

Please use code tags. It's the <> button under the :) and it makes the code so much more readable.

 

You need a default case on enter code to exit before doing total+=subt otherwise it reads from an uninitialised variable

You enter the amount tendered to an int. Change it to a double and all your problems are solved

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, its breakdown and amount in words. Finally, it asks the user if he or she wants to process more transactions.

 

I have already done the code but whenever I put decimals on the part where you input the Amount to be tendered, it suddenly ends the program.

 

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

using namespace std;
int main()
{
    char response;
    double price,subt, change, change_word;
    int code,quantity,tender;
  
    cout<<"Start Transaction? (Y/N)";
    cin>>response;
   
    while(response=='y' || response == 'Y')
{
    cout<<"\t\t\t\tWelcome to 126 Mart!";
    cout<<"\n\n\tItem Codes\t     Description\t\tPrice";
    cout<<"\n\n\t100\t\tSweet n' Ripe Grapes\t\t125.00";
    cout<<"\n\n\t101\t\tCrunchy Apples\t\t\t52.00";
    cout<<"\n\n\t102\t\tGame Pass\t\t\t59.00";
    cout<<"\n\n\t103\t\tPacked Grapes\t\t\t105.00";
    cout<<"\n\n\t104\t\tBit Coins\t\t\t8.00";
    cout<<"\n\n\t105\t\tMouse Trap\t\t\t49.00";
    cout<<"\n\n\t106\t\tPlay Toy\t\t\t69.00";
    cout<<"\n\n\t107\t\tSennheiser Earphones\t\t3700.00";
    cout<<"\n\n\t108\t\tMouse Pad\t\t\t500.00";
    cout<<"\n\n\t109\t\tNescafe Latte\t\t\t23.00";
   
double total = 0;
            
   do
    {
    cout<<"\n\nEnter Code:";
 cin>>code;
 switch(code)
 {
 
 case 100:
    cout<<"Sweet n' Ripe Grapes 125.00"<<endl;
    cout<<"Enter Quantity: ";
    cin>>quantity;
    if(quantity<=0)
 {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=125.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
 break; 
 
 case 101:
    cout<<"Crunchy Apples 52.00"<<endl;
    cout<<"Enter Quantity: ";
    cin>>quantity;
    if(quantity<=0)
 {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=52.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
 break;
   
 case 102:
    cout<<"Game Pass 59.00"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
 {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=59.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
    break;

 case 103:
    cout<<"Packed Grapes 105.00"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=105.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
    break;

 case 104:
    cout<<"Bit Coins 8.00"<<endl;
    cout<<"Enter Quantity;";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
  cin>>quantity; }
    price=8.00;
    subt=(quantity*price);
 cout<<"Php "<<subt<<endl;
    break;

    case 105:
    cout<<"Mouse Trap 49.00"<<endl;
    cout<<"Enter Quantity:";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=49.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
    break;

    case 106:
    cout<<"Play Toy 69.00"<<endl;
    cout<<"Enter Quantity:";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=69.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
    break;
   
    case 107:
    cout<<"Sennheiser Earphones 3700.00"<<endl;
    cout<<"Enter Quantity:";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=3700.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
    break;

    case 108:
    cout<<"Mouse Pad 500.00"<<endl;
    cout<<"Enter Quantity:";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=500.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
    break;

    case 109:
    cout<<"Nescafe Latte 23.00"<<endl;
    cout<<"Enter Quantity:";
    cin>>quantity;
    if(quantity<=0)
    {cout<<"Invalid Quantity"<<endl;
 cin>>quantity;}
    price=23.00;
    subt=(quantity*price);
    cout<<"Php "<<subt<<endl;
    break;
         
 case 0:
    cout<<"Subtotal: "<<total<<endl;
   
    do{
    cout<<"Amount Tendered: ";
    cin>>tender;                                           //THIS IS WHERE THE PROGRAM SUDDENLY ENDS WHEN YOU PUT DECIMALS
    if ((tender-total)<0)
    {
    cout<<"Invalid Amount Tendered"<<endl;
    }
    }while ((tender-total)<0);

       change = tender-total;
       change_word = tender-total;
       cout<<"Change: "<<change;

             
    cout<<"\n\n\t=========CHANGE BREAKDOWN=========";
   
    int change1;
    char repeat;
   
   
    change1 = (int)change;
   
    int otb, fhb, thb, ohb, fb, tb, tc, fc, oc,tf_cent, ten_cent, fh_cent;
   
    otb =  (change1 / 1000);
    fhb = (change1 - (otb * 1000)) / 500;
    thb = (change1 - (otb * 1000) - (fhb * 500)) /200;
    ohb = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)) /100;
    fb = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)- (ohb * 100)) /50;
    tb = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)- (ohb * 100) - (fb * 50)) /20;
    tc = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)- (ohb * 100) - (fb * 50) - (tb * 20)) /10;
    fc = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)- (ohb * 100) - (fb * 50) - (tb * 20) - (tc * 10)) /5;
    oc = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)- (ohb * 100) - (fb * 50) - (tb * 20) - (tc * 10) - (fc * 5)) /1;
    tf_cent = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)- (ohb * 100) - (fb * 50) - (tb * 20) - (tc * 10) - (fc * 5) - (oc * 1.00)) /.25;
    ten_cent = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)- (ohb * 100) - (fb * 50) - (tb * 20) - (tc * 10) - (fc * 5) - (oc * 1.00) - (tf_cent * .25)) /.1;
    fh_cent = (change1 - (otb * 1000) - (fhb * 500) - (thb * 200)- (ohb * 100) - (fb * 50) - (tb * 20) - (tc * 10) - (fc * 5) - (oc * 1.00) - (tf_cent * .25) - (ten_cent * .1)) /.05;
   
    cout<<"\n\n\t\t1000\t x"<<otb<<"\t"<<otb * 1000;
    cout<<"\n\t\t500\t x"<<fhb<<"\t"<<fhb * 500;
    cout<<"\n\t\t200\t x"<<thb<<"\t"<<thb * 200;
    cout<<"\n\t\t100\t x"<<ohb<<"\t"<<ohb * 100;
    cout<<"\n\t\t50\t x"<<fb<<"\t"<<fb * 50;
    cout<<"\n\t\t20\t x"<<tb<<"\t"<<tb * 20;
    cout<<"\n\t\t10\t x"<<tc<<"\t"<<tc * 10;
    cout<<"\n\t\t5\t x"<<fc<<"\t"<<fc * 5;
    cout<<"\n\t\t1\t x"<<oc<<"\t"<<oc * 1;
    cout<<"\n\t\t0.25\t x"<<tf_cent<<"\t"<<tf_cent * .25;
    cout<<"\n\t\t0.10\t x"<<ten_cent<<"\t"<<ten_cent * .10;
    cout<<"\n\t\t0.05\t x"<<fh_cent<<"\t"<<fh_cent * .05;
   
    double dec, dec1;
    int whole, dec2;

    whole = int (change);
    dec = change - whole;
    dec1 = int (dec);
    dec2 = int (dec1);
   
    cout<<"\n\n\tYour Change is ";

    if((whole%1000)==9)
     cout<<"nine thousand ";
    else if((whole%1000)==8)
     cout<<"eight thousand ";
    else if((whole%1000)==7)
     cout<<"seven thousand ";
    else if((whole%1000)==6)
     cout<<"six thousand ";
    else if((whole%1000)==5)
     cout<<"five thousand ";
    else if((whole%1000)==4)
     cout<<"four thousand ";
    else if((whole%1000)==3)
     cout<<"three thousand ";
    else if((whole%1000)==2)
     cout<<"two thousand ";
    else if((whole%1000)==1)
     cout<<"one thousand ";

    if((whole%1000)/100==9)
     cout<<"nine hundred ";
    else if((whole%1000)/100==8)
     cout<<"eight hundred ";
    else if((whole%1000)/100==7)
     cout<<"seven hundred ";
    else if((whole%1000)/100==6)
     cout<<"six hundred ";
    else if((whole%1000)/100==5)
     cout<<"five hundred ";
    else if((whole%1000)/100==4)
     cout<<"four hundred ";
    else if((whole%1000)/100==3)
     cout<<"three hundred ";
    else if((whole%1000)/100==2)
     cout<<"two hundred ";
    else if((whole%1000)/100==1)
     cout<<"one hundred ";

    if(((whole%1000)%100)/10==9)
     cout<<"ninety ";
    else if(((whole%1000)%100)/10==8)
     cout<<"eighty ";
    else if(((whole%1000)%100)/10==7)
     cout<<"seventy ";
    else if(((whole%1000)%100)/10==6)
     cout<<"sixty ";
    else if(((whole%1000)%100)/10==5)
     cout<<"fifty ";
    else if(((whole%1000)%100)/10==4)
     cout<<"fourty ";
    else if(((whole%1000)%100)/10==3)
     cout<<"thirty ";
    else if(((whole%1000)%100)/10==2)
     cout<<"twenty ";
    else if(((whole%1000)%100)/10==1)
     cout<<"ten ";

    if((whole%1000)%100==19)
     cout<<"nineteen ";
    else if((whole%1000)%100==18)
     cout<<"eighteen ";
    else if((whole%1000)%100==17)
     cout<<"seventeen ";
    else if((whole%1000)%100==16)
     cout<<"sixteen ";
    else if((whole%1000)%100==15)
     cout<<"fifthteen ";
    else if((whole%1000)%100==14)
     cout<<"fourteen ";
    else if((whole%1000)%100==13)
     cout<<"thirteen ";
    else if((whole%1000)%100==12)
     cout<<"twelve ";
    else if((whole%1000)%100==11)
     cout<<"eleven ";
    else if(((whole%1000)%100)%10/1==9)
     cout<<"nine ";
    else if(((whole%1000)%100)%10/1==8)
     cout<<"eight ";
    else if(((whole%1000)%100)%10/1==7)
     cout<<"seven ";
    else if(((whole%1000)%100)%10/1==6)
     cout<<"six ";
    else if(((whole%1000)%100)%10/1==5)
     cout<<"five ";
    else if(((whole%1000)%100)%10/1==4)
     cout<<"four ";
    else if(((whole%1000)%100)%10/1==3)
     cout<<"three ";
    else if(((whole%1000)%100)%10/1==2)
     cout<<"two ";
    else if(((whole%1000)%100)%10/1==1)
     cout<<"one ";

     cout<<"pesos ";

     if(dec2==95)
     cout<<"ninety-five cents ";
     else if(dec2==90)
     cout<<"ninety cents ";
     else if(dec2==85)
     cout<<"eighty-five cents ";
     else if(dec2==80)
     cout<<"eighty cents ";
     else if(dec==75)
     cout<<"seventy-five cents ";
     else if(dec==70)
     cout<<"seventy cents ";
     else if(dec==65)
     cout<<"sixty-five cents ";
     else if(dec==60)
     cout<<"sixty cents ";
     else if(dec==55)
     cout<<"fifty-five cents ";
     else if(dec==50)
     cout<<"fifty cents ";
     else if(dec==45)
     cout<<"fourty-five cents ";
     else if(dec==40)
     cout<<"fourty cents ";
     else if(dec==35)
     cout<<"thirthy-five cents ";
     else if(dec==30)
     cout<<"thirthy cents ";
     else if(dec==25)
     cout<<"twenty-five cents ";
     else if(dec==20)
     cout<<"twenty cents ";
     else if(dec==15)
     cout<<"fifthteen cents ";
     else if(dec==10)
     cout<<"ten cents ";
     else if(dec==5)
     cout<<"five cents ";
   
    cout<<"\n\n\t\tThanks for Shopping!";
    break;

 default:
  cout<<"Invalid Code!"<<endl;
  break;
 }
  
 total+=subt;
 
 
    }
while (code!=0);

 

cout<<"\n\nDo you have anymore customers?(Y/N)";
cin>>response;
}
}

CPU: Intel Core i5-4670k Mobo: MSI Z97 Gaming 3 RAM: Crucial Ballistix Tactical Tracer 8GB 1866 Video Card: MSI GTX 970 GAMING 4G SSDSamsung 840 Evo 250GB HDD: WD 1TB + 500GB Caviar Blue
PSU: EVGA SuperNOVA 750W G2 Case: Gigabyte GZ-G1 Mouse: Corsair M65 RGB Keyboard: Corsair K70 MX Brown MousePad: Corsair MM200
Steam/Origin/Uplay: renz62

Link to comment
https://linustechtips.com/topic/52726-cashier-programming-help/
Share on other sites

Link to post
Share on other sites

Please use code tags. It's the <> button under the :) and it makes the code so much more readable.

 

You need a default case on enter code to exit before doing total+=subt otherwise it reads from an uninitialised variable

You enter the amount tendered to an int. Change it to a double and all your problems are solved

Edited by colonel_mortis

HTTP/2 203

Link to comment
https://linustechtips.com/topic/52726-cashier-programming-help/#findComment-706803
Share on other sites

Link to post
Share on other sites

Please use code tags. It's the <> button under the :) and it makes the code so much more readable. And could you add a comment pointing out which bit is broken so it's easier to identify.

 

You need the default to exit before doing total+=subt otherwise it reads from an uninitialised variable

I'll keep that code tags in mind.

I have already pointed out the part where the program suddenly ends when I input a decimal with a comment and in bold letters :)

CPU: Intel Core i5-4670k Mobo: MSI Z97 Gaming 3 RAM: Crucial Ballistix Tactical Tracer 8GB 1866 Video Card: MSI GTX 970 GAMING 4G SSDSamsung 840 Evo 250GB HDD: WD 1TB + 500GB Caviar Blue
PSU: EVGA SuperNOVA 750W G2 Case: Gigabyte GZ-G1 Mouse: Corsair M65 RGB Keyboard: Corsair K70 MX Brown MousePad: Corsair MM200
Steam/Origin/Uplay: renz62

Link to comment
https://linustechtips.com/topic/52726-cashier-programming-help/#findComment-706816
Share on other sites

Link to post
Share on other sites

I'll keep that code tags in mind.

I have already pointed out the part where the program suddenly ends when I input a decimal with a comment and in bold letters :)

Oops... But see my edit above, I think I've found your problem

HTTP/2 203

Link to comment
https://linustechtips.com/topic/52726-cashier-programming-help/#findComment-706832
Share on other sites

Link to post
Share on other sites

Oops... But see my edit above, I think I've found your problem

wow! I didn't notice that!

 

thanks a lot for the fast help!!! :D

CPU: Intel Core i5-4670k Mobo: MSI Z97 Gaming 3 RAM: Crucial Ballistix Tactical Tracer 8GB 1866 Video Card: MSI GTX 970 GAMING 4G SSDSamsung 840 Evo 250GB HDD: WD 1TB + 500GB Caviar Blue
PSU: EVGA SuperNOVA 750W G2 Case: Gigabyte GZ-G1 Mouse: Corsair M65 RGB Keyboard: Corsair K70 MX Brown MousePad: Corsair MM200
Steam/Origin/Uplay: renz62

Link to comment
https://linustechtips.com/topic/52726-cashier-programming-help/#findComment-706847
Share on other sites

Link to post
Share on other sites

Just would like to offer a few notes.  Strictly speaking doubles should not be used when keeping accurate decimal points is neccessary.  0.01 in c++ double form actually isn't 0.01 (0.009999999999999787)...Typically speaking when integers are used so reading in 10.01 you read it in as 1001 and then when you output results 1001/10 = dollars and 1001%100 = cents.  Anyways that is just as a reminder, not really a huge deal.

 

The other thing to mention would be your giant if-else statements....

starting from if((whole%1000)==9)

to
    else if(((whole%1000)%100)%10/1==1)
     cout<<"one ";

     cout<<"pesos ";

is very hard to read...here is what I would have done (I didn't compile so it might have a flaw here or there, but easy to follow and should be quicker on average)

char* numberResults[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };char* tensResults[] = { "", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };char* teensResults[] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };int thousandthPlace = (whole/1000);int hundredthPlace = (whole % 1000) / 100;int tenthPlace = (whole % 100)/10;int singlesPlace = whole%10;if(thousandthPlace >= 1 && thousandthPlace <= 9)	cout << numberResults[thousandthPlace] << " thousand ";if(hundredthPlace >= 1)	cout << numberResults[hundredthPlace] << " hundred ";if(tenthPlace >= 2)	cout << numberResults[tenthPlace] << " ";else if(tenthPlace == 1)	cout << teensResults[singlesPlace] << " ";cout << "pesos ";

Didn't bother with the cents portion...but imo doing something like this allows for all the calculations to be done once, and all in one place...this allows easier understanding, less lines of code, less likelyhood of a bug, and less calculations

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/52726-cashier-programming-help/#findComment-708511
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×