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