# Thread: Beginner to C need help

1. ## Beginner to C need help

Hey guys just saw this forum today and joined.
I am new to C(and programming actually) soo please bear with me
I have to make a gas mileage calculator which should display the average miles per gallon after taking inputs from the user using ONLY the for loop(no break statements or anything). If the user presses -1 on the input where it says enter gallons then the program should terminate. Heres the code(I havent completed it ofc.. Im having trouble with the looping):
Code:
```#include<stdio.h>
#include<conio.h>
int main(void){
float gallon=0, mile=0, tgmile=0;
int lop=0, num=1;
printf("Enter gallons used(-1 to exit)");
scanf("%d", &gallon);
if(gallon==-1){

lop=-1;
}

for(lop;lop==-1;num++){

printf("\nEnter miles driven");
scanf("%d",&mile);
if(lop!=-1){
printf("\nEnter gallons used(-1 to exit)");
scanf("%d", &gallon);
gallon=lop;
}
}
return 0;
}```

The way Ive tried to do is to ask the input about gallons first before starting the loop and if the input is -1 then i should assign -1 to lop variable so the for loop doesnt execute. But if I give some number other than 1 to the first input it still doesnt execute the for loop. Any help? I have to use only the for loop no break statements or goto or anything like that
the num variable will be used for calculating the average and its assigned 1 since it started before the for loop and tgmile is for storing the complete average gallon per mile of EVERY input.
Basically it should continue asking for input of gallon and miles till the user presses -1 on the gallon input 2. Code:
`for(lop;lop==-1;num++)`
Your loop is saying while lop is -1 keep getting input. Perhaps you want to terminate the loop when lop does == -1; i.e. keep looping while lop != -1 3. A for loop is probably the wrong kind of loop. Especially since I don't see why num is useful. I would use a while loop. The only other thing I would do is make sure that the code that calculates is under an if statement, so if the user decides to quit, most of the loop won't do anything:

Code:
```while gallons not equal to -1:
prompt and get miles
prompt and get gallons
if gallons not equal to -1:
do calculation
end if
end while```
Try making those steps into code... 4. You need to use the specifier %f (for floats) in your scanf, not %d (which is for ints).
And get rid of conio.h since you're not using it. 5. well I think I fixed the problem by eliminating the lop variable completely to stop using redundant ifs and just using the gallon variable in the for loop and replacing the == in the for by >-1 heres the new code:
Code:
```#include<stdio.h>
#include<conio.h>
int main(void){
float gallon=0, mile=0, tgmile=0;
int num=1;
printf("Enter gallons used(-ve number to exit)");
scanf("%f", &gallon);
for(gallon;gallon>-1;num++){

printf("\nEnter miles driven");
scanf("%f",&mile);
printf("\nEnter gallons used(-ve number to exit)");
scanf("%f", &gallon);

}
return 0;
}```
its working now(and its not finshed ofc) and terminating for all numbers less than 0(but I want it to end somehow when I only press -1) Originally Posted by SirPrattlepod Code:
`for(lop;lop==-1;num++)`
Your loop is saying while lop is -1 keep getting input. Perhaps you want to terminate the loop when lop does == -1; i.e. keep looping while lop != -1
Oh yea I really have trouble with this sort of thing. I tried your code it does run for numbers greater than -1 but when i press -1 it still runs.. any reason why this is happening?

Im not supposed to use the while loop. Just for. Ive managed to make it work like this and it works but I just want it to terminate when I press -1 not terminate when its less than -1 6. Originally Posted by spiderslayerx but when i press -1 it still runs.. any reason why this is happening?

And get rid of the code that asks for gallons outside of the loop.
That's pointless.
Look again at whiteflags pseudocode and try to implement it. 7. Code:
```float gallon=0, mile=0, tgmile=0;
int lop=0, num=1;
printf("Enter gallons used(-1 to exit)");
scanf("%d", &gallon);```
%d is used to read integers. gallon is a float. They are represented totally differently, and what you've done here is write an integer value into an address occupied by a float. Because the representations are different, the value of gallon interpreted as a float won't resemble the number you entered. Use %f for float: printf - C++ Reference 8. Originally Posted by oogabooga And get rid of the code that asks for gallons outside of the loop.
That's pointless.
Look again at whiteflags pseudocode and try to implement it.
Ive put the gallons input outside of the loop because if the user presses -1 it should end immediately. If I put it insde the loop then press -1 then it would ask for miles and then end the program. I have to end the program as soon as -1 is pressed on gallons wihtout taking the miles input wihtout using break statements or goto Originally Posted by smokeyangel Code:
```float gallon=0, mile=0, tgmile=0;
int lop=0, num=1;
printf("Enter gallons used(-1 to exit)");
scanf("%d", &gallon);```
%d is used to read integers. gallon is a float. They are represented totally differently, and what you've done here is write an integer value into an address occupied by a float. Because the representations are different, the value of gallon interpreted as a float won't resemble the number you entered. Use %f for float: printf - C++ Reference
yup I think this is my problem right here. 9. So now you've got the %f thing sorted, doesn't it work if you just do this?

Code:
`for(gallon;gallon != -1;num++){` 10. Originally Posted by smokeyangel So now you've got the %f thing sorted, doesn't it work if you just do this?

Code:
`for(gallon;gallon != -1;num++){`
yes I finally got it working like I want thanks all

Now I got something to ask.. if you enter a decimal value in a float variable a(like 1.5 or something) and store it in an int b, will only 1 be stored?(assuming a is 1.5)

Also if I input like a 1(not 1.0000 or something similar) in a float variable.. will it be stored as 1.00000.... or something like 0.99999? 11. Originally Posted by spiderslayerx Ive put the gallons input outside of the loop because if the user presses -1 it should end immediately. If I put it insde the loop then press -1 then it would ask for miles and then end the program. I have to end the program as soon as -1 is pressed on gallons wihtout taking the miles input wihtout using break statements or goto
Blah blah blah.
Did you even read whiteflags post?
What an idiot.... 12. Hmmmmmm 13. Originally Posted by spiderslayerx Now I got something to ask.. if you enter a decimal value in a float variable a(like 1.5 or something) and store it in an int b, will only 1 be stored?(assuming a is 1.5)
Yes - if you have a float and assign it to an int, the fractional part is thrown away. Even if it was 1.9999, it'd still be 1. Originally Posted by spiderslayerx Also if I input like a 1(not 1.0000 or something similar) in a float variable.. will it be stored as 1.00000.... or something like 0.99999?
It'll be stored precisely as 1.0000. Integers can be stored accurately in floats, up to a certain number (around 24 bits I think).
The problem comes as soon as any fractional parts are introduced. Just like one third can't be expressed precisely in decimals, some numbers can't be expressed precisely in binary floating point. For instance 0.11 will be stored as 0.10999999 14. Whoa, can we not call people idiots? That's a little harsh and doesn't seem to be constructive in a place that is intended for beginners.

Honestly, if you're good at programming then what are you doing here? This forum has no use to someone who is advanced so calling anyone an 'idiot' to me is a grievous offense.

As the King of the Beasts and the inheritor of the Yawarakai-Te I have spoken and I dismiss you of all credibility, ooga. As far as I'm concerned, you're of no use and are hindering the sentient evolution of the human race so it's either go with the flow or drown. 15. Originally Posted by spiderslayerx yes I finally got it working like I want thanks all

Now I got something to ask.. if you enter a decimal value in a float variable a(like 1.5 or something) and store it in an int b, will only 1 be stored?(assuming a is 1.5)

Also if I input like a 1(not 1.0000 or something similar) in a float variable.. will it be stored as 1.00000.... or something like 0.99999?
Hi, Popular pages Recent additions 