Help my program is giving a problem

This is a discussion on Help my program is giving a problem within the C Programming forums, part of the General Programming Boards category; i fixed that part but its not calculating the final price properly Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...

  1. #31
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    i fixed that part but its not calculating the final price properly

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    
    
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle[5]);
    
    int main(void)
    
    {
    
    char vehicle[5];
    int car;
    int truck;
    int hrsn; //Hour the vehicle entered the parking lot
    int minn; //Minute the vehicle came in the parking lot
    int hrso; //Hour the vehicle left the parking lot
    int mino; //Minute the vehicle left the parking lot
    int hr; //final hour
    int min; //final minute
    int minutes_max;//60 minutes
    int hours_max;//24 hours
    hrsn=0;
    hrso=0;
    minn=0;
    mino=0;
    hr=0;
    min=0;
    minutes_max=0;
    hours_max=0;
    int price=0;
    float finalprice=0;
    float gct=0.175; //17.5%
    int price_car=0;
    int price_truck=0;
    
    
    printf("Where you driving a car or truck?\n");
    scanf("%s", &vehicle );
    
    while( strcmp( vehicle, "truck" ) != 0 && strcmp( vehicle, "car" ) != 0 )
    {
        printf("Please input the words Car or Truck: ");
        scanf("%s",&vehicle);
    }
    
    printf("What Hour did the %s enter the lot? (0-23)\n", vehicle);
    scanf("%d",&hrsn);
    while((hrsn<0 || hrsn>23))
    {
        printf("\a\a\a Please Input Value from 0-23!\n");
        scanf("%d",& hrsn);
    }
    
    {
        printf("What Minute did the %s enter the lot? (0-59)\n", vehicle);
        scanf("%d",& minn);
    }
    while((minn<0 || minn>59))
    {
        printf("\a\a\a Please Enter Correct Minute Between 0-59!\n");
        scanf("%d",& minn);
    }
    
    {
        printf("What Hour did the %s leave the lot? (0-23)\n", vehicle);
        scanf("%d",& hrso);
    }
    while((hrso<0 || hrso>23))
    {
        printf("\a\a\a Please Enter the Correct Time Between 0-23!\n");
        scanf("%d",& hrso);
    }
    
    {
        printf("What Minute did the %s leave the lot? (0-59)\n", vehicle);
        scanf("%d",& mino);
    }
    while(mino<0 || mino>59)
    {
        printf("\a\a\a PLease Enter the Correct time between 0-59!\n");
        scanf("%d",& mino);
    }
    
    
    if(minn < mino)
    {
      minutes_max = 60 - (mino - minn);
      //minutes_max = 60 - 5
      //minutes_max = 55 minutes
    }
    else if(minn > mino)
    {
         minutes_max = (60 - minn) + mino;
         hrso = hrso-1;
    }
    
    else
    
      minutes_max = 0; //minutes_in equals minutes_out
    
    
    if (hrsn < hrso)
    {
        hours_max = 24 - (hrso - hrsn);
    }
    
    else if (hrsn > hrso)
    {
        hours_max = (24 - hrso) + hrsn;
    }
    
    else
    hours_max = 0;
    
    
    hours_max = (hrso - hrsn);
    
    
    printf("Time in %d:%d\n", hrsn, minn);
    printf("Parking Time:  %d:%d \n", hours_max, minutes_max);
    
    Tax(&gct,&price_car,&price_truck,&price,&finalprice,&minutes_max,&hours_max,&vehicle[5]);
    
    
    }
    
    
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle[5])
    {
    
    while((strcmp(*vehicle, "car")) == 0)
    {
    if( *minutes_max > 1 || *minutes_max < 30)
    {
        *price = *price_car + 50;
    }
    else if(*minutes_max > 30 || *minutes_max < 60)
    {
        *price = *price_car + 100;
    }
    else if( *hours_max >= 1)
    {
        *price = (*price_car+100) * *hours_max;
    }
    
    *finalprice = (*gct * *price) + *price;
    
    
    if((strcmp(*vehicle, "truck")) == 0)
    {
    
    if( *minutes_max > 1 || *minutes_max < 30)
    {
        *price = *price_truck + 50;
    }
    else if(*minutes_max > 30 || *minutes_max < 60)
    {
        *price = *price_truck + 100;
    }
    else if( *hours_max >= 1)
    {
        *price = (*price_truck+100) * *hours_max;
    }
    
    *finalprice = (*gct * *price) + *price;
    
    }
    }
    printf("The Final Price is: %f", *finalprice);
    
    }

  2. #32
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    At the risk of suggesting a huge change...

    Why do you work in hours and minutes?
    If you timestamp each vehicles arrival time with In = time(NULL);
    Then Timestamp each vehicles departure with Out = time(NULL);
    All you have to do is subract... TimeInLot = Out - In;
    The answer will be in seconds... So calculate your price on seconds.
    The only time you need hours and minutes is to *display* how long he was there.

  3. #33
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Because this is an assessment the teacher is definately not going to take the time out to be calculating seconds to input i need something extremely user friendly not even i would take the time out to calculate seconds like that

  4. #34
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zangetsu View Post
    Because this is an assessment the teacher is definately not going to take the time out to be calculating seconds to input i need something extremely user friendly not even i would take the time out to calculate seconds like that
    What are you talking about?

    The suggestion was to have the software work *internally* on seconds...
    A small function would display hours and minutes from seconds...
    you already have a function to convert from hours to minutes.

    You and your teacher don't have to calculate *anything*... that's what software is for.

  5. #35
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    you already have a function to convert from hours to minutes.
    I DO!?... Where!? lmao like seriously i dont think we're on the same page can u explain it to mi a little better please? cause im not understanding what it is exactly what your saying?

  6. #36
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One thing you really need to do, is to incrementally debug your program, as you're writing it.

    Verify each of your calculations, before you move onto the writing up the next one. The worst thing is to have a bug show up, after you've got scores of new code written up, and you have little idea of which function is causing the problem.

    Imo, you're making this more difficult than it needs to be, and passing too many variables around to your functions - but that's OK, if it's what you want. it's more difficult than it has to be, is all.

    If a calculation is not right, you need to step through the code with your debugger, and watch the value of the variables involved, to see what's wrong. You can do that better than anyone else, because no one knows your program, better than you do, now.

  7. #37
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zangetsu View Post
    I DO!?... Where!? lmao like seriously i dont think we're on the same page can u explain it to mi a little better please? cause im not understanding what it is exactly what your saying?
    Ok... standard C function ... time(); ... the number of seconds since midnight 1980... incremented every second.

    Car pulls into lot... TimeIn = time(NULL);
    Time marches on...
    Care pulls upt to pay... TimeOut = time(NULL);

    Now in your present system you have to track hours and minutes separately... The logic and the math is complex, especially if it rolls over midnight so that a car enters at 10:00pm and leaves at 2:00am....

    My way it's a simple arithmetic function... TimeCharge = TimeOut - TimeIn;
    Now you know how many seconds he's been on the lot, thus you can easily calculate how much to charge, with nothing more complex than a quick subtraction...

    Look at your code... how much of it is figuring out how many hours and minutes he's been on the lot? I just reduced that to one simple subtraction statement.

    Fine... you charge by the minute... MinutesDue = TimeCharge / 60;

    So all that logic has just been reduced to a single division problem.

    Look how much of your code is calculating this... You could reduce it to 2 or 3 lines of code.

    That's all I was suggesting.

  8. #38
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    May I suggest that you're beating a dead horse? < ROFL! >

  9. #39
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    May I suggest that you're beating a dead horse? < ROFL! >
    Who me? Never!

  10. #40
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    in my program i think its something with the while part cause maybe i haven't have it done something for that statement , i probably sound confusing like with a if statement if i say
    if(that = bat)
    then do this
    but i jus have while then a if statement

    Code:
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle[5])
    {
    
    while((strcmp(*vehicle, "car")) == 0)
    {
    if( *minutes_max > 1 && *minutes_max < 30)
    {
        *price = *price_car + 50;
    }

  11. #41
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    This is getting real mind boggling now cause i try different things and i get different outputs but its always the same and its not printing that second to last printf can u tell me a better way to arrange the last past about the selection of the car or truck?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    
    
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle[5]);
    
    int main(void)
    {
    
    char vehicle[5];
    int car;
    int truck;
    int hrsn; //Hour the vehicle entered the parking lot
    int minn; //Minute the vehicle came in the parking lot
    int hrso; //Hour the vehicle left the parking lot
    int mino; //Minute the vehicle left the parking lot
    int hr; //final hour
    int min; //final minute
    int minutes_max;//60 minutes
    int hours_max;//24 hours
    hrsn=0;
    hrso=0;
    minn=0;
    mino=0;
    hr=0;
    min=0;
    minutes_max=0;
    hours_max=0;
    int price=0;
    float finalprice=0;
    float gct=0.175; //17.5%
    int price_car=0;
    int price_truck=0;
    
    
    printf("Where you driving a car or truck?\n");
    scanf("%s", &vehicle );
    
    while( strcmp( vehicle, "truck" ) != 0 && strcmp( vehicle, "car" ) != 0 )
    {
        printf("Please input the words Car or Truck: ");
        scanf("%s",&vehicle);
    }
    
    printf("What Hour did the %s enter the lot? (0-23)\n", vehicle);
    scanf("%d",&hrsn);
    while((hrsn<0 || hrsn>23))
    {
        printf("\a\a\a Please Input Value from 0-23!\n");
        scanf("%d",& hrsn);
    }
    
    {
        printf("What Minute did the %s enter the lot? (0-59)\n", vehicle);
        scanf("%d",& minn);
    }
    while((minn<0 || minn>59))
    {
        printf("\a\a\a Please Enter Correct Minute Between 0-59!\n");
        scanf("%d",& minn);
    }
    
    {
        printf("What Hour did the %s leave the lot? (0-23)\n", vehicle);
        scanf("%d",& hrso);
    }
    while((hrso<0 || hrso>23))
    {
        printf("\a\a\a Please Enter the Correct Time Between 0-23!\n");
        scanf("%d",& hrso);
    }
    
    {
        printf("What Minute did the %s leave the lot? (0-59)\n", vehicle);
        scanf("%d",& mino);
    }
    while(mino<0 || mino>59)
    {
        printf("\a\a\a PLease Enter the Correct time between 0-59!\n");
        scanf("%d",& mino);
    }
    
    
    if(minn < mino)
    {
      minutes_max = 60 - (mino - minn);
      //minutes_max = 60 - 5
      //minutes_max = 55 minutes
    }
    else if(minn > mino)
    {
         minutes_max = (60 - minn) + mino;
         hrso = hrso-1;
    }
    
    else
    
      minutes_max = 0; //minutes_in equals minutes_out
    
    
    if (hrsn < hrso)
    {
        hours_max = 24 - (hrso - hrsn);
    }
    
    else if (hrsn > hrso)
    {
        hours_max = (24 - hrso) + hrsn;
    }
    
    else
    hours_max = 0;
    
    
    hours_max = (hrso - hrsn);
    
    
    printf("Time in %d:%d\n", hrsn, minn);
    printf("Parking Time:  %d hours:%d minutes\n", hours_max, minutes_max);
    
    Tax(&gct,&price_car,&price_truck,&price,&finalprice,&minutes_max,&hours_max,&vehicle[5]);
    
    
    }
    
    
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle[5])
    {
    {
    if((strcmp(*vehicle, "car")) == 0)
    {
        printf("The price for the car will be shown shortly:");
    }
    else if( *minutes_max > 1 || *minutes_max < 30)
    {
        *price = *price_car + 50;
    }
    else if(*minutes_max > 30 || *minutes_max < 60)
    {
        *price = *price_car + 100;
    }
    else if( *hours_max >= 1)
    {
        *price = (*price_car+100) * *hours_max;
    }
    
    *finalprice = (*gct * *price) + *price;
    }
    
    
    {
    if((strcmp(*vehicle, "truck")) == 0)
    {
        printf("The Price for the truck will be shown shortly");
    }
    else if( *minutes_max > 1 || *minutes_max < 30)
    {
        *price = *price_truck + 100;
    }
    else if(*minutes_max > 30 || *minutes_max < 60)
    {
        *price = *price_truck + 150;
    }
    else if( *hours_max >= 1)
    {
        *price = (*price_truck+200) * *hours_max;
    }
    
    *finalprice = (*gct * *price) + *price;
    
    
    }
    
    printf("The Final Price is: %f", *finalprice);
    
    
    
    }

  12. #42
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    i changed it to a do while in the tax function section but now its running both of the do whiles, please help


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <math.h>
    
    
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle[5]);
    
    int main(void)
    {
    
    char vehicle[5];
    int car;
    int truck;
    int hrsn; //Hour the vehicle entered the parking lot
    int minn; //Minute the vehicle came in the parking lot
    int hrso; //Hour the vehicle left the parking lot
    int mino; //Minute the vehicle left the parking lot
    int hr; //final hour
    int min; //final minute
    int minutes_max;//60 minutes
    int hours_max;//24 hours
    hrsn=0;
    hrso=0;
    minn=0;
    mino=0;
    hr=0;
    min=0;
    minutes_max=0;
    hours_max=0;
    int price=0;
    float finalprice=0;
    float gct=0.175; //17.5%
    int price_car=0;
    int price_truck=0;
    
    
    printf("Where you driving a car or truck?\n");
    scanf("%s", &vehicle );
    
    while( strcmp( vehicle, "truck" ) != 0 && strcmp( vehicle, "car" ) != 0 )
    {
        printf("Please input the words Car or Truck: ");
        scanf("%s",&vehicle);
    }
    
    printf("What Hour did the %s enter the lot? (0-23)\n", vehicle);
    scanf("%d",&hrsn);
    while((hrsn<0 || hrsn>23))
    {
        printf("\a\a\a Please Input Value from 0-23!\n");
        scanf("%d",& hrsn);
    }
    
    {
        printf("What Minute did the %s enter the lot? (0-59)\n", vehicle);
        scanf("%d",& minn);
    }
    while((minn<0 || minn>59))
    {
        printf("\a\a\a Please Enter Correct Minute Between 0-59!\n");
        scanf("%d",& minn);
    }
    
    {
        printf("What Hour did the %s leave the lot? (0-23)\n", vehicle);
        scanf("%d",& hrso);
    }
    while((hrso<0 || hrso>23))
    {
        printf("\a\a\a Please Enter the Correct Time Between 0-23!\n");
        scanf("%d",& hrso);
    }
    
    {
        printf("What Minute did the %s leave the lot? (0-59)\n", vehicle);
        scanf("%d",& mino);
    }
    while(mino<0 || mino>59)
    {
        printf("\a\a\a PLease Enter the Correct time between 0-59!\n");
        scanf("%d",& mino);
    }
    
    
    if(minn < mino)
    {
      minutes_max = 60 - (mino - minn);
      //minutes_max = 60 - 5
      //minutes_max = 55 minutes
    }
    else if(minn > mino)
    {
         minutes_max = (60 - minn) + mino;
         hrso = hrso-1;
    }
    
    else
    
      minutes_max = 0; //minutes_in equals minutes_out
    
    
    if (hrsn < hrso)
    {
        hours_max = 24 - (hrso - hrsn);
    }
    
    else if (hrsn > hrso)
    {
        hours_max = (24 - hrso) + hrsn;
    }
    
    else
    hours_max = 0;
    
    
    hours_max = (hrso - hrsn);
    
    
    printf("Time in %d:%d\n", hrsn, minn);
    printf("Parking Time:  %d hours:%d minutes\n", hours_max, minutes_max);
    
    Tax(&gct,&price_car,&price_truck,&price,&finalprice,&minutes_max,&hours_max,&vehicle[5]);
    
    
    }
    
    
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle[5])
    {
    
    do{
    printf("The price for the car will be shown shortly:\n");
    
    if( *minutes_max > 1 || *minutes_max < 30)
    {
        *price = *price_car + 50;
    }
    else if(*minutes_max > 30 || *minutes_max < 60)
    {
        *price = *price_car + 100;
    }
    else if( *hours_max >= 1)
    {
        *price = (*price_car+100) * *hours_max;
    }
    
    *finalprice = (*gct * *price) + *price;
    
    printf("The Final Price is: %f\n", *finalprice);
    }while((strcmp(*vehicle, "car")) == 0);
    
    do{
        printf("The Price for the truck will be shown shortly\n");
    
    if( *minutes_max > 1 || *minutes_max < 30)
    {
        *price = *price_truck + 100;
    }
    else if(*minutes_max > 30 || *minutes_max < 60)
    {
        *price = *price_truck + 150;
    }
    else if( *hours_max >= 1)
    {
        *price = (*price_truck+200) * *hours_max;
    }
    
    *finalprice = (*gct * *price) + *price;
    
    printf("The Final Price is: %f\n", *finalprice);
    
    }while((strcmp(*vehicle, "truck")) == 0);
    
    }

  13. #43
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,309
    $ gcc -Wall park.c
    park.c: In function ‘main’:
    park.c:39: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[5]’
    park.c:44: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[5]’
    park.c:123: warning: passing argument 8 of ‘Tax’ from incompatible pointer type
    park.c:8: note: expected ‘char **’ but argument is of type ‘char *’
    • If you're planning on storing "truck", you need at least 6 characters (5 + 1 for the null).
    • In the scanf calls, you only need vehicle. The name of an array serves as a pointer to it's first element.
    • The parameter char *vehicle[5] in the Tax function is an array of pointers-to-char, you just want the *, no [].
    • Once you fix that parameter, remove the * from vehicle in the strcmp calls in Tax.

    park.c:14: warning: unused variable ‘truck’
    park.c:13: warning: unused variable ‘car’
    park.c:126: warning: control reaches end of non-void function
    • You can ditch car and truck since they're not used.
    • You said main would return an int, so make it do just that (return 0 at the end).


    Without re-reading the entire thread, the problem is that you have two distinct do-while loops in your Tax function. A do-while loop always executes at least one time. That means that you will always run the car tax code and the truck tax code at least once each. You can always switch the do-while loops to an if-else setup:
    Code:
    if it's a car
        do car calcs
    else if it's a truck
        do truck calcs

  14. #44
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    First of all thx for the quick response since the deadline is drawing much closer and if u could look at my older post i did try the if else an while if else set up and it didnt work at all

  15. #45
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try to use the simple logic shown by Anduril, above:

    Code:
    if(it's a car) 
       //do what you need for a car
    else //it's a truck
       //do what you need for a truck
    Then post your latest code, and I'll study it when I return in 2 hours. Now, I have to go.

Page 3 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  3. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

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