user3845189
user3845189

Reputation:

How to loop back to the start of a switch case statement C++

i'm working on a program for a shop and need to ask the user if they want to buy items more than once. I've looked around but can't find anywhere or any code that helps me loop back to the start.

Here's what i have so far:

cout << "Welcome to Techi Toys!" << endl;


cout << "" << endl;
  cout << "Please select what product you would like to purchase: \n\n\n 1) iPhone \n 2) iPad Air \n 3) Sony Laptop \n 4) Kindle Fire \n 5) Xbox One \n 6) PS4 \n 7) Macbook Air \n 8) iPod Touch \n 9) DSLR Camera \n 10) Polaroid Camera \n" << endl;                                 
  cin >> choice; 

  switch(choice)
  {



case 1:
        cout << "\nYou have chosen iPhone 5S\n" << endl;
        price = 549;
        break;    
case 2:
        cout << "\nYou have chosen iPad Air\n" << endl;
        price = 399;
        break;           
case 3:
        cout << "\nYou have chosen Sony Laptop\n" << endl;
        price = 798;         
        break;    
case 4:
        cout << "\nYou have chosen Kindle Fire\n" << endl;
        price = 120;
        break;        
case 5:
        cout << "\nYou have chosen Xbox One\n" << endl;
        price = 430;
        break;  
case 6:
        cout << "\nYou have chosen PS4\n" << endl;
        price = 350;
        break;  
case 7:
        cout << "\nYou have chosen Macbook Air\n" << endl;
        price = 849;
        break;  
case 8:
        cout << "\nYou have chosen iPod Touch\n" << endl;
        price = 219;
        break;  
case 9:
        cout << "\nYou have chosen DSLR Camera\n" << endl;
        price = 519;
        break;  
case 10:
        cout << "\nYou have chosen Polaroid Camera\n" << endl;
        price = 100;
        break; 


}                   
 //purchasing
 cout << "How many would you like to purchase?\n" << endl;
 cin >> quantity;
 cout << "\nThe total  is " << pound << ""<< price*quantity << endl;

Any help would be greatly appreciated

Upvotes: 0

Views: 1460

Answers (3)

ur.
ur.

Reputation: 2947

Actually you need some means for the user to specify he want's to end his buy now...

So put your switch statement into a loop like this:

bool fTerminate = false;
do {
  // ... other stuff
  switch(choice) {
    case 0:
      fTerminate = true;
      break;

  // ...
  } // end of switch statement

  // purchasing / selecting the number of items ...
  // ...
} while( (!fTerminate) );

// ...

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

Something as

do
{

  cout << "Please select what product you would like to purchase: \n\n\n 1) iPhone \n 2) iPad Air \n 3) Sony Laptop \n 4) Kindle Fire \n 5) Xbox One \n 6) PS4 \n 7) Macbook Air \n 8) iPod Touch \n 9) DSLR Camera \n 10) Polaroid Camera \n" << endl;                                 
  cin >> choice; 

  switch(choice)
  {



case 1:
        cout << "\nYou have chosen iPhone 5S\n" << endl;
        price = 549;
        break;    
case 2:
        cout << "\nYou have chosen iPad Air\n" << endl;
        price = 399;
        break;           
case 3:
        cout << "\nYou have chosen Sony Laptop\n" << endl;
        price = 798;         
        break;    
case 4:
        cout << "\nYou have chosen Kindle Fire\n" << endl;
        price = 120;
        break;        
case 5:
        cout << "\nYou have chosen Xbox One\n" << endl;
        price = 430;
        break;  
case 6:
        cout << "\nYou have chosen PS4\n" << endl;
        price = 350;
        break;  
case 7:
        cout << "\nYou have chosen Macbook Air\n" << endl;
        price = 849;
        break;  
case 8:
        cout << "\nYou have chosen iPod Touch\n" << endl;
        price = 219;
        break;  
case 9:
        cout << "\nYou have chosen DSLR Camera\n" << endl;
        price = 519;
        break;  
case 10:
        cout << "\nYou have chosen Polaroid Camera\n" << endl;
        price = 100;
        break; 


}                   
 //purchasing
 cout << "How many would you like to purchase?\n" << endl;
 cin >> quantity;
 cout << "\nThe total  is " << pound << ""<< price*quantity << endl;

std::cout << "One more (y/n)? ";
} while ( std::cin >> yes_no && yes_no == 'y' );

Upvotes: 1

Charline
Charline

Reputation: 283

If you want to ask the user if he wants to make more choices, you can put a loop around the switch, with the variable wantsMore = true. The loop would be

while (wantsMore)
{
    switch...
    // ask if user wants more, if no set wantsMore = false
}

Upvotes: 4

Related Questions