c++ - How to total all inputted item code (with corresponding value) using loop and array? -


to programmers enthusiast , professionals out there! new @ c++ programming. want ask how going approach task in program "purchasing item". user inputting item code , item code has corresponding value , inputting quantity of item. program ask "would purchased item" if user said yes program loop , need add previous value user inputted. problem how going loop , total value of item user inputted. dont use fstream.h

p104 , p103 item code

#include<iostream.h> #include<stdio.h> #include<math.h> #include<conio.h> main() { clrscr(); int p104 = 25, order, total = 0, subtotal, p103 = 22,itemcode, quantity, back; do{ cout<<"enter item code: "; cin>>itemcode; cout<<"\nenter number of quantity: "; cin>>quantity; cout<<subtotal; total=total+subtotal; cout<<"\n"<<total; cout<<"\nwould purchase other item? [y]-yes?"; cin>>back;   } while(back=='y'||back=='y'); getch(); return 0; } 

use getchar() after cin>>back_; taking newline character , calculate subtotal based on condition.

#include<iostream> #include<stdio.h> #include<math.h> #include<conio.h> using namespace std; main() { //clrscr(); char back_; int p104 = 25, order, total = 0, subtotal =0, p103 = 22,itemcode, quantity; do{ cout<<"enter item code: "; cin>>itemcode; cout<<"\nenter number of quantity: "; cin>>quantity; //cout<<subtotal; if (itemcode == 104){     subtotal = quantity * p104; }else if (itemcode == 103){     subtotal = quantity * p103; } total=total+subtotal; subtotal =0; cout<<"\n"<<total; cout<<"\nwould purchase other item? [y]-yes?"; cin>>back_; getchar();   } while(back_=='y'||back_=='y'); return 0; } 

Comments