Thread: Yes/No in a loop

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    10

    Yes/No in a loop

    Here is part of my program that im having issues with. I know it still needs work and im not done, but on my loop, how can i get it to stop with a "n" or "N" selection? should i get rid of the for loop completely?
    Code:
    int k;
         int choice;
         char selection;
         
         cout <<"You can make up to 8 single order selections"<<endl;
         cout <<"Do you want to make a selection Y/y(Yes), N/n(No):";
         cin >> selection;
         
         if(selection == 'y'||selection == 'Y')
         {
            for(k=0;k<8;k++){
            cout<<"Enter item number: ";
            cin >> choice;
            cout<<"Select another item Y/y(Yes), N/n(No): ";
            cin >>selection;
         
         switch(choice)
         {
              case 1: m.menuitem[k]="Plain Egg";
                   m.menuprice[k]=1.45;
                   break;
              case 2: m.menuitem[k]="Bacon and Egg";
                   m.menuprice[k]=2.45;
                   break;
              case 3: m.menuitem[k]="Muffin";
                   m.menuprice[k]=.99;
                   break;
              case 4: m.menuitem[k]="French Toast";
                   m.menuprice[k]=1.99;
                   break;
              case 5: m.menuitem[k]="Fruit Basket";
                   m.menuprice[k]=2.49;
                   break;
              case 6: m.menuitem[k]="Cereal";
                   m.menuprice[k]=.69;
                   break;
              case 7: m.menuitem[k]="Coffee";
                   m.menuprice[k]=.50;
                   break;
              case 8: m.menuitem[k]="Tea";
                   m.menuprice[k]=.75;
                   break;
         }
        }}
        else if (selection=='N'||selection=='n')
        cout<<"OK. Good bye."<<endl;
        else 
        cout<<"Invalid selection"<<endl;

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    im relatively new to programming ,but why not put an if statement within the loop?
    Last edited by africanwizz; 11-26-2012 at 07:51 PM.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Ideally you'd put the condition (choice is not y) as part of the loop body.

    If you find that the check needs to happen in the middle of the loop, you can break when the condition is met. Alternatively you could set a flag, and skip the rest of the loop. Then ad the flag variable to the condition. There is no consensus on which of these methods is better, so use whatever seems more clear to you, or your instructor.

    Also, indentation should follow curly brases. If you open a curtly brace, increase the indent. If you close one, decrease the it. It makes your code easier to read and saves you from errors.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 01:19 AM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM