Thread: Basic C Programming help!

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    10

    Basic C Programming help!

    Hello, I just started to learn the C language.

    I want to make a simple rental program that requests a user to input the time and day, and displays the duration that something was rented out for.. like a program for a library, or a car company.

    Really confused on how to save/display time and date... do I use time.h? Thanks in advance


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int main(void)
    {
        char rentdate, returned;
        char rentTime, returnedTime;
        int totalTime;
        int cost;
        
        printf("Please enter the date the bike is rented out.(MM/DD/YY):\n");
        scanf("%s", &rentdate);
        printf("Please enter the time the bike is rented out.(HH:MM):\n");
        scanf("%s", &rentTime);
        printf("Please enter the date the bike was returned.(MM/DD/YY):\n");
        scanf("%s", &returned);
        printf("Please enter the time the bike is returned out (HH:MM):\n");
        scanf("%s", &returnedTime);
        
        totalTime = 
        getch();
    }
    Last edited by convex; 02-14-2014 at 08:33 PM.

  2. #2
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    I wanted to edit my post but I can't..

    I've changed my code up a little bit since I posted, but I am still really stuck on figuring out the time/date issue, it's making everything worse for the math part of things! I do not know what else to do..Please help.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int main(void)
    {
        int rentyear, rentmonth, rentday, hour, min;
        int returnyear, returnmonth, returnday, returnhour, returnmin;
        int totalTimeHour, totalTimeMin;
        int cost, costTotal;
        
        printf("Please enter the date and time the bike is rented out\n");
        printf("Year (YYYY): ");
        scanf("%d", &rentyear);
        printf("Month (1 to 12): ");
        scanf("%d", &rentmonth);
        printf("Day (1 to 31): ");
        scanf("%d", &rentday);
        printf("Hour (0 to 23): ");
        scanf("%d", &hour);
        printf("Minutes (0 to 59): ");
        scanf("%d", &min);
        
        printf("Please enter the date and time the bike is returned\n");
        printf("Year (YYYY): ");
        scanf("%d", &returnyear);
        printf("Month (1 to 12): ");
        scanf("%d", &returnmonth);
        printf("Day (1 to 31): ");
        scanf("%d", &returnday);
        printf("Hour (0 to 23): ");
        scanf("%d", &returnhour);
        printf("Minutes (0 to 59): ");
        scanf("%d", &returnmin);
        
        totalTimeHour = hour - returnhour;
        totalTimeMin = min - returnmin;
        if(totalTimeHour=<5, costTotal = 5);
        else(costTotal = 12, costTotal = costTotal + 0.5);
        printf("Duration of Rental: " "%d hours," "%d minutes\n", totalTimeHour, totalTimeMin);
        printf("The total price comes to: " "$%d\n", costTotal);
    
    
        getch(0);
    }
    I want to calculate and display the total duration and cost of rental time...
    Last edited by convex; 02-14-2014 at 10:08 PM.

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    10
    The specifications that you have given are pretty vague. What calculates the total cost? Is it in $/hr? By the minute? It might be easier to look at things if you have a clear definition of what it is that needs to be done. For instance, if the rental cost is in hours, you only need to convert the values you have received into an hours value. In that case, you would want to declare float variables that will keep track of the rentedHour and returnedHour.

    Also in your code, you only computing hour - returnhour, which from your prompts I can only assume is incorrect. In a pseudo-code sort of way of looking at it I would imagine you would want something like this:

    Code:
    int main (void)
    {
         //Declare int / float variables
         //Perhaps a constant conversion factor if converting to hours
              //24hrs / day, etc..
         //Read in the rented year, month, etc
    
        //Read in the returned year, month...
        //Do some calculation or statement to compare rented year, etc with returned year, etc..
        //Calculate total cost and display
    }
    Also, your if statements aren't going to evaluate. You need a condition and then do any calculation in the body of the if statement. I.E. :
    Code:
    if (totalTime < 5)
    {
       totalCost = 5;
    }
    And not:
    Code:
    if (totalTime < 5, totalCost = 5);   //Incorrect

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    Thanks for the reply JBC223456,

    Thanks for pointing out the IF statement! No wonder it wasn't working, I completely forgot!

    The cost is not what I'm worried about, more the date and time in military time format(to even calculate the cost). For the cost, I just want it to be $5 if rented for 5 or less hours, and $12 everything else, + $0.50 for each additional hour(that's what i wanted my if statement to do haha)

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>
    
    
    int main(void)
    {
        int rentyear, rentmonth, rentday, hour, min;
        int returnyear, returnmonth, returnday, returnhour, returnmin;
        int totalTimeHour, totalTimeMin;
        int cost, costTotal;
        
        printf("Please enter the date and time the bike is rented out\n");
        printf("Year (YYYY): ");
        scanf("%d", &rentyear);
        printf("Month (1 to 12): ");
        scanf("%d", &rentmonth);
        printf("Day (1 to 31): ");
        scanf("%d", &rentday);
        printf("Hour (0 to 23): ");
        scanf("%d", &hour);
        printf("Minutes (0 to 59): ");
        scanf("%d", &min);
        
        printf("Please enter the date and time the bike is returned\n");
        printf("Year (YYYY): ");
        scanf("%d", &returnyear);
        printf("Month (1 to 12): ");
        scanf("%d", &returnmonth);
        printf("Day (1 to 31): ");
        scanf("%d", &returnday);
        printf("Hour (0 to 23): ");
        scanf("%d", &returnhour);
        printf("Minutes (0 to 59): ");
        scanf("%d", &returnmin);
        
        totalTimeHour = hour - returnhour;
        totalTimeMin = min - returnmin;
        
        if(totalTimeHour <= 5)
        {
                     costTotal = 5;
                     }
        else(costTotal = 12);
        costTotal = costTotal + 0.50;
                       
        printf("Duration of Rental: " "%d hours, " "%d minutes\n", totalTimeHour, totalTimeMin);
        printf("The total price comes to: " "$%d\n", costTotal);
    
    
        getch(0);
    }
    Last edited by convex; 02-14-2014 at 10:29 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You are doing the time calculation yourself - so you don't need time.h. time.h is used mostly when your program needs to work with the reported times from the computer.

    You can keep all your variables about time, as integers, since you have the time broken down into years, days, hours, and minutes. If the time used is 2 hours and 28 minutes, for instance, you don't need to work with 2.47 hours.

    To make your math right, start with the largest quantity, and calculate it first: years. Then get the elapsed months, followed by days, and finally hours and minutes. What you want to do is decide what the rate you're program is charging, will be set up for - do you want to charge by the minute, or by the hour, or ??


    Next, make any calculations that apply for different time periods - looks like 5 hours is one such period.
    Once that's done, have your math change each of these elapsed time units which are greater than zero, into the basic time units you want to charge, and you're good to calculate the rest of the charge.

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    Quote Originally Posted by Adak View Post
    You are doing the time calculation yourself - so you don't need time.h. time.h is used mostly when your program needs to work with the reported times from the computer.

    You can keep all your variables about time, as integers, since you have the time broken down into years, days, hours, and minutes. If the time used is 2 hours and 28 minutes, for instance, you don't need to work with 2.47 hours.

    To make your math right, start with the largest quantity, and calculate it first: years. Then get the elapsed months, followed by days, and finally hours and minutes. What you want to do is decide what the rate you're program is charging, will be set up for - do you want to charge by the minute, or by the hour, or ??


    Next, make any calculations that apply for different time periods - looks like 5 hours is one such period.
    Once that's done, have your math change each of these elapsed time units which are greater than zero, into the basic time units you want to charge, and you're good to calculate the rest of the charge.
    Thanks for the response Adak! Thanks for explaining what time.h is, I thought I needed it to calculate the rental duration! When I found out I didn't need it, I broke the rental time down into years/days/hours/mins in order to calculate duration. However, I'm still struggling with that.. how do I convert the month/day/hour/mins ? How do I calculate the duration of rental using them?


    I want to have it so that it charges just $5 for 5 hours or less, $12 for 6 hours or more(up to 24 hours), and each additional hour after 24 hours, charge $0.50. So I guess by the hour.


    Overall, my program is suppose to show the duration of rental in number of hours and minutes including the date, and the final cost of the rental!



    SAMPLE RUN OUTPUT



    Please enter the day and time the bike was rented out
    Year (YYYY): 2014
    Month (1 to 12): 2
    Day (1 to 31): 14
    Hour (0 to 23): 12
    Minutes (0 to 59): 10


    Please enter the day and time the bike was returned
    Year (YYYY): 2014
    Month (1 to 12): 2
    Day (1 to 31): 15
    Hour (0 to 23): 10
    Minutes (0 to 59): 35
    Duration of Rental: 22 hours, 25 min
    The total price comes to: $12.00
    Last edited by convex; 02-14-2014 at 10:52 PM.

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    10
    If it is by the hour, then you only need minutes in order for display purposes, so you can leave that out of the calculation. You will want to use several if statements to calculate the amount charged. Think about how you would structure that portion (hours <= 5? If so, totalcost = $5, etc for 6-24, and then for 24+). You have to keep in mind that you want to charge $0.50 more per hour if the time exceeds 24 hours. Therefore, you will need a loop. The way you have it now, you will only charge a total of $12.50, even if something was rented for 10 years!

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    Quote Originally Posted by jbc223456 View Post
    If it is by the hour, then you only need minutes in order for display purposes, so you can leave that out of the calculation. You will want to use several if statements to calculate the amount charged. Think about how you would structure that portion (hours <= 5? If so, totalcost = $5, etc for 6-24, and then for 24+). You have to keep in mind that you want to charge $0.50 more per hour if the time exceeds 24 hours. Therefore, you will need a loop. The way you have it now, you will only charge a total of $12.50, even if something was rented for 10 years!
    Thanks, you're right, it will only charge 12.50 for anything over 5 hours... However, I'm more worried about that after, near the finishing point of the program. The problem I'm more worried about now is, I don't know how to calculate the duration.
    How do I go abouts calculating/finding the duration something was rented for, in number of hours? Given the inputs of the year/month/day/hour/min?
    Last edited by convex; 02-14-2014 at 11:25 PM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There isn't just ONE WAY to make this calculation. ATM, I'd look to changing all the time intervals into minutes. Lots of them, won't both the computer at all.

    Then see, how many (if any) minutes will be charged at a premium rate, and make that calculation and save it.

    Then how many (if any) minutes should be charged at a regular rate, and make that calculation and save it.

    Finally, how many (if any) minutes should be charged at a discount rate (if you have one of them). Make that calculation, and save it also.

    Now add up premium, + regular + discount charges, and apply any tax, or other adjustment to the total charge.

    And there's your answer, but again, there isn't just one way to do this, but this way seems good to me, atm.

  10. #10
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    Quote Originally Posted by Adak View Post
    There isn't just ONE WAY to make this calculation. ATM, I'd look to changing all the time intervals into minutes. Lots of them, won't both the computer at all.

    Then see, how many (if any) minutes will be charged at a premium rate, and make that calculation and save it.

    Then how many (if any) minutes should be charged at a regular rate, and make that calculation and save it.

    Finally, how many (if any) minutes should be charged at a discount rate (if you have one of them). Make that calculation, and save it also.

    Now add up premium, + regular + discount charges, and apply any tax, or other adjustment to the total charge.

    And there's your answer, but again, there isn't just one way to do this, but this way seems good to me, atm.

    Adak, thanks but that sounds completely different than what my simple program wants to do! If I wanted to make a full rental program including different rates and taxes, I would definitely take more time to read and research about C. I am learning as we go, and am trying to do this for a school assignment! So I only need the basics for now.

    I don't need this program to do all the fancy stuff a regular rental rate program needs, just given the info, I need to display the duration of rental in number of hours and minutes as well as the cost (the rate is simply $5 for 5 or less hours, $12 for 6 or more hours, and 50 cents each hour after first 24 hours.) (keep in mind this is my second assignment for my school programming course)

    I'm really stuck on calculating/finding the duration something was rented for, in number of hours... Given the inputs of the year/month/day/hour/min
    Last edited by convex; 02-14-2014 at 11:25 PM.

  11. #11
    Registered User
    Join Date
    Jan 2014
    Posts
    10
    Every programmer has their own way of doing things - and there is not one method to calculating what you need.

    - Read in your values as you are.
    - Make sure the input is valid (i.e. can't return something before it is rented)

    Your math could get kind of confusing the way it is set up - Is it required that you get the year, month, day, hour, and minute when you only need to calculate hour and minute duration? If so, you are going to need to handle special cases [i.e. rented month is 9, returned month is 4 (but of the next year)], so you will need to make sure you take into account something like rentedYear: 2013, rentedMonth: 10, rentedDay: 21, rentedHour: 17, rentedMin: 00 and returnedYear: 2014 with returnedMonth: 2.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If you don't need to add taxes, etc., then LEAVE THAT PART OUT. You WILL need to calculate at least three different rates, however. Your calculations MUST be similar to what I wrote above.

    This is simple math, and math doesn't give a damn about your proficiency in C, school grade, etc. It's easier than it looks. I don't know a way to use what you have, and get the right answers - and I don't believe it's even possible. You can order the operations in a very different sequence than I did, but you must do the basics of getting the time * rate = subtotal, for each type of rate. Then adding the sub-totals together.

    If the rental month is greater than the returned month, what does that already tell you? Right, the rental period has extended over into at least one other year.
    Last edited by Adak; 02-14-2014 at 11:26 PM.

  13. #13
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    I'm really stuck on what to do. What do you mean I need 3 different rates? $5/$12/and $0.50 every hour after 24 hours?

    Say I input April 10th of 2014 at 13:10(1:10PM) rented out, and returned at April 12th, of 2014 at 11:35(AM). That's a total of 46 hours and 25 mins, which should cost me a total of $23.50..

    My program would read that as 2 days (but it hasn't really been 2 days because it hasn't really been 48 hours, and it would mess up the total cost..)
    For the hours, my program would read 11+13 = 24 hours = 1 day, instead of 46 hours total.. I'm not very good with math ..

    Or like jbc223456 said, If I input the rent date as 25th of December 2013, and the return date of 2nd of January 2014, my program will read that as 1 year no matter the duration...I'm not sure how to calculate the duration that it was rented out for in hours/minutes

    Help!

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You posted:
    I need to display the duration of rental in number of hours and minutes as well as the cost (the rate is simply $5 for 5 or less hours, $12 for 6 or more hours, and 50 cents each hour after first 24 hours.)
    Which looks like three different rates to me.

    The only way to learn this, is by doing it. Take a paper and pencil, and work through some simulated rental costs, for different amounts of rental time. Do it by hand, and save your notes on the paper. That will be the calculations your program will need to complete this program.

    It's not difficult to do it by hand, but it takes some time, and you have to be able to understand it. It's rare that you can code up something that works right, when you don't fully understand that something, and how it was calculated. Really REALLY rare.

  15. #15
    Registered User
    Join Date
    Feb 2014
    Posts
    10
    I'm still lost on what to do! I've changed all of the time intervals into minutes

    Code:
        int minsyear = 527040;
        int minsday = 1440;
        int minshour = 60;
    I'm just really confused about the math. I'm not sure how to calculate the rental duration in minutes... I've gotten my program up to the point where I could calculate the difference in Years/Months/Days and display that (simple returnyear/returnmonth/returnday subtract from rentyear/rentmonth etc.)

    Say I input April 10th of 2014 at 13:10(1:10PM) rented out, and returned at April 12th, of 2014 at 11:35(AM). That's a total of 46 hours and 25 mins, which should cost me a total of $23.50..

    My program would read that as 2 days (but it hasn't really been 2 days because it hasn't really been 48 hours, and it would mess up the total cost..)
    For the hours, my program would read 11+13 = 24 hours = 1 day, instead of 46 hours total.. I'm not very good with math ..


    If I change the days into minutes(1440 mins a day), I don't know what to do and how to calculate the difference between return day and rented day...


    This is for my school assignment, I'm already 10% late and I'm more confused than when I started, help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very basic OOP programming in C
    By patishi in forum C Programming
    Replies: 2
    Last Post: 12-20-2013, 03:08 PM
  2. help with my basic c programming
    By chris199236 in forum C Programming
    Replies: 15
    Last Post: 09-25-2012, 09:23 PM
  3. Basic C programming 3
    By vivekgupta in forum C Programming
    Replies: 10
    Last Post: 06-13-2012, 11:22 AM
  4. Basic C programming -1
    By vivekgupta in forum C Programming
    Replies: 29
    Last Post: 06-11-2012, 03:26 PM
  5. basic C-programming Q
    By slyonedoofy in forum C Programming
    Replies: 2
    Last Post: 10-30-2002, 09:54 PM