More C Programming Help


Recommended Posts

Hi, this is Liz's (blim's) son, aka "son". I have another C programming question. I have to write a program that displays the reimbursement rates for gas used while driving so many miles. The rates that are used are:

0-10 miles = $.55 reimbursment per mile driven

11-100 miles = $.42 reimbursment per mile driven

101-500 miles = $.32 reimbursment per mile driven

501+ miles = $.15 reimbursment per mile driven

The miles traveled has to be written as an integer value, the reimbursement rate and the amount of reimbursement has to be written as doubles.

I cant use any conditional statements other than if or if-else. and I cant use any functions other than printf() or scanf().

This is an example as what the screen should look like:

Travel

# miles traveled 1576

Your reimbursement will be $236.40

The code that I have written so far is:

/* Travel.c by Aron 10/09/2005 */
/* This program computes the reimbursement
* rates for gas consumed while on the job
*/

#include <stdio.h>

int main ()
{
/* declaration */
int miles;
double rate, amount;


/* prompt and integer input */
printf("Travel\n\n\n");
printf("# miles traveled ");
scanf("%lf", &miles, &amount);

/* processing */
if (miles <= 10) rate = .55;
if (miles >= 11 && miles <= 100) rate = .42;
if (miles >= 101 && miles <= 500) rate = .32;
if (miles >= 501) rate = .15;

/* Formula for conversion */
amount = rate * miles;

/* Output to screen */
printf("\n\nYour reimbursement will be %lf\n");

return 0;
}

The program runs with no errors, but the problem that I'm getting is that when it runs it says your reimbursement will be 0.000000, so I'm assuming I'm just not putting the formula in the right spot, I just don't know where to put the formula. Any help would be appreciated.

Thank you

-Aron

Link to post
Share on other sites

I no next to nothing about c, but I noticed this. It seems your miles is getting set to 0, that is explaning why you formula is ouputting 0. I added an extra printf() to see the value of miles

  GNU nano 1.3.4               File: prog.c

/* Travel.c by Aron 10/09/2005 */
/* This program computes the reimbursement
* rates for gas consumed while on the job
*/

#include <stdio.h>

int main ()
{
/* declaration */
int miles;
float rate, amount;


/* prompt and integer input */
printf("Travel\n\n\n");
printf("# miles traveled ");
scanf("%lf", &miles, &amount);

/* processing */
if (miles <= 10) rate = .55;
if (miles >= 11 && miles <= 100) rate = .42;
if (miles >= 101 && miles <= 500) rate = .32;
if (miles >= 501) rate = .15;

/* Formula for conversion */
amount = rate * miles;

/* Output to screen */
printf("%d",miles);
printf("\n\nYour reimbursement will be %lf\n");

return 0;
}

it shows it being set to zero.

Link to post
Share on other sites

Yeah, you're right. I didn't need it in there. I was writing it as I go, was thinking I was going to use it at first and didn't think about deleting it until you said something. Good call

*edit*

Hmph, that's weird. I don't know why it works with float but not double...

Not sure if this is related, but I have an "int" value and a "double" value, but I only use the "double" conversion specifier (%lf). Should I use the int conversion specifier (%d) somewhere?

Edited by buckwheat88
Link to post
Share on other sites

The problem is with your IO function calls.

Make sure the format tags in the string and the arguments match up. there should be the same number of each and they should have corresponding types.

%i or %d int

%f float

%li long int

%lf long float or double

("%i %i %lf", intvar1, intvar2, doublevar1)

You might also consider using else to simplify the rate selection. You can do it with just 3 comparisons rather than 6. This probably isn't necessary though.

You can link ifs together like this.

if(test1) something1;
else if (test2) something2;
else if (test3) something3;
else something4;

If test1 is true, then something1 happens and the program jumps to the end of the block of ifs, otherwise test2 is evaluated, and if it is true, something2 happens, otherwise test3 is checked to see if something3 happens, finally if and only if none of the tests are true, something4 happens. So exactly one of the somethings happens.

Edited by Hai-Etlik
Link to post
Share on other sites

@ Hai-Etlik

I kinda understand what you're saying Hai-Etlik... I changed the "if" statements to "else if" statements, that worked fine, but the first part I'm kinda foggy on.

Are you saying I should add something similar to this "("%i %i %lf", intvar1, intvar2, doublevar1)" into my program, only using my values instead, for example "("%lf, %lf, %d", &rate, &amount, &miles)", and should I put that in the scanf() section?

@ shanenin

I think I do need the "amount" added in there, but I think I need something else too, because every time I run the program with that added, no matter what number I put in, I get "-47244603.000000"

Edited by buckwheat88
Link to post
Share on other sites

scanf() is used to get user input. so in your code scanf() only needs to get the input miles from the user. So scanf() should only be used with miles. the variables amount and rate are not inputed by the user.

Edited by shanenin
Link to post
Share on other sites

Sorry, my example wasn't really that clear. And it was JUST an example of the arguments you could pass to a printf or scanf, not necessarily ones that would be directly useful to you.

If you have three format tags in the string, then there should be three arguments afterward. If you have one format tag then there should be one argument afterward. And the types should match: "%i" or "%d" should have an int argument, "%lf %lf" should have two double arguments, "%i %lf" should have an int and then a double as arguments. This applies to both scanf and printf.

scanf("%i", &intvar)

printf("Two doubles: %lf %lf", doublevar1, doublevar2)

The important thing is that the tags and arguments match. In the scanf, the %i matches with int argument, and in the printf the two %lfs match with the two double arguments.

The else if thing is just an extra bit of refinement, don't worry about it until you have everything else working, and SAVE the working version separately before you try it.

Link to post
Share on other sites

So it's kinda like what shanenin was saying earlier, I should add "amount" to the end of the final "printf()". Earlier when I attempted that it gave me a weird number every time, now when I do that it's giving me 0.000000 again.

I also notice I don't have rate anywhere except for in my formula, should rate be in somewhere?

Link to post
Share on other sites

the idea to programming is to split it into pieces. you could try writing what you need to do with the code in regular words(pseudo code), then make actual code

I need to get user input

-what function does this

I need to use that user input to determine the rate

-you are doing that with your if-else statments

I need to get the value of amount

-you are doing that with you multiplication formula

I need to print the value of amount

- what function does that

just some ideas :-)

Edited by shanenin
Link to post
Share on other sites

I know this doesn't really have anything to do with the problem that we're working with now, but I fixed one minor problem that I was having. The problem was the number of decimal places for the final product. I was getting "0.000000", but I made the final floating point "$%.2lf" to make it say "$0.00" now.

And I think I'm going to go to bed and work on it some more during my breaks tommorrow (it's 2 am here, have to be up at 8 am). So good night, and thanks for your help.

Link to post
Share on other sites

Yes, shanenin was right, I was trying to get you to see WHY he was right.

You need to know WHY things are right if you are going to be able to write code without help. You can't figure out how to fix it if you don't know what is wrong.

Link to post
Share on other sites

Figured out the problem I was having. I understood that amount had to be in there, but the program still wasn't working. After comparing the 2 programs (the one using only float that worked, and my program that still didn't work) I finally realized my problem was here:

I originally had "scanf("%lf", &miles);" but miles was an integer value, not a floating point, so I had to change it to "scanf("%int", &miles);". Minor mistake that I just never noticed. Once I noticed it I was like "D'Oh! Of course!"

But anyways, thanks for the help.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...