c++ - I dont know why i cant loop the whole code? when i press y on "next customer" -
i want loop whole codes if press y on "next customer"? , reset program first try. type 0101 , 0307 on itemcode put of codes. define gotoxy running while on "next customer " think error?
#include<iostream.h> #include<conio.h> #include<stdio.h> #define g gotoxy main() { char back_, back; float change, p0101 = 25.5, p0307=11,subtotal=0,total=0; int y=9, quantity, itemcode, cash,discount; clrscr(); do{ do{ g(1,8); cout<<"itemcode"; g(15,8); cout<<"quantity"; g(41,8); cout<<"price"; g(51,8); cout<<"subtotal"; g(62,8); cout<<"total"; g(1,y); cin>>itemcode; g(15,y); cin>>quantity; { g(25,y); if (itemcode == 0101){ cout<<"yogurt(12 oz)"; subtotal = quantity * p0101; } else if (itemcode == 0307){ cout<<"pumpkin (1000g)"; subtotal = quantity * p0307; } } { g(41,y); if(itemcode == 0101){ cout<<"25.50"; } else if (itemcode == 0307){ cout<<"79.75"; } } g(51,y); cout<<subtotal; total=total+subtotal; g(62,y); cout<<total; subtotal=0; g(72,8); cout<<"add item?"; g(72,y); cin>>back; y++; if(back=='n'||back=='n'){ { g(1,y+2); cout<<"cash: "; cin>>cash; } if(cash>total){ { g(1,y+3); cout<<"[r]-regular\t[d] - discount :"; } cin>>discount; g(1,y+4); if(discount=='d'||discount=='d'){ (change=cash-(.8*total)); cout<<"change: "<<change; } else{ change=cash-total; cout<<"change: "<<change; } } else if(cash<total){ cout<<"insufficient amount"; } { g(18,y+8); cout<<"exchange of item type , size allowed,"; } { g(18,y+9); cout<<"subject standard provisions , product warranty"; } { g(18,y+10); cout<<" please present receipt"; } { g(15,y+12); cout<<"next customer?"; cin>>back; } } } while(back_=='y'||back_=='y'); } while(back=='y'||back=='y'); enter code here getchar(); return 0; }
i cannot see input back_
have cin>>back;
twice. error? control structures hard follow.
you have, essentially,
do { { // input stuff item cout<<"add item?"; cin>>back; if(back == 'n' || == 'n') { // code finalize transaction cout<<"next customer?"; cin>>back; } } while (back_=='y' || back_=='y') } while (back=='y' || == 'y')
you defined back_
never initialized or used in code when while (back_=='y' || back_=='y')
comes along, when press 'y', loop never loops.
if change code to:
do { { // input stuff item cout<<"add item?"; cin>>back; if(back == 'n' || == 'n') { // code finalize transaction cout<<"next customer?"; cin>>back_; } } while (back_=='y' || back_=='y') } while (back=='y' || == 'y')
the program loop again if pressed 'y' interesting behavior when pressed 'n' "add item?"
, 'y' "next customer?"
. predict program close prematurely. better have:
char new_customer, add_item { //---this outer loop---// { //---this inner loop---// // input stuff item cout<<"add item?"; cin>>add_item; if(add_item == 'n' || add_item == 'n') { // code finalize transaction cout<<"next customer?"; cin>>new_customer; } //---end of inner loop---// } while (add_item=='y' || add_item=='y') //---end of outer loop---// } while (new_customer=='y' || new_customer=='y')
this way inner loop breaks when press 'n' @ "add item?"
, outer loop breaks when press 'n' @ "next customer?"
, ending program. pressing 'y' @ either continue respective loops.
other odd behavior can come from
- pressing other 'y' or 'n' @ decision points
- inputting other numbers variables used calculate prices , such
- not formatting output price fields
$
or having 2 decimal places money
the first 2 can fixed reading in strings , parsing out character character check valid input , converting appropriate strings numbers using built-in c++ functions. last 1 can fixed little fancy calculations on data %
(modulus) operator see if values end 0 or not. ideas.
looks works, except 1 major error.
Comments
Post a Comment