Thread: Confused about case statements

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    36

    Confused about case statements

    This assignment is turning into a huge mess.Here are my instructions:


    Note: 1) You must use at least one printf and one scanf function
    2) If there are more than two conditions that you have to check for then
    you must use a switch statement
    3) Comments are mandatory.
    4) Proper indentation is mandatory
    5) Your program must include all the features, must work and must output correct results
    Otherwise do not turn it in
    6) You must validate all user input.
    7) No global variables

    Run lab3.exe from my folder for a demo. CAUTION: This is just a prototype. Somethings may not work correctly.

    There is a drive-thru Car Wash that offers different services.
    1) Regular Car Wash
    2) Super Car Wash
    3) Super Dooper Car Wash
    4) Quit

    The fee for each one of these services is different.
    Prompt the user for type of service?
    Ask the driver for his or her age?

    If the user selects Regular Car Wash
    Prompt the user for the number passengers in the car?


    If there are more than 3 passengers
    The price for the carwash is the number of passengers multiplied by the user’s age
    display the price
    display the grand total
    Otherwise
    display as many stars as the number of passengers


    If the user select Super Car Wash

    If the person is a senior citizen( 65 or older)
    price is .50 cents multiplied by all the years in excess of 65
    (Example : 67 years of age would be equal to $1.00)
    otherwise
    if the person’s age is even (e.g 44, 32)
    display “No Charge”
    otherwise
    the price is equal to the person’s age
    display the price
    display the grand total


    If the user selects Super Dooper Car Wash
    Prompt for the number of passengers who are in the car(Must be less than 4?

    If the user enters an invalid number prompt the user again.

    For each passenger ask their age and their gender

    if the passenger is a female
    The price is the age of the passenger multiplied by $1.50
    otherwise
    The price is the age of the driver multiplied by $2.00

    Display total for all the passengers in the car
    Display Grand Total

    At the end, when the user selects quit display the following pattern one character at a time (You would be displaying combinations of spaces and asterisks to come up with pattern, using two loops

    *
    **
    ***
    ****

    Give a grand total for everyone

    Here is my code so far:

    Code:
    #include <cstdio>                                        //Header Files
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        
    int choice, age, passengers, passage, a;
    bool notValid;
    char passgend, f, m;
    double fee, grandtotal=0;
    
    cout << "Welcome to the Fair Price Car Wash\n\n";
    cout << "1)  Regular Car Wash\n"; 
    cout << "2)  Super Car Wash\n"; 
    cout << "3)  SuperDooper Car Wash\n"; 
    cout << "4)  Quit\n\n\n";
    cout << "Select one of the options:";
    cin >> choice;
    
                 do{
                       notValid=false;
                       printf ("\nHow old are you?");
                       cin >> age;
                             if (age<1 || age>115)
                                {
                                 cout << "Invalid entry."; 
                                 notValid=true;
                                }
                   } while (notValid);
              
                 
                 switch (choice)
           {
           case 1:
               
                do{
                       notValid=false;
                       cout << "\nHow many passengers do you have?";
                       cin >> passengers;
                             if (passengers <1 || passengers >10)
                                {
                                 cout << "Invalid entry."; 
                                 notValid=true;
                                }
                   } while (notValid);
                 
                 if (passengers>=3)
                    {
                    grandtotal=grandtotal+fee;
                    fee=passengers*age;
                    cout << "The price for you is        $", fee;
                    
                    
                    }
                 else 
                    {
                        int x = 0;
                        int y = 0;
                        for (int x = 1; x <= passengers; x = x + 1)
                           {    
                          cout<<"\n";
                          for (int y = 1; y != x; y = y + 1)
                          cout << "*";
                           }    
                           }
         case 2:
               grandtotal=grandtotal+fee;
              if (age>=65)
                   fee=(age-65)*.5;
              else if(age% 2 == 0)
                   cout << "No Charge";
              else
                  fee=age;
                  cout << "The fee is  ";
                  cout << fee;
                   cout << "\nThe grand total is  ";
                  cout << grandtotal;
             
             case 3:
                  
                  do{
                       notValid=false;
                       cout << "\nHow many passengers do you have?";
                       cin >> passengers;
                             if (passengers >4)
                                {
                                 cout << "Invalid entry."; 
                                 notValid=true;
                                }
                   } while (notValid);
                     {
                           for (a=0; a>=passengers;) 
                           
                            cout << "Enter gender of passeneger number", a, "f/m): ";
                            cin >> passgend;
                            cout << "Enter the age of passeneger number", a,": ";
                            cin >> passage;
                            a++; 
                            if (passgend==f)
                                {
                                fee=age*1.5;
                                cout << fee;
                                }
                            else 
                                fee=2*age;
                            
                            
         default:
          int x = 0;
          int y = 0;
              for (int x = 1; x <= 4; x = x + 1)
              {    
                cout<<"\n";
                for (int y = 1; y != x; y = y + 1)
                cout << "*";
              }    
            }    
    
            
         
    
    system ("pause");
    }
    
    
    }
    Why is the program falling through the cases? If anyone could give me any help, I would really appreciate it. I think I have been staring at this for too long and am probably making some very stupid mistakes.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by veronicak5678 View Post
    Why is the program falling through the cases?
    Fallthrough is normal behavior. Add a break statement after the code for each case.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Can someone please clean up my code
    By ki113r in forum C Programming
    Replies: 10
    Last Post: 09-12-2007, 10:03 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM