[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 841
  • Last Modified:

How to restart a loop automatically

If you view my code below My question pertains to function called valuemeal1()

within the code I have multiple loops each checking if the proper values have been entered

my question is if for example someone is asked if they want fries   and the answer is neither yes or no
they type  'yet' or make a typo  how can I have the program loop back..currently I have it say that is not a valid answer but I also want it to then re ask if they want fries or not..

I also have another qustion not sure if I should post a new one but i'll ask anyway..

How can I have my answer not case sensitive...I want the user to be able to enter   Yes or yes for an answer

//Jeffrey Calandra
//2/11/08


#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <string>

using namespace std;


void menu(void)
{
     cout <<"Enter Menu Item " << endl;
     cout <<"1 = Double Quarter Pounder w/Cheese Value Meal" <<endl;
     cout <<"2 = Hoagie" <<endl;
     cout <<"3 = Soda" <<endl;
     cout <<"4 = Fries" <<endl;
     cout <<"5 = Chips" <<endl;
     cout <<endl;
     cout <<"0 = quit" <<endl;
     cout <<endl;
}

void valuemeal1(void)
{
     float qty;
     string y = "yes";
     string n = "no";
     string s = "Small";
     string m = "Medium";
     string l = "Large";
     string fries;
     string supersize;
     string soda;
     cout <<"Enter the quanity" <<endl;
     cout <<endl;
     cin  >>qty;
     cout <<endl;
     cout <<"Would you like fries with that? (yes or no): ";
     cin  >>fries;
     if(fries == y)
     {
              cout <<endl;
              cout <<"Super Size the Fries ? (yes or no): ";
              cin  >>supersize;
              if(supersize == y)
              {
                           cout <<endl;
                           cout <<"What Size Soda ? (Small,Medium,Large): ";                          
                           cin  >>soda;
                           if((soda == s) || (soda == m) || (soda == l))
                           {
                                    cout <<endl;
                                    cout <<"You have ordered" <<endl;
                                    cout <<qty <<" Double Quarter Pounder(s) w/Cheese" <<endl;
                                    cout <<"Super Size Fries" <<endl;
                                    cout <<soda <<" soda" <<endl;
                                    cout <<endl;
                                    cout <<"Thank you for your purchase" <<endl;
                                    cout <<endl;
                           }
                           else
                           {
                               cout <<endl;
                               cout <<"That is not a valid soda size" <<endl;
                               cout <<endl;
                           }
              }
              else
              {
                  if(supersize != n)
                  {
                               cout <<endl;
                               cout <<"That is not a valid Supersize Option" <<endl;
                               cout <<endl;
                  }
                  if(supersize == n)
                  {
                  cout <<endl;
                  cout <<"What Size Soda ? (Small,Medium,Large): ";                          
                  cin  >>soda;
                  if((soda == s) || (soda == m) || (soda == l))
                  {
                           cout <<endl;
                           cout <<"You have ordered" <<endl;
                           cout <<"Double Quarter Pounder w/Cheese" <<endl;
                           cout <<"Regular Fries" <<endl;
                           cout <<soda <<" soda" <<endl;
                           cout <<endl;
                           cout <<"Thank you for your purchase" <<endl;
                           cout <<endl;
                           }
                           else
                           {
                               cout <<endl;
                               cout <<"That is not a valid soda size" <<endl;
                               cout <<endl;
                           }
                  }
                  }
     }
     else
     {
         if(fries != n)
         {
                  cout <<endl;
                  cout <<"That is not a valid fries option" <<endl;
                  cout <<endl;
                 
         }
         if(fries == n)
         {
         cout <<endl;
         cout <<"What Size Soda ? (Small,Medium,Large) : ";
         cin  >>soda;
         if((soda == s) || (soda == m) || (soda == l))
         {
                  cout <<endl;
                  cout <<"You have ordered" <<endl;
                  cout <<"Double Quarter Pounder w/Cheese" <<endl;
                  cout <<"No Fries" <<endl;
                  cout <<soda <<" soda" <<endl;
                  cout <<endl;
                  cout <<"Thank you for your purchase" <<endl;
                  cout <<endl;
         }
         else
         {
             cout <<endl;
             cout <<"That is not a vaild soda size" <<endl;
             cout <<endl;
         }
         }
     }                                  
                           
     cout <<endl;
     system("pause");
}

void hoagie(void)
{
     char b;
     cout <<"Enter Size of Hoagie " <<endl;
     cout <<"(L) for Large or (S) for small ";
     cin  >>b;
     if((b == 'L') || (b == 'S'))
     {
           cout <<endl;
           cout <<"The size of hoagie you ordered = : " <<b <<endl;
           cout <<endl;
         
     }
     else
     {    
     
           cout <<endl;
           cout <<"YOU DID NOT ENTER A VALID SIZE";
           cout <<endl;
     }
}

void soda(void)
{
     char c;
     cout <<"Enter the Size of Soda " <<endl;
     cout <<"(L) for Large" <<endl;
     cout <<"(M) for Medium" <<endl;
     cout <<"(S) for Small" <<endl;
     cout <<endl;
     cin  >>c;
     if((c == 'L') || (c == 'M') || (c == 'S'))
     {
           cout <<endl;
           cout <<"The size of soda you ordered = : " <<c <<endl;
           cout <<endl;
     }
     else
     {
         cout <<"YOU DID NOT ENTER A VALID SIZE";
         cout <<endl;
     }
}

void fries(void)
{
     float d;
     cout <<"Enter the amount of Fries " <<endl;
     cin  >>d;
     cout <<endl;
     cout <<"You purchase " <<d <<" fries " <<endl;
     cout <<endl;
}

void chips(void)
{
     string e = "Sour Cream";
     string f = "Ranch";
     string g = "Regular";
     string type;
     
     cout <<"Enter the type of Chips " <<endl;
     cout <<"Sour Cream " <<endl;
     cout <<"Ranch " <<endl;
     cout <<"Regular " <<endl;
     cout <<endl;
     cin  >>type;
     if((type == e) || (type == f) || (type ==g))
     {
             
           cout <<endl;
           cout <<"Your purchased " <<type <<" chips " <<endl;
           cout <<endl;
     }
           
 
     else
     {
         cout <<endl;
         cout <<"YOU DID NOT ENTER A VALID TYPE" <<endl;
         cout <<endl;
     }
}


int main()
{
   
    int a;  
    do
    {
        cout <<endl;
        menu();
        cin >> a;
        cout <<endl;
                         
                     switch(a)
                     {
                                      case 1: valuemeal1();
                                      break;
                                      case 2: hoagie();
                                      break;
                                      case 3: soda();
                                      break;
                                      case 4: fries();
                                      break;
                                      case 5: chips();
                                      break;
                     
                                     
                     }
    }while (a!= 0);  
       
return 0;
}


Thanks
0
WestonGroup
Asked:
WestonGroup
  • 2
1 Solution
 
jkrCommented:
You could just make that
fries = "";
 
while (fries != y && fries != n)
{
     cout <<"Would you like fries with that? (yes or no): ";
     cin  >>fries;
}

Open in new window

0
 
itsmeandnobodyelseCommented:
You best put all user iput into a separate function like you did for 'soda'. Then loop in the function until you have valid input (or quit)

bool soda(char& c )
{
     c = ' ';
     while (true)
     {
        cout <<"Enter the Size of Soda " <<endl;
        cout <<"(L) for Large" <<endl;
        cout <<"(M) for Medium" <<endl;
        cout <<"(S) for Small" <<endl;
        cout <<endl;
        cin  >>c;
        if((c == 'L') || (c == 'M') || (c == 'S'))
        {
            cout <<endl;
            cout <<"The size of soda you ordered = : " <<c <<endl;
            cout <<endl;
            return true;
        }
        else if (c == 'Q' || c == 'q')
            return false;
        else
        {
           cout <<"YOU DID NOT ENTER A VALID SIZE";
           cout <<endl;
        }
     }
}

The loop would continue until valid input is given.

Note, in case of 'quit' the function would return false.

Regards, Alex

0
 
Infinity08Commented:
>> How can I have my answer not case sensitive...I want the user to be able to enter   Yes or yes for an answer

You can by default transform all input to uppercase (or lowercase), and then check.
0
 
itsmeandnobodyelseCommented:
>>>> You can by default transform all input to uppercase (or lowercase), and then check.

e. g.

    cin >> c;
    c = toupper(c);
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now