Answer to Question #246839 in C++ for Iman

Question #246839

Sample Run 3 

Welcome to CS126 Mart! 

Item Codes 

  Description Price 

100 Sweet ‘n Ripe Grapes  125.35 

101 Crunchy Apples        52.20 

109 Green Peas  25.75 

 

Enter Code: 101 

  Crunchy Apples              52.20 

      Quantity  1     Total Amount Due:  52.20 

  Cash:        50.00  

Insufficient Cash

Sample Run 4 will be

Enter Code: 101 and will buy Crunchy apples (1 quantity) and will pay (Cash: 1000) and the change will be 947.80 and the program will say "Take care!" at the end

1
Expert's answer
2021-10-05T04:57:30-0400
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main(){
    cout<<"Welcome to CS126 Mart!\n";
    int item_codes[3] = {100, 101, 109};
    string description[3] = {"Sweet 'n Ripe Grapes", "Crunchy Apples", "Green Peas"};
    float price[3] = {125.35, 52.20, 25.75};


    cout<<setw(10)<<"Item Codes"<<setw(25)<<left<<"    Description"<<setw(5)<<"Price\n";
    for(int i = 0;  i < 3; i++){
        cout<<setw(10)<<item_codes[i]<<setw(25)<<left<<description[i]<<setw(5)<<price[i]<<endl;
    }
    cout<<"Enter code: ";
    int code; cin>>code;
    int i = 0;
    switch(code){
        case 100: i = 0; break;
        case 101: i = 1; break;
        case 109: i = 2; break;
        default: i = 3; break;
    }
    if(i < 3){
        cout<<setw(25)<<description[i]<<setw(5)<<price[i]<<endl;
        cout<<"Quantity  1     Total Amount Due: "<<price[i]<<endl;
        cout<<"Cash: ";
        float cash; cin>>cash;
        if(cash < price[i]){
            cout<<"Insufficient Cash";
            return -1;
        }
        cout<<"Change: "<<cash - price[i]<<endl;
        cout<<"Take Care!";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS