Chars/String help

This is a discussion on Chars/String help within the C++ Programming forums, part of the General Programming Boards category; Hello.... I'm a little confused on how the char system works. As you can see, I started coding what I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    7

    Chars/String help

    Hello.... I'm a little confused on how the char system works. As you can see, I started coding what I wanted to do. I'm just confused on classes like Car and Truck and how to apply them to my code. I'm pretty good when I get down to the IF statements now... however, I still can't get a grasp on the char function yet. I basically want the user to enter Car or Truck... or even 'C', 'c', 'T', 't' obviously the capital and little C equaling Car and the capital T and little t equaling Truck. Anyone know what I'm trying to say... I'm even confusing myself.

    ---------------------------------------------------------------------
    Problem: Determine the cost of a single customer to park in the parking lot. Trucks are charged at a rate of $3/hour and cars are charged at a rate of $1.75/hr. Trucks get 1 hour of free parking and cars get 2 hours of free parking. Input the number of minutes that the vehicle parked and determine the cost. If a vehicle parks for part of an hour, we will charge them for the entire hour. Display the vehicle type, hours parked, and the fee to park in the lot.



    Code:
      /**********************************************************************
    *Program Name   :    Parking Fee Receipt
    *Program Description: Write a program that will determine how much a car or truck will have to pay in the  parking lot.
    *
    *BEGIN - Parking Fee Receipt
    *   Input Vehicle Type
    *   Clear the screen
    *   If(Vehicle Type Is Valid)
    *   Input Number Of Minutes Parked
    *   Convert Minutes Parked To Hours Parked
    *   If(Vehicle Type Is A Truck)
    *       Calculate Cost For Truck
    *       Vehicle Type = "Truck"
    *   Else Vehicle Is A Car
    *       Calculate Cost For Car
    *       Vehicle Type = "Car"
    *   End If
    *   Display Receipt  
    *   Else // Vehicle Type Is Invalid
    *   Display Error Message
    *   End If        
    *END - Parking Fee Receipt
    **********************************************************************/
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
       
        //local constants
       
        const int TRUCK_RATE = 3;
        float CAR_RATE = 1.75;
        const int FREE_TRUCK = 1;
        const int FREE_CAR = 2;
    
        //local variables
       
        int Num_Min;
        char Veh_Type;
       
        /************************start main program************************/
       
        // Input Vehicle Type
        cout << "Car or Truck? : ";
        cin >> Veh_Type;
       
        // If The Vehicle Is Valid
        If( Veh_Type == 'C'
       
        //Clear the screen
            system("cls");

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Apart from not including your close parenthesis, what's wrong with it?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    Here's an update... still not getting it

    Code:
    /**********************************************************************
    *Program Name   :    Parking Fee Receipt 
    *Program Description: Write a program that will determine how much a car
    *or truck will have to pay in the OnCenter parking lot.
    *
    *BEGIN - Parking Fee Receipt
    *   Input Vehicle Type
    *   Clear the screen
    *   If(Vehicle Type Is Valid)
    *   Input Number Of Minutes Parked
    *   Convert Minutes Parked To Hours Parked
    *   If(Vehicle Type Is A Truck)
    *       Calculate Cost For Truck
    *       Vehicle Type = "Truck"
    *   Else Vehicle Is A Car
    *       Calculate Cost For Car
    *       Vehicle Type = "Car"
    *   End If
    *   Display Receipt  
    *   Else // Vehicle Type Is Invalid
    *   Display Error Message
    *   End If        
    *END - Parking Fee Receipt
    **********************************************************************/
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        
        //local constants
        
        const int TRUCK_RATE = 3;
        const int FREE_TRUCK = 1;
        const int FREE_CAR = 2;
        const int NUM_HRS = 24;
        const int MIN_NUM = 60;
        const int TOT_MIN = 1440;
        float CAR_RATE = 1.75;
        
    
        //local variables
        
        int Num_Min;
        int Park_Fee;
        char Veh_Type;
        
       
        
        /************************start main program************************/
        
        // Input Vehicle Type
        cout << "Car (Capital 'C') or Truck (Capital 'D')? : ";
        cin >> Veh_Type;
        
         //Clear the screen
            system("cls");
        
        // If The Vehicle Is Valid Input Number Of Minutes Parked
    
        if( Veh_Type == 'C' || Veh_Type == 'D' )
    {
        cout << "Enter Number Of Minutes Parked :           ";
        cin >> Num_Min;
    }
    
        // Convert Minutes To Hours Parked
        
        if( TOT_MIN % MIN_NUM )
          NUM_HRS = Num_Min / MIN_NUM;
          NUM_HRS ++;
    
        
        // If Vehicle Is A Truck
        
        if(Veh_Type == 'T')
    {
          (NUM_HRS - FREE_TRUCK) * TRUCK_RATE;
          cout << Veh_Type == "Truck";
    }
        else 
        Veh_Type = 'C';
        (NUM_HRS - FREE_CAR) * CAR_RATE
         cout << Veh_Type == "Car";
         
        // END IF
    
    
        // Display Receipt
        
           cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);
            cout << "\n\n\n\n";
            cout << setw(56) << "Parking Receipt     " << "\n\n";
            cout << setw(43) << "Category            " << setw(9) << "Result"   << "\n\n";
            cout << setw(43) << "Hours Parked        " << setw(9) << NUM_HRS    << "\n\n";
            	 << setw(43) << "Vehicle Type        " << setw(9) << Veh_Type	<< "\n\n";
            	 << setw(43) << "Fee                 " << setw(9) << Park_Fee   << "\n\n";
                 << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
        
       
            //pause the output screen for viewing 
        system("pause");
            
        return 0;
        
    } // end if main function

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should decide do you want 'T' or 'D' for the Track and be consistenty about it

    to allow low chars use toUpper after you get the char from user
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    I can see four major problems here:

    #1: You have set NUM_HRS as a "const int", which means it is "constant", which means it can NEVER change its value. You DO change it when you convert the minutes to hours.

    #2: Your final else statement doesn't have any braces. Without these, it only performs the first line after it, which I think isn't what you want.

    #3:
    Code:
    (NUM_HRS - FREE_TRUCK) * TRUCK_RATE;
    does not assign to anything. In otherwords, you need to have an equals sign somewhere.

    #4:
    Code:
    cout << Veh_Type == "Truck";
    This will probably not do what you think. This will output either a 1 or 0 to the screen. What it is saying is "Is Veh_Type a "Truck"", and it will respond with either yes (1) or no (0).

    #5: Veh_Type is a char type, meaning that it holds only ONE character. "Truck" is, at least, 5. Change Veh_Type to a std::string or char array.

    Here's a rough overview of the differences between strings and chars:
    Strings are WORDS or SENTENCES that can be of any length. Chars are of only one character, for example, 'A' or '6' or '%' are all a char. Chars aren't super useful, but you can have many chars in an array. An array can look like this:

    Code:
    char TESTWORD[20];
    This is pretty much the same as a string, except it is limited to only 20 characters.

    There are a couple of other little things... but that should be a big enough load for now.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    7
    I think I fixed the major problems. What else do I have on my plate?

    Code:
    /**********************************************************************
    *Program Name   :    Parking Fee Receipt 
    *Program Description: Write a program that will determine how much a car
    *or truck will have to pay in the OnCenter parking lot.
    *
    *BEGIN - Parking Fee Receipt
    *   Input Vehicle Type
    *   Clear the screen
    *   If(Vehicle Type Is Valid)
    *   Input Number Of Minutes Parked
    *   Convert Minutes Parked To Hours Parked
    *   If(Vehicle Type Is A Truck)
    *       Calculate Cost For Truck
    *       Vehicle Type = "Truck"
    *   Else Vehicle Is A Car
    *       Calculate Cost For Car
    *       Vehicle Type = "Car"
    *   End If
    *   Display Receipt  
    *   Else // Vehicle Type Is Invalid
    *   Display Error Message
    *   End If        
    *END - Parking Fee Receipt
    **********************************************************************/
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        
        //local constants
        
        const int TRUCK_RATE = 3;
        const int FREE_TRUCK = 1;
        const int FREE_CAR = 2; 
        const int MIN_NUM = 60;
        const int TOT_MIN = 1440;
        float CAR_RATE = 1.75;
        char Veh_Type[5];
        char Veh_Type[3];
        
    
        //local variables
        
        int Num_Min;
        int Park_Fee;
        int Num_Hrs;
        
         /************************start main program************************/
        
        // Input Vehicle Type
        cout << "Enter either Car (Capital 'C') or Truck (Capital 'D')? : ";
        cin >> Veh_Type;
        
         //Clear the screen
            system("cls");
        
        // If The Vehicle Is Valid Input Number Of Minutes Parked
    
        if( Veh_Type[3] == 'C' || Veh_Type[5] == 'D' )
    {
        cout << "Enter Number Of Minutes Parked :           ";
        cin >> Num_Min;
    }
    
        // Convert Minutes To Hours Parked
        
        if( TOT_MIN % MIN_NUM )
          Num_Hrs = Num_Min / MIN_NUM;
          Num_Hrs ++;
    
        
        // If Vehicle Is A Truck
        
        if(Veh_Type == 'T')
    {
          (Num_Hrs - FREE_TRUCK) * TRUCK_RATE = Park_Fee;
           
    }
        else 
    {
        Veh_Type = 'C';
        (NUM_HRS - FREE_CAR) * CAR_RATE = Park_Fee;
    }
         
        // END IF
    
    
        // Display Receipt
        
           cout << setiosflags(ios::fixed | ios::showpoint) << setprecision(2);
            cout << "\n\n\n\n";
            cout << setw(56) << "Parking Receipt     " << "\n\n";
            cout << setw(43) << "Category            " << setw(9) << "Result"   << "\n\n";
            cout << setw(43) << "Hours Parked        " << setw(9) << Num_Hrs    << "\n\n";
            	 << setw(43) << "Vehicle Type        " << setw(9) << Veh_Type	<< "\n\n";
            	 << setw(43) << "Fee                 " << setw(9) << Park_Fee   << "\n\n";
                 << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
        
       
            //pause the output screen for viewing 
        system("pause");
            
        return 0;
        
    } // end if main function

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if( Veh_Type[3] == 'C' || Veh_Type[5] == 'D' )
    if(Veh_Type == 'T')
    Still not sure what you want, I see...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    You don't nedd to declare arrays since you only need single characters.
    Code:
    char Veh_Type[5];
        char Veh_Type[3];
        
    //What for?

    You must first ask the user for the number of minutes used without any if statement
    You want the number of minutes irrespective of being a car or truck
    Code:
    cout << "Enter Number Of Minutes Parked :           ";
    cin >> Num_Min;
    // and then ask the user whether its a car or truck
    // then calculate the number of hours
    //then the parking fee
    Probably this block will never get executed

    Code:
    if( TOT_MIN % MIN_NUM )
          Num_Hrs = Num_Min / MIN_NUM;
          Num_Hrs ++;
    
    //const int MIN_NUM = 60;
    //    const int TOT_MIN = 1440;
    / Now  your if ( 1440 % 60 ) which is equal to 0 or false
    //Probably you were attempting to find out the nnumber of hours
    //Num_Hrs = (int)Num_Min / 60
    //Now if you have parked for 119 minutes or 100 minutes
    //the above statement will give you Num_Hrs=1
    //You have a choice of passing the benefit to your customer
    Now
    Code:
    if(Veh_Type == 'T')
    {
          (Num_Hrs - FREE_TRUCK) * TRUCK_RATE = Park_Fee;
          //should be
          //  Park_Fee=  (Num_Hrs - FREE_TRUCK) * TRUCK_RATE ;
    
    }
        else //should be else if( Veh_Type == 'C')
    {
       
        (NUM_HRS - FREE_CAR) * CAR_RATE = Park_Fee;
       //should be  Park_Fee=   (NUM_HRS - FREE_CAR) * CAR_RATE 
    }
    //Now what if you enter a letter other than C or  T
    //You still display the parking fee
    //One thing you can do after the above 'else-if statement is'
    //add another else statement
    else
    {
     cout << "Invalid Input" ;
      return 0;
    }

  9. #9
    nhm
    nhm is offline
    Registered User
    Join Date
    Feb 2011
    Posts
    1
    please give me full c++ coding for parking. i really need that which only use #include <stdio.h> only.

  10. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    455
    Quote Originally Posted by nhm View Post
    please give me full c++ coding for parking. i really need that which only use #include <stdio.h> only.
    hhmmm.... so you pumped an old thread to ask someone to do your hw/exam/whatever for you with constrains that show you know absolutely nothing about the language. Good luck with that.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21