I am trying to figure out what I'm doing wrong with this code to keep getting errors, its a simple trip planner for a class I am taking however it will not compile and is giving me these errors:

C:\Documents and Settings\Justin\Desktop\trip.c In function `main':
24 C:\Documents and Settings\Justin\Desktop\trip.c syntax error before "printf"
26 C:\Documents and Settings\Justin\Desktop\trip.c syntax error before "scanf"
27 C:\Documents and Settings\Justin\Desktop\trip.c [Warning] passing arg 1 of `printf' makes pointer from integer without a cast
At top level:
29 C:\Documents and Settings\Justin\Desktop\trip.c syntax error before numeric constant

Here is my full code

//Name: Justin Hooker
//Assignment: Lab 2
//Date Written: July 10th 09
//Course: CS133U
//Program: trip.c
//Purpose: Trip Planner

#include <stdio.h>
#include <stdlib.h>
int main()

{

	int fuel, mpg, miles, fuelr;
	
	printf("Enter Price of Fuel per gallon.");
		scanf("%f",&fuel);
	printf("Enter your cars average miles per gallon.");
		scanf("%f",&mpg);
	printf("Enter Total number of miles for the trip.");
		scanf("%f",&miles);

	printf("Trip Summary")
	printf("Fuel Required %.2f", miles / mpg);
	printf("Enter Total amount of fuel required")
		scanf("%f",&fuelr);
	printf(fuelr * fuel);
}
Return 0;

Recommended Answers

All 13 Replies

printf("Trip Summary");

You are missing the semicolon here

and here

printf("Enter Total amount of fuel required");

and why do you have the return statement outside the main function?

And another addition: all C keywords (and function names from the standard function library) are written in lowercase.
As C is case-sensitive, [B]Return 0;[/B] isn't valid, and has to be written like [B]return 0;[/B] instead.
As already told you by tuse, you have to put a return statement inside a function's body, to fix your code, you take a look at tuse's post, and you move the return statement inside your main function.
(Take into account that you have to write it in lowercase!)

BTW, not only missing semicolons in your code, also wrong formatting specifiers:
You declared the following variables as of type integer: int fuel, mpg, miles, fuelr; , but in your scanfs, you tell the scanf function that you expect a floating point number, not an integer.
If your intention was getting a floating point number from the user, then you should declare your variables as of type float or double, otherwise you leave them all of type integer, and you change the formatting specifiers in all the scanfs from %f to %d.

Also you have to change: printf(fuelr * fuel); to printf([B]"%f",[/B]fuelr * fuel); (if you declare your variables to be of a floating point type), or printf([B]"%d",[/B]fuelr * fuel); (if you declare your variables to be of type integer).

Remember: The printf and scanf functions always need to know exactly what type of data they have to deal with.

So I've made as many changes as I can and am now only getting 3 error messages:

C:\Documents and Settings\Justin\Desktop\trip.c In function `main':
C:\Documents and Settings\Justin\Desktop\trip.c In function `main':
C:\Documents and Settings\Justin\Desktop\trip.c In function `main':

code is

//Name: Justin Hooker
//Assignment: Lab 2
//Date Written: July 10th 09
//Course: CS133U
//Program: trip.c
//Purpose: Trip Planner

#include <stdio.h>
#include <stdlib.h>
#define terminal -9
#define MAX_SIZE 15
int main(void)


{

	int fuel, mpg, miles, fuelr;
	
	printf("Enter Price of Fuel per gallon.");
		scanf("%f",&fuel);
	printf("Enter your cars average miles per gallon.");
		scanf("%f",&mpg);
	printf("Enter Total number of miles for the trip.");
		scanf("%f",&miles);

	printf("Trip Summary\n")
 	printf("Fuel Required%.2f", miles / mpg);
	printf("Enter Total amount of fuel required")
		scanf("%f",&fuelr);
	printf("Amount for fuel for trip.%.2f", fuelr * fuel);

return 0;
}

Well, I guess you haven't seen my previous post, because you were writing your post at the same moment as I was writing it, so I would suggest you to check my previous post out, and fix the errors in your program.

Not sure what to change but line 27 and 29 and main still giving me error codes here is what I currently have, I am learning this as we speak and trying to read up on it so bare with me =]

//Name: Justin Hooker
//Assignment: Lab 2
//Date Written: July 10th 09
//Course: CS133U
//Program: trip.c
//Purpose: Trip Planner

#include <stdio.h>
#include <stdlib.h>
#define terminal -9
#define MAX_SIZE 15
int main(void)


{

	int fuel, mpg, miles, fuelr;
	
	printf("Enter Price of Fuel per gallon.%d");
		scanf("%d",&fuel);
	printf("Enter your cars average miles per gallon.%d");
		scanf("%d",&mpg);
	printf("Enter Total number of miles for the trip.%d");
		scanf("%d",&miles);

	printf("Trip Summary.%d")
 	printf("Fuel Required.%d", miles / mpg);
	printf("Enter Total amount of fuel required.%d")
		scanf("%d",&fuelr);
	printf("Amount for fuel for trip.%d", fuelr * fuel);

return 0;
}

Well, you forgot to add semicolons there:

printf("Trip Summary.%d")[B];[/B] [B]// <--[/B]
printf("Fuel Required.%d", miles / mpg);
printf("Enter Total amount of fuel required.%d")[B];[/B] [B]// <--[/B]

BTW, things like this in your code: printf("Enter Price of Fuel per gallon.[B]%d[/B]"); will result in undefined behaviour, because for each format specifier you add, you also have to pass a corresponding value to printf.

My suggestion to fix the undefined behaviour: remove every %d format specifier which is at the end of the string you print using printf.

Finally got it to compile, but when I run the code with either integer or float format for the user to input it doesnt recognize the numbers and does not calculate like I want it to, either that or I'm completely confused and I'm just not coding it to show the numbers? anyways heres the code at its current state:

//Name: Justin Hooker
//Assignment: Lab 2
//Date Written: July 10th 09
//Course: CS133U
//Program: trip.c
//Purpose: Trip Planner

#include <stdio.h>
#include <stdlib.h>
#define terminal -9
#define MAX_SIZE 15
int main(void)


{

	float fuel, mpg, miles, fuelr = 0;
	
	printf("Enter Price of Fuel per gallon.");
		scanf("%f",&fuel);
	printf("Enter your cars average miles per gallon.");
		scanf("%f",&mpg);
	printf("Enter Total number of miles for the trip.");
		scanf("%f",&miles);

	printf("Trip Summary."); 
 	printf("Fuel Required.", miles / mpg);
	printf("Enter Total amount of fuel required.");
		scanf("%f",&fuelr);
	printf("Amount for fuel for trip.");
    printf ("%f", fuelr * fuel);


}

your last few lines, where you're presumably calculating and printing the "fuel required" doesnt make any sense.

youre supposed to calculate the fuel required, not prompt the user to enter the fuel required.

fuel required is calculated by "total miles / miles per gallon" ... you can print it like this: printf("Amount of Fuel Required: %f gallons\n", miles/mpg); and the Total Cost of Fuel Required is equal to the "total fuel required * cost per gallon", so you should now be able to figure this out.

Alright hopefully this will be my last question on this subject. Actually last two questions. I run my current script and it just closes before giving me any solution so I need it to not stop at the end, and I would like to make it if the user inputs something it will exit.
Current code:

//Name: Justin Hooker
//Assignment: Lab 2
//Date Written: July 10th 09
//Course: CS133U
//Program: trip.c
//Purpose: Trip Planner

#include <stdio.h>
#include <stdlib.h>
#define terminal -9
#define MAX_SIZE 15
int main(void)


{

	float fuel, mpg, miles  = 0;
	
	printf("Enter Price of Fuel per gallon.\n");
		scanf("%f",&fuel);
	printf("Enter your cars average miles per gallon.\n");
		scanf("%f",&mpg);
	printf("Enter Total number of miles for the trip.\n");
		scanf("%f",&miles);

	printf("Trip Summary.\n"); 
 	printf("%f Gallon(s) of Fuel Required.\n", miles / mpg);
	printf("Total cost of fuel for trip.\n");
    printf ("%f", miles / mpg * fuel);
return 0;
}

Alright inserted a system pause but how to I make it so it stops say if the user enters exit or a number i define?

Also how would I take this program and have it loop?

Add a do-while loop around the main portions of the code. At the end of the loop simply ask a question. Input the answer and the while conditional can exit the loop or not.

But, you should read this series about scanf() first.

And you might as well read this, too.

So finally this guy has COMPLETED his home work.
congrates...

commented: what's your point? -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.