Thread: Help my program is giving a problem

  1. #46
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    i keep getting the same results every time if i choose truck or car with any amount of time but truck's result is different from car's

    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);
    
    int main(void)
    {
    
    char vehicle[6];
    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;
    price=0;
    float finalprice;
    finalprice=0;
    float gct;
    gct=0.175; //17.5%
    int price_car;
    price_car=0;
    int price_truck;
    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);
    return 0;
    }
    
    
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle)
    {
    
    if((strcmp(vehicle, "car")) == 0)
    {
    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);
    }
    else
    
    if((strcmp(vehicle, "truck")) == 0)
    {
        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);
    
    }
    
    }

  2. #47
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    scanf("%s", &vehicle);
    ...
    Tax(&gct,&price_car,&price_truck,&price,&finalprice,&minutes_max,&hours_max,&vehicle);
    Remove the & from those two places. The name of the array is a pointer to it's first element, so vehicle is a char *. That makes &vehicle a char **, which is not what Tax wants.
    Last edited by anduril462; 02-23-2011 at 07:05 PM. Reason: Removed garbage

  3. #48
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I formatted and tweaked your program. Can't tell if it's accurate or not, but the car and truck pricing is different in the test I made.

    Check it out, and use it in any future posts, because I'm not spending any more time formatting your code and removing useless code, again.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle);
    
    int main(void)
    {
      char vehicle[6];
      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
      int price;
      int price_car;
      int price_truck;
      int i, length;
      float finalprice;
      float gct;
      hrsn=0;
      hrso=0;
      minn=0;
      mino=0;
      hr=0;
      min=0;
      minutes_max=0;
      hours_max=0;
      price=0;
      finalprice=0;
      gct=0.175; //17.5%
      price_car=0;
      price_truck=0;
      
      printf("\n\nWhere 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);
      }
      length = strlen(vehicle);  
      for (i=0; i<length; i++)   
      { //change all vehicle letters to lowercase
        vehicle[i] = tolower(vehicle[i]); 
      }
    
      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);
      return 0;
    }
    void Tax(float *gct, int *price_car, int *price_truck, int *price, float *finalprice, int *minutes_max, int *hours_max, char *vehicle)
    {
      if((strcmp(vehicle, "car")) == 0)
      {
        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);
      }
      else
      //if((strcmp(vehicle, "truck")) == 0)
      {
        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);
      }
    }

  4. #49
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Thanks for neating up the code i tried different stuff with the tax part but it just aint working for me

  5. #50
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by zangetsu View Post
    Thanks for neating up the code i tried different stuff with the tax part but it just aint working for me
    Ok, it's not calculating the tax correctly. Let's debug:

    1) What input is giving the right answer? Anything?

    2) What input is giving the wrong answer? What's the right answer for this input?

    Post up the arithmetic that should be used for the tax. I don't want to use the code, if it's known to be wrong.

    What's the equation for the tax? Do you have an equation, instead of a description, to calculate the tax?

    When you say "Tax", you mean "Parking Charge", right?

  6. #51
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    its like this, car and truck are suppose to have different prices and tax is the added on Government tax onto the fare. So its the fare plus tax. so for 0 to 30 mins its bout $50 for car, and 30 mins to 60 is treated like an hour so its $100 then every hour is $100
    so the final thing is finalprice=(gct X price)+price, before this during the 0 to 30 mins thing if u look at the code its like price=price_car+50 and gct = %17.5 or 0.175

  7. #52
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK. thanks for clearing that up.

    Now, what vehicle and what time for that vehicle, is giving the wrong output?

    What should the answer be to that same vehicle, and that same time?

  8. #53
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    it doesn't matter what i put in, i'm always getting the same answer. BAsically 30 minutes should be $50 +tax for a car and it should be $100 for every hour

  9. #54
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What if I'm there for an hour and a half?
    Code:
    if minutes between 1 and 29
        ... no, i'm at minutes 30
    ELSE
    if minutes between 30 and 59
        ... yes, my minutes are at 30
    ELSE
    if hours ...
        ... yes i have one hour, but you can't check that, because you already checked minutes
    See your problem yet?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #55
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    i think so are u saying that since i only want 30mins and a hour i should take out the 30 to 59 part? is that what your saying cause im not too understanding your non-sentences

  11. #56
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What he's saying is that your arithmetic on the hours and minutes, fails if the vehicle is there for 90 minutes.

    Try it and see if Quzah is correct.

  12. #57
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    What he's saying is that your arithmetic on the hours and minutes, fails if the vehicle is there for 90 minutes.

    Try it and see if Quzah is correct.
    I see my dead horse is still kicking...

    Frankly I'm a little amazed that nobody here has come straight out and told the OP he could probably do this job in about 50 lines of code if he simply converted the time to minutes.

    It doesn't matter when the car or truck arrived. It doesn't matter when it left. What matters is how long it was parked on the lot...

    1.Get the start time as hours and minutes from the operator.
    2.Get the end time as hours and minutes from the operator.
    3.Calculate the number of minutes on the lot (maybe 15 or 20 lines of code so far)
    4.If more than 30 minutes, work out the charge for the first 30 minutes. (5 lines max)
    5.Subtract 30 from the number of minutes on the lot. (1 line)
    6.Work out the charge for the rest of the time. (2 or 3 lines)
    7. Add up the two charges (1 line)
    8. Calculate tax on the total (2 lines max)
    9. Print the guy's bill (5 lines max)

    The fact there are two rates in play chould add no more than 5 extra lines to the code.

    Really... This is not a complex problem.

  13. #58
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Tater, you told him several times. Obviously, he doesn't want to do that. Not everyone is as good at beating zee dead horse, as you are!

    Good to see you're still at it - +1 for sticking with it. I'm not going to mention that it will happen when pigs fly.

    The tickets you get from the parking garage, will typically show time in and time out.

  14. #59
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Tater, you told him several times. Obviously, he doesn't want to do that. Not everyone is as good at beating zee dead horse, as you are!

    Good to see you're still at it - +1 for sticking with it. I'm not going to mention that it will happen when pigs fly.
    Even you, my patient friend, have to admit, it's getting just a bit ridiculous.

  15. #60
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Really... This is not a complex problem.
    Clearly, you never met the next of kin to Rube Goldberg! ANYTHING can be a complex problem. Just give them a chance...

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