| | |
Questions on a simple C program
![]() |
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
//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;
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?
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?
Last edited by tuse; Jul 11th, 2009 at 6:15 pm.
My blog on .NET- http://dotnet.tekyt.info
And another addition: all C keywords (and function names from the standard function library) are written in lowercase.
As C is case-sensitive,
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!)
As C is case-sensitive,
Return 0; isn't valid, and has to be written like return 0; 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!)
Last edited by tux4life; Jul 11th, 2009 at 6:53 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
BTW, not only missing semicolons in your code, also wrong formatting specifiers:
You declared the following variables as of type 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:
Remember: The printf and scanf functions always need to know exactly what type of data they have to deal with.
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("%f",fuelr * fuel); (if you declare your variables to be of a floating point type), or printf("%d",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.
Last edited by tux4life; Jul 11th, 2009 at 7:05 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
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
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
C Syntax (Toggle Plain Text)
//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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
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 =]
C Syntax (Toggle Plain Text)
//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:
BTW, things like this in your code:
My suggestion to fix the undefined behaviour: remove every %d format specifier which is at the end of the string you print using printf.
printf("Trip Summary.%d"); // <--
printf("Fuel Required.%d", miles / mpg);
printf("Enter Total amount of fuel required.%d"); // <--BTW, things like this in your code:
printf("Enter Price of Fuel per gallon.%d"); 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.
Last edited by tux4life; Jul 11th, 2009 at 8:10 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Jul 2009
Posts: 9
Reputation:
Solved Threads: 0
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:
C Syntax (Toggle Plain Text)
//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:
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.
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.
![]() |
Similar Threads
- Mystery with getline, very simple program (C++)
- help!Simple Program on Grades (Java)
- Porting simple program from linux to minix (Kernels and Modules)
- Help with Simple C program :) (C)
- Can someone help me develop a simple program (Visual Basic 4 / 5 / 6)
- can anyone help me develop a simple program (Visual Basic 4 / 5 / 6)
- need help for a C++ simple program. thx very mcuh!! (Community Introductions)
- Need advice & help with a very simple program (IT Professionals' Lounge)
- Erroneus outputs in string manipulation simple program (C++)
- I am a girl doing my 1st simple program (C)
Other Threads in the C Forum
- Previous Thread: my code
- Next Thread: can anyone explain me how dis code is working
| Thread Tools | Search this Thread |
adobe api array arrays binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators intmain() iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc program programming pyramidusingturboccodes read recursion recv recvblocked repetition research scanf scheduling segmentationfault send shape socketprograming socketprogramming stack standard strchr string suggestions systemcall test unix urboc user variable voidmain() wab win32api windows.h






