Questions on a simple C program

Reply

Join Date: Jul 2009
Posts: 9
Reputation: makavelixx is an unknown quantity at this point 
Solved Threads: 0
makavelixx makavelixx is offline Offline
Newbie Poster

Questions on a simple C program

 
0
  #1
Jul 11th, 2009
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

  1. //Name: Justin Hooker
  2. //Assignment: Lab 2
  3. //Date Written: July 10th 09
  4. //Course: CS133U
  5. //Program: trip.c
  6. //Purpose: Trip Planner
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. int main()
  11.  
  12. {
  13.  
  14. int fuel, mpg, miles, fuelr;
  15.  
  16. printf("Enter Price of Fuel per gallon.");
  17. scanf("%f",&fuel);
  18. printf("Enter your cars average miles per gallon.");
  19. scanf("%f",&mpg);
  20. printf("Enter Total number of miles for the trip.");
  21. scanf("%f",&miles);
  22.  
  23. printf("Trip Summary")
  24. printf("Fuel Required %.2f", miles / mpg);
  25. printf("Enter Total amount of fuel required")
  26. scanf("%f",&fuelr);
  27. printf(fuelr * fuel);
  28. }
  29. Return 0;
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 169
Reputation: tuse is an unknown quantity at this point 
Solved Threads: 14
tuse's Avatar
tuse tuse is offline Offline
Junior Poster

Re: Questions on a simple C program

 
0
  #2
Jul 11th, 2009
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?
Last edited by tuse; Jul 11th, 2009 at 6:15 pm.
My blog on .NET- http://dotnet.tekyt.info
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Questions on a simple C program

 
0
  #3
Jul 11th, 2009
And another addition: all C keywords (and function names from the standard function library) are written 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."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Questions on a simple C program

 
0
  #4
Jul 11th, 2009
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("%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."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: makavelixx is an unknown quantity at this point 
Solved Threads: 0
makavelixx makavelixx is offline Offline
Newbie Poster

Re: Questions on a simple C program

 
0
  #5
Jul 11th, 2009
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
  1. //Name: Justin Hooker
  2. //Assignment: Lab 2
  3. //Date Written: July 10th 09
  4. //Course: CS133U
  5. //Program: trip.c
  6. //Purpose: Trip Planner
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #define terminal -9
  11. #define MAX_SIZE 15
  12. int main(void)
  13.  
  14.  
  15. {
  16.  
  17. int fuel, mpg, miles, fuelr;
  18.  
  19. printf("Enter Price of Fuel per gallon.");
  20. scanf("%f",&fuel);
  21. printf("Enter your cars average miles per gallon.");
  22. scanf("%f",&mpg);
  23. printf("Enter Total number of miles for the trip.");
  24. scanf("%f",&miles);
  25.  
  26. printf("Trip Summary\n")
  27. printf("Fuel Required%.2f", miles / mpg);
  28. printf("Enter Total amount of fuel required")
  29. scanf("%f",&fuelr);
  30. printf("Amount for fuel for trip.%.2f", fuelr * fuel);
  31.  
  32. return 0;
  33. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Questions on a simple C program

 
0
  #6
Jul 11th, 2009
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."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: makavelixx is an unknown quantity at this point 
Solved Threads: 0
makavelixx makavelixx is offline Offline
Newbie Poster

Re: Questions on a simple C program

 
0
  #7
Jul 11th, 2009
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 =]
  1. //Name: Justin Hooker
  2. //Assignment: Lab 2
  3. //Date Written: July 10th 09
  4. //Course: CS133U
  5. //Program: trip.c
  6. //Purpose: Trip Planner
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #define terminal -9
  11. #define MAX_SIZE 15
  12. int main(void)
  13.  
  14.  
  15. {
  16.  
  17. int fuel, mpg, miles, fuelr;
  18.  
  19. printf("Enter Price of Fuel per gallon.%d");
  20. scanf("%d",&fuel);
  21. printf("Enter your cars average miles per gallon.%d");
  22. scanf("%d",&mpg);
  23. printf("Enter Total number of miles for the trip.%d");
  24. scanf("%d",&miles);
  25.  
  26. printf("Trip Summary.%d")
  27. printf("Fuel Required.%d", miles / mpg);
  28. printf("Enter Total amount of fuel required.%d")
  29. scanf("%d",&fuelr);
  30. printf("Amount for fuel for trip.%d", fuelr * fuel);
  31.  
  32. return 0;
  33. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Questions on a simple C program

 
0
  #8
Jul 11th, 2009
Well, you forgot to add semicolons there:
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."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: makavelixx is an unknown quantity at this point 
Solved Threads: 0
makavelixx makavelixx is offline Offline
Newbie Poster

Re: Questions on a simple C program

 
0
  #9
Jul 11th, 2009
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:

  1. //Name: Justin Hooker
  2. //Assignment: Lab 2
  3. //Date Written: July 10th 09
  4. //Course: CS133U
  5. //Program: trip.c
  6. //Purpose: Trip Planner
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #define terminal -9
  11. #define MAX_SIZE 15
  12. int main(void)
  13.  
  14.  
  15. {
  16.  
  17. float fuel, mpg, miles, fuelr = 0;
  18.  
  19. printf("Enter Price of Fuel per gallon.");
  20. scanf("%f",&fuel);
  21. printf("Enter your cars average miles per gallon.");
  22. scanf("%f",&mpg);
  23. printf("Enter Total number of miles for the trip.");
  24. scanf("%f",&miles);
  25.  
  26. printf("Trip Summary.");
  27. printf("Fuel Required.", miles / mpg);
  28. printf("Enter Total amount of fuel required.");
  29. scanf("%f",&fuelr);
  30. printf("Amount for fuel for trip.");
  31. printf ("%f", fuelr * fuel);
  32.  
  33.  
  34. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Questions on a simple C program

 
0
  #10
Jul 11th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC