Thread: Assignment Help Needed, First year uni C programming assignment, any help appreciated

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Exclamation Assignment Help Needed, First year uni C programming assignment, any help appreciated

    I need some help for this task I have to have in soon. I'm writing the code using Quincy and have got all the basis down; but I'm just getting stuck on little fiddly bits which I'm unfamiliar with. EG local and global variables, calling and returning functions, if/else functions, etc.

    Any help is appreciated.

    Thanks

    Task:
    a) Prompt the user to input the vehicle type. ‘c’ or ‘C’ for a car, ‘m’ or ‘M’ for a motorbike, ‘t’ or ‘T’ for a truck. Display a message 'Wrong vehicle type' when the input character is other than specified.

    b) Prompt the user to input the parking time in hours. The parking time is supposed to be a positive integer number greater that 0. Therefore, one hour minimum charge is applied. The program should validate the input time. Display a message 'Wrong parking time when parking time is invalid.

    c) Calculates the amount charged based upon the following rates:
    car - $5p/h, motorbike - $2p/h, truck - $10p/h
    However, if parking time is longer than 4hrs, after that a 25% discount rate is applied ( after 4hrs ). If parking time is longer than 12hrs, after that 50% discount is applied ( to all period after 12hrs ).

    d) Output the parking time and the parking fee


    Code so far:
    Code:
    #include <stdio.h>
    #include <stdbool.h>
    
    #define CAR        5.0
    #define MOTORBIKE    2.0
    #define TRUCK        10.0
    #define DISCOUNT1    0.25
    #define DISCOUNT2    0.50
    #define TIME1        4
    #define TIME2        12
    
    /* function prototypes */
    bool validateVechileType(char type);
    bool validateParkingTime(int hours);
    double getParkingFee(char type, float hours, float fee);
    void displayParkingFee();
    float getParkingRate(float rate);
    
    int main(void)
    {
        char type;
        int hours;
        float fee;
        float rate;
        bool validtype;
        bool validtime;
        
        
        printf("Enter your vehicle type (c, m or t): ");
        scanf("%c", &type );
        
        validtype = validateVehicleType( type);
        if( validtype == true)
        {
            rate = getVehicleRate( type);
            printf("Enter hours: ");
            scanf("%f", &hours );
            validtime = validateParkingTime( hours);
                if( validtime == true)
                {
                fee = getParkingFee( hours, rate);
                displayParkingFee;
                }
                
                else
                {
                printf("Wrong parking time");
                return 0;
                }
        }
        
        else
        {
        printf("Wrong vehicle type");
        }
    
    
        return 0;
    }    
    /* validate the input vehicle type according to predefined values*/
    bool validateVechileType(char type)
    {
        if (type == 'C' || 'c' || 'M' || 'm' || 'T' || 't')
            return (true);
        else
            return (false);
    }
    
    /*assign apropriate value for rate*/
    float getVehicleRate(char type)
        if (type == 'C' || 'c')      
            {
            rate = CAR;
            }
            
        if (type == 'T' || 't')      
            {
            rate = TRUCK;
            }
            
        if (type == 'M' || 'm')
            {
            rate = MOTORBIKE;
    
        return (rate);
    }
            
    /* validate the input parking time according to predefined values*/
    bool validateParkingTime(int hours);
    {
        if (hours >> 0 && hours%1 == 0)
            return (true);
        else
            return (false);
    }              
    
    /*calculate and return the parking fee*/
    float getParkingFee(int hours, float rate);
    {
        if (hours <= TIME1)    
            {
            fee = rate * hours;
            }
        
        if (TIME1 < hours && hours <= TIME2)
            {
              fee = (rate * 4) + (rate * (hours - 4));
               }
            
        if (TIME2 < hours)
            {
            fee = (rate * 4) + (rate * DISCOUNT1 * 8) + (rate * DISCOUNT2 * (hours - 12));
            }
            
        return (fee);
    }
    
    /*display the calculated parking fee*/
    displayParkingFee(char type, int hours, float fee );
    {
    if (type == 'C' || 'c')
        printf("Car %d hrs parking fee: %.2f", hours, fee);
    if (type == 'M' || 'm')
        printf("Motorbike %d hrs parking fee: %.2f", hours, fee);
    if (type == 'T' || 't')
        printf("Truck %d hrs parking fee: %.2f", hours, fee);
        
        return();
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Still working on this same problem weeks later, eh?

    have got all the basis down; but I'm just getting stuck on little fiddly bits which I'm unfamiliar with. EG local and global variables, calling and returning functions, if/else functions, etc.
    HELLO!!! Those *are* the basics! Stay away from the keggers and go to class.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Thumbs down

    Quote Originally Posted by rags_to_riches View Post
    Still working on this same problem weeks later, eh?


    HELLO!!! Those *are* the basics! Stay away from the keggers and go to class.
    For your information have been to ALL lectures and tutes... and haven't bothered going to a party in over a year.

    Was mainly looking for suggestions on how to structure my code better than it already was.
    Never mind, it's done now anyway.

    P.S. Thanks for your help rags_to_riches, not.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by MadGolfer
    Was mainly looking for suggestions on how to structure my code better than it already was.
    Ah, then you should have said so
    Being vague tends to make people think that you want them to improve your homework for you.

    For the most part, it looks like you did well in breaking up your code into well named functions. I have a few comments though...

    This:
    Code:
    if( validtype == true)
    would be better written as:
    Code:
    if( validtype)
    But the only reason that validtype exists is to store the return value of the call to validateVehicleType(). As such, you would be even better off doing without this variable:
    Code:
    if (validateVehicleType(type))
    (Thus, isValidVehicleType might be an even better name for the function.) The same idea applies to getting rid of the validtime variable.

    Now, this line does not quite do what you probably want it to do:
    Code:
    if (type == 'C' || 'c' || 'M' || 'm' || 'T' || 't')
    You need to compare type with each of the char literals, e.g.,
    Code:
    if (type == 'C' || type == 'c' || /* ... */)
    But why use an if statement? You can return directly:
    Code:
    return type == 'C' || type == 'c' || /* ... */;
    You need to make a similiar change (but not to a single return statement, of course) in getVehicleRate() and displayParkingFee().

    Note that you declared displayParkingFee() as returning void, but when you defined it, you forgot about the return type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Desperate help needed. C++ Uni Assignment.
    By ILLaViTaR in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2009, 01:30 PM
  2. Determining the Date of the [nth] [Day] in [Month]
    By Dave_Sinkula in forum C Programming
    Replies: 5
    Last Post: 05-25-2007, 04:12 PM
  3. Replies: 2
    Last Post: 05-27-2005, 05:05 PM
  4. Assignment operator
    By Claire in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2002, 04:08 AM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM