Thread: Please Help!

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    13

    Post Please Help!

    Trouble with program

    I’m new to C and seem to be having some problem with my subtracting in “void process” can someone please give me some help?

    The problem I have been given to solve is –

    “A car hire company calculates the charges for the hire of a car using a standard rate for each mile travelled together with a “wear & tear” surcharge for each complete 1000 miles travelled. Provide a solution which accepts as input the milometer readings at the start and finish of a hire and calculates the total charge for the hire.

    Standard rate per mile = £00.42
    Surcharge per complete 1000 miles = £10.84”

    I’ve attached a picture of the results I get when I run the program also.

    Your help would be much appreciated

    Chris


    Code:
     
    //Car hire
    //Chris Chalmers
    //19/01/2009
     
    #include<conio.h>
    #include<stdio.h>
     
    int miles_before,miles_after;
    float rate_per_mile,surcharge,total;
     
    void input()
    {
        printf("please enter the miles on the clock AFTER rental.");
        scanf("%i",&miles_after);
        printf("Please enter the miles on the clock BEFORE rental.");     
        scanf("%i",&miles_before);
    }
     
    void process()
    {
        total=miles_after - miles_before*0.42;
        if (rate_per_mile>1000);
        {
        total+10.84;
        }
     
    }
     
    void output()
    {
        printf("\nThe total charge for your rental is %.2f",total);
    }
     
    int main()
    {
    //clrscr();
    input();
    process();
    output();
    getch();
    return 0;
    }
    Last edited by Salem; 01-21-2009 at 11:05 AM. Reason: Next time, less font/size abuse please

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Computer arithmetic generally follows the rules of precedence you hopefully remember from grade school. Try:
    Code:
    total=(float)(miles_after - miles_before)*0.42f;
    Notice (the parantheses). You might also want/need to include the cast (float) since miles_after, miles_before are not floating numbers, and the final f.
    Last edited by MK27; 01-21-2009 at 10:10 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    You probably want to change this line in your process() function.

    Code:
    total+10.84;

    If you're adding 10.84 to the total then

    Code:
    total=total+10.84;
    or equivalent

    Code:
    total+=10.84;

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    13

    Smile

    Thanks MK27! I think I understand what you mean … I know it as BODMAS (Brackets. Of. Divide. Multiply .Add .Subtract) I completely forgot about the whole concept. Currently doing a HNC at college and I’ve never been great with programming so this probably won’t be the last time I’m on here!

    Thanks freevryheid! If you hadn’t mention that my if statement wouldn’t have worked properly either. I would have probably sat at the computer with a blank look on my face for another half an hour!

    Thanks very much guys!

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    13

    Thumbs down Stuck once again

    Like I said yesterday I’m pretty new to C. I’m currently trying to write a program which involves the user entering some characters. I think for the char function you have to set an array? Because it only holds one character at a time? But it doesn’t seem to work when I do it, everything goes all funny after I enter my first line of text.

    This is how I set my arrays or attempted to.

    Code:
    char surname[25],name[25];
    int  age,years_worked,retire;
    I dunno I think I’m missing something. Like there is more to it than that?

    Anyways here is the full program minus my attempt at the arrays

    Code:
    //Retirement Program
    //Chris Chalmers 22/1/2009
    
    #include<stdio.h>
    #include<conio.h>
    
    char surname,name;
    int  age,years_worked,retire;
    
    void input()
    
    {
         printf("\nPlease enter your christian name.");
         scanf("%c",&name);
         
         printf("\nPlease enter your surname.");
         scanf("%c",&surname);
         
         printf("\nPlease enter your age.");
         scanf("%i",&age);
         
         printf("\nPlease enter the amount of year worked.");
         scanf("%i",&years_worked);
    }
    
    void process()
    
    {
     
         retire=(65-years_worked);
    
    }
    
    void output()
    
    {
    
         printf("Christian Name\n",name);
         printf("Surname\n",surname);
         printf("Age\n",age);
         printf("Years worked\n",years_worked);
         printf("You will retire in\n",retire);
    
    }
    
    int main()
    {
    input();
    process();
    output();
    getch();
    return 0;
    }
    I dunno I’m finding this so frustrating because I’m beginning to get to grips with the basics and I always forget something so simple!

    Anyways once again your help would be much aprreicated.

    Chris

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    % reads a character. Most people have more than one letter in their names.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    13
    So this instead?

    Code:
    printf("\nPlease enter your christian name.");
         scanf("c",&name);
    Also would I set my arrays at the top like this?

    Quote Originally Posted by Chalmers86 View Post

    Code:
    char surname[25],name[25];
    int  age,years_worked,retire;

  8. #8
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    No, not "c" --- use this --- %s for string which is what the name is (a string).Just replace the "%c" with "%s"

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    However, a direct replacement of %c with %s will create a buffer overflow loophole. If name and surname are arrays of 25 characters, then they have 24 characters each for the actual name data, hence you would write for name:
    Code:
    scanf("%24s", name);
    Note that the address of name is not taken since it will be implicitly converted to a pointer to the first char to be filled in with the data read.
    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

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    13

    Smile Thanks!

    Thanks again guys! That's absolutely brilliant, I feel I am actually really learning stuff as well because you guys are just giving me pointers instead of writing the whole code for me

Popular pages Recent additions subscribe to a feed