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; My program is giving me a problem when i error check it an run it after i input wrong data ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    36

    Help my program is giving a problem

    My program is giving me a problem when i error check it an run it after i input wrong data it skips the thing an moves on an also i need guidance or a hint as to how the time minus thing and what exactly is rounded time as i was instructed to put it in but im not exactly sure as to what it is

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    
    {
    char vehicle[15];
    int car;
    int truck;
    int hrsn, minn;
    int hrso, mino;
    int time;
    int rt;
    hrsn=0;
    hrso=0;
    minn=0;
    mino=0;
    
    
    printf("Where you driving a car or truck?\n");
    scanf("%s", &vehicle );
    
    printf("What Hour did the %s enter the lot? (0-23)\n", vehicle);
    scanf("%d",&hrsn);
    if((hrsn<0 || hrsn>23))
    {
        printf("\a\a\a Please Input Value from 0-23!\n");
    }
    else
    {
        printf("What Minute did the %s enter the lot? (0-59)\n", vehicle);
        scanf("%d",& minn);
    }
    if((minn<0 || minn>59))
    {
        printf("\a\a\a Please Enter Correct Minute Between 0-59!\n");
    }
    else
    {
        printf("What Hour did the %s leave the lot? (0-23)\n", vehicle);
        scanf("%d",& hrso);
    }
    if((hrso<0 || hrso>23))
    {
        printf("\a\a\a Please Enter the Correct Time Between 0-23!\n");
    }
    else
    {
        printf("What Minute did the %s leave the lot? (0-59)\n", vehicle);
        scanf("%d",& mino);
    }
    if(mino<0 || mino>59)
    {
        printf("\a\a\a PLease Enter the Correct time between 0-59!\n");
    }
    else
    {
    rt = hrso - hrsn && mino - minn;
    
    printf("Time in %d:%d\n", hrsn, minn);
    printf("Time out %d:%d\n", hrso, mino);
    
    printf("Parking Time %d\n", time);
    printf("Rounded time %d\n", rt);
    }
    
    
    
    
    return 0;
    
    
    }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yes, what you want is to ask the user to input a correct amount and until he does keep asking. Think that you are a salesman. What you do is
    Code:
    printf("Would you by this fine product?");
    scanf("%c", c);
    if (c != 'y')
    {
      printf("Oh, well, have a nice day sir");
      fireEmployee();
    }
    else
    {
       printf("Nice! We aslo have this coupon...");
    }
    what you want is:
    Code:
    printf("Would you by this fine product?");
    scanf("%c", c);
    while (c != 'y')
    {
        printf("Please reconsider");
        scanf("%c", c);
    }
    printf("Nice! We aslo have this coupon...");
    Try replacing the if-else with while.

    As for the rounded time is not clear like that. Maybe the total time in minutes? Maybe rounding up the time to hours? Your line of code for rt = ... is definitely not doing what you want (&& is the logical AND).

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The key concept to remember here is the word "aslo". Lol, j/k
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    Thx C_ntua i'll try it an get back to u and for the example u have given me can that work for words like with what i have i would definately need error checking in words for example for the user to input the words car or truck only, can your example be used like right here:
    "(c != 'y')"
    be used like (c != 'car') then blah blah blah

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My program is giving me a problem when i error check it an run it after i input wrong data it skips the thing an moves on an also i need guidance or a hint as to how the time minus thing and what exactly is rounded time as i was instructed to put it in but im not exactly sure as to what it is
    === Whew! What a read, Zangetsu! ===

    You may thank God almighty that you're not an English major!
    Last edited by Adak; 01-29-2011 at 10:59 PM.

  6. #6
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    Code:
    printf("Would you by this fine product?");
    scanf("%c", c);
    while (c != 'y')
    {
        printf("Please reconsider");
        scanf("%c", c);
    }
    printf("Nice! We also have this coupon...");
    Is the point to ........ the costumer off? I would go with this

    Code:
    int loop = 0;
    ...
    printf("Would you by this fine product?");
    scanf("%c", c);
    while (c != 'y' || loop >= 50)
    {
        loop++;
    
        printf("Please reconsider");
    
        scanf("%c", c);
    
        
    }
    if (loop > 2)
    {
         printf("well, have a nice day then...");
    }
    
    else
    {
         printf("Nice! We also have this coupon...");
    }

    (I didn't read the question I just thought your answer was comical.)

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    (I didn't read the question I just thought your answer was comical.)
    I don't "by" that!

  8. #8
    That weird Java guy xniinja's Avatar
    Join Date
    Jun 2010
    Posts
    230
    lol... I recognized that after I posted it, I fixed aslo though.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    k i input the while statements and they worked so thanx very much C_ntua and everyone else who gave valid input and not jokes.lol Is there a way to do error checking for words because i would need for the user to input the words car or truck only is this possible i thought of maybe doing something to count the words car or truck so if the input is greater than 3 characters then its not the word car so is this line of thinking even possible?

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    I worked on the time section pls run it and tell me if im doing the right thing cause now i think i might have confused myself a bit

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    
    {
    char vehicle[15];
    int car;
    int truck;
    int hrsn, minn;
    int hrso, mino;
    int hr; //final hour
    int min; //final minute
    hrsn=0;
    hrso=0;
    minn=0;
    mino=0;
    hr=0;
    min=0;
    
    printf("Where you driving a car or truck?\n");
    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 (hrsn < (hrso - 1) && minn > mino)
    { 
    hrso = hrso - 1;
    mino = mino + 60;
    }
    if (mino - minn > 1 && mino - minn < 30)
    {
    mino = minn+30;
    }
    
    else if (mino - minn > 30 && mino - minn < 60)
    {
    mino = minn + 60;
    }
    
    else
    {
    mino = minn + 00;
    }
    
    hr = (hrso-hrsn);
    min = (mino - minn);
    
    printf("Time in %d:%d\n", hrsn, minn);
    printf("Time out %d:%d\n", hrso, mino);
    
    printf("Parking Time %d:%d\n", hr, min);
    
    }
    
    return 0;
    
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    "Doctor, I may have a pain."

    Please be specific with what the problem is!

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    The time aspect of the program in subtracting the time i would like your advice on it to c if what i did was right or if u have any advice or anything like that basically your critique on it

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If it's accurate - it's OK. Run-time and clarity are not an issue for the code. It's short and simple.

    IMO, you don't need to include either math.h or stdlib.h, and your code should be indented so the subordinate lines of code are indented 2 to 4 spaces.

    Code:
    do {
      print something
      ask for input with scanf
    }while(input value was etc.)
    
    //looks a lot easier to understand and debug, than
    do
    {
    print something
    ask for input with scanf
    }
    while(input value is etc.)
    After you've been programming for awhile, your eye will become trained, if you use standard indentation, to readily pick up on syntax bugs in your code. Properly indented code allows us to also use our trained eyes, to quickly spot some kinds of common errors.

    If you want people to study your code for free, why not help them to do so?

  14. #14
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    I'm trying to have my program do word error checking but for some reason its not working i want the user to input either the words car or truck this is what i have done so far!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main()
    
    {
    char *vehicle;
    int car;
    int truck;
    int hrsn, minn;
    int hrso, mino;
    int hr; //final hour
    int min; //final minute
    hrsn=0;
    hrso=0;
    minn=0;
    mino=0;
    hr=0;
    min=0;
    
    printf("Where you driving a car or truck?\n");
    scanf("%s", &vehicle );
    
    while(vehicle != "car" && vehicle != "truck")
    {
        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 (hrsn < (hrso - 1) && minn > mino)
    {
    hrso = hrso - 1;
    mino = mino + 60;
    }
    if (mino - minn > 1 && mino - minn < 30)
    {
    mino = minn+30;
    }
    
    else if (mino - minn > 30 && mino - minn < 60)
    {
    mino = minn + 60;
    }
    
    else
    {
    mino = minn + 00;
    }
    
    hr = (hrso-hrsn);
    min = (mino - minn);
    
    printf("Time in %d:%d\n", hrsn, minn);
    printf("Time out %d:%d\n", hrso, mino);
    
    printf("Parking Time %d:%d\n", hr, min);
    
    }
    
    return 0;
    
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should use strcmp to compare strings.

Page 1 of 5 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-26-2010, 12:02 AM
  2. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 03:58 PM
  3. Replies: 2
    Last Post: 09-16-2009, 07:00 AM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 01:35 AM

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