943,796 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 983
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 11th, 2009
0

Questions on a simple C program

Expand Post »
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;
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
makavelixx is offline Offline
9 posts
since Jul 2009
Jul 11th, 2009
0

Re: Questions on a simple C program

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.
Reputation Points: 32
Solved Threads: 14
Junior Poster
tuse is offline Offline
173 posts
since Jul 2007
Jul 11th, 2009
0

Re: Questions on a simple C program

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 11th, 2009
0

Re: Questions on a simple C program

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 11th, 2009
0

Re: Questions on a simple C program

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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
makavelixx is offline Offline
9 posts
since Jul 2009
Jul 11th, 2009
0

Re: Questions on a simple C program

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 11th, 2009
0

Re: Questions on a simple C 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 =]
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
makavelixx is offline Offline
9 posts
since Jul 2009
Jul 11th, 2009
0

Re: Questions on a simple C program

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 11th, 2009
0

Re: Questions on a simple C program

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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
makavelixx is offline Offline
9 posts
since Jul 2009
Jul 11th, 2009
0

Re: Questions on a simple C program

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.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: my code
Next Thread in C Forum Timeline: can anyone explain me how dis code is working





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC