Question: I need the following problems corrected in this code: * When selecting option 20, it returns the information from case 2 . I need option
I need the following problems corrected in this code:
* When selecting option 20, it returns the information from case 2 . I need option 20 to be the output instead.
* How can I correct the Name portion so that it allows for a last name (if the user chooses to add it) without the last name returning as the address
* The options need to NOT be case sensitive
* Needs to make sure there's error checking after any choices made like (cout<<"Invalid choice!") and then allow the user to try again.
#include using namespace std; int main() { float cart=0.00; //for the cart total char choice; //user's choices string name, address; //user name and address label1: cout<<" A. Learn about fruits"; //label1: main menu cout<<" B. Buy fruits "; cin>>choice; //getting choice of user if(choice == 'a') { cout<<" Choose: 1. Apple 2. Banana 20. Berry "; cin>>choice; switch(choice) //cases depending on user choice { case '1': cout<<"Apple is a red, green fruit that keeps the doctor away"; break; case '2': cout<<"Banana is a yellow fruit rich in potassium"; break; case '20': cout<<"Berry is small tangy fruit"; break; default: cout<<"Invalid choice!"; break; } cout<<" Enter any key to return to previous menu: "; cin>>choice; //any key input is accepted goto label1; //goto main menu } else if(choice == 'b') { cout<<" Buy from these fruits: "; label2: cout<<"a. tamarind $1.50 b. kiwi $2.00 c. acai $2.50 "; //label2: buying menu cin>>choice; switch(choice) //cases of fruit to buy { case 'a': cart += 1.50; //adding the cost to cart break; case 'b': cart += 2.00; //adding the cost to cart break; case 'c': cart += 2.50; //adding the cost to cart break; default: cout<<"invalid choice!"; break; } cout<<" a. Add more to cart b. continue with payment c. cancel"; cout<<" r. Return to main menu "; cin>>choice; if(choice == 'a') goto label2; //goto Buying menu else if(choice == 'b') { //Display purchase details cout<<" Purchase details: "; cout<<"Your total is $"< cout<<" Enter your name: "; cin>>name; cout<<"Enter your address: "; cin.ignore(1); getline(cin, address); } else if(choice == 'c') { cout<<" Purchase cancelled!"; } else if(choice == 'r') goto label1; //goto main menu
cout <<" Alright, " < cout<<" Thankyou, come again"; } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
