I created an article previously doing this program in C++, but now I am trying to convert it to C.

#include<stdio.h>
double hamCalc(double b, double p);
double cheeseCalc(double b, double p, double c);
double dcheeseCalc(double b, double p, double c);

int main()
{
double patty, bun, cheese;
printf("This is the C version.\n\n");

scanf("Enter the price for each patty: %d", &patty);
scanf("Enter the price for each bun: %d", &bun);
scanf("Enter the price for each cheese slice: %d", &cheese);

double val1 = hamCalc(bun, patty);
double val2 = cheeseCalc(bun, patty, cheese);
double val3 = dcheeseCalc(bun, patty, cheese);

printf("Hamburger price: %.2f", val1);
printf("Cheeseburger price: %.2f", val2);
printf("Double Cheeseburger Price: %.2f", val3);


}

double hamCalc(double b, double p)
{
    double total = b + p;
    double total2 = total / 2;
    total = total + total2;
    return total;
}

double cheeseCalc(double b, double p, double c)
{
    double total = b + p + c;
    double total2 = total / 2;
    total = total + total2;
    return total;
}

double dcheeseCalc(double b, double p, double c)
{
    double total = b + p + p + c + c;
    double total2 = total / 2;
    total = total + total2;
    return total;
}

I compile it by
gcc -c burger.c

Now, my first question is, do I try and run the 'burger.c' file or do i compile the created 'burger.o' file because when I try the .o file it says 'cannot execute binary file'.

However, when I run the 'burger.c' file, I get an error saying:
./burger.c: line 2: syntax error near unexpected token (' ./burger.c: line 2:double hamCalc(double b, double p);'

Anyone know whats up? Thanks in advance. I plan on splitting these functions later on into separate files using a makefile but wanted to debug before everything is in separate places.

Recommended Answers

All 11 Replies

Now, my first question is, do I try and run the 'burger.c' file or do i compile the created 'burger.o' file because when I try the .o file it says 'cannot execute binary file'.

you don't do either. You can only run the executable. Add the -o filename to the gcc line. If -o is missing gcc will default the filename to a.out So in your example run ./a.out

Thank you. I figured out the proper way of running and the syntax error disappears. My next question is about user input. Since this isn't c++, it isn't just a simple cin.

I used an example from here:
http://www.cprogramming.com/tutorial/c/lesson1.html

But it doesn't work when I write this:

  8 double patty, bun, cheese;
  9 printf("This is the C version.\n\n");
 10 
 11 printf("Enter the price for each patty: ");
 12 scanf("%f", &patty);
 13 
 14 printf("Enter the price for each bun: ");
 15 scanf("%f", &bun);
 16 
 17 printf("Enter the price for each cheese slice: ");
 18 scanf("%f", &cheese);

Do the variables I created on line 8 (line1 on code snippet) automatically have the value of &patty, &bun, and &cheese because my debugging revealed that the variables are still set to 0.00000.

Your question doesn't make any sense. The variables you declared on line 8 have a value of whatever happened to be at the memory addresses, it could be 0 but it could also be any random value. That's not a bad thing because the program changes the values by calling scanf(). My only suggestion here is to use "%lf" instead of "%f" because %f if for floats and %lf for doubles.

Thanks again for your help. My problem was that I used %f instead of %lf. My last question is: when I try to separate the functions into separate files and run it, my output numbers are extremely large. This is what appears on my screen:

~/Chapter10/Assign10 $./program
This is the C version.

Enter the price for each patty: .25
Enter the price for each bun: .10
Enter the price for each cheese slice: .20

Grabbed Values: 0.250000, 0.100000, 0.200000

Hamburger price: $1070596096.00
Cheeseburger price: $1070176665.00
Double Cheeseburger Price: $1070176665.00

Obviously, the prices aren't supposed to be calculated that large. Maybe it has something to do with my make file?

  1 prog1_func1.o:prog1_func1.c
  2         gcc -c prog1_func1.c
  3 prog1_func2.o:prog1_func2.c
  4         gcc -c prog1_func2.c
  5 prog1_func3.o:prog1_func3.c
  6         gcc -c prog1_func3.c
  7 prog1_main.o:prog1_main.c
  8         gcc -c prog1_main.c
  9 program: prog1_func1.o prog1_func2.o prog1_func3.o prog1_main.o
 10         gcc prog1_func1.o prog1_func2.o prog1_func3.o prog1_main.o -o progra    m

main = main()
func1 = hamCalc()
func2 = cheeseCalc()
func3 = dcheeseCalc()

I'll also post my individual function files:

prog1_main.c:

  1 #include<stdio.h>
  2 
  3 int main()
  4 {
  5 double patty, bun, cheese;
  6 printf("This is the C version.\n\n");
  7 
  8 printf("Enter the price for each patty: ");
  9 scanf("%lf", &patty);
 10 
 11 printf("Enter the price for each bun: ");
 12 scanf("%lf", &bun);
 13 
 14 printf("Enter the price for each cheese slice: ");
 15 scanf("%lf", &cheese);
 16 
 17 printf("%lf, %lf, %lf", patty, bun, cheese);
 18 double val1 = hamCalc(bun, patty);
 19 double val2 = cheeseCalc(bun, patty, cheese);
 20 double val3 = dcheeseCalc(bun, patty, cheese);
 21 
 22 printf("\nHamburger price: $%.2lf", val1);
 23 printf("\nCheeseburger price: $%.2lf", val2);
 24 printf("\nDouble Cheeseburger Price: $%.2f", val3);
 25 
 26 printf("\n\nProgrammer: Daniel Ansher\n");
 27 
 28 }

prog1_func1:

  1 double hamCalc(double b, double p)
  2 {
  3         double total = b + p;
  4         double total2 = total / 2;
  5         total = total + total2;
  6         return total;
  7 }

and so on for the other two functions.
Any ideas?

I figured out that val1, val2, and val3 are not getting the proper values. Any ideas why?

11 double patty, bun, cheese;
 12 printf("This is the C version.\n\n");
 13 
 14 //grabs user input for prices
 15 printf("Enter the price for each patty: ");
 16 scanf("%lf", &patty);
 17 
 18 printf("Enter the price for each bun: ");
 19 scanf("%lf", &bun);
 20 
 21 printf("Enter the price for each cheese slice: ");
 22 scanf("%lf", &cheese);
 23 
 24 //calculates necessary numbers to collect 50% profit of goods.
 25 double val1 = hamCalc(bun, patty);
 26 double val2 = cheeseCalc(bun, patty, cheese);
 27 double val3 = dcheeseCalc(bun, patty, cheese);
 28 
 29 //prints out values to the user
 30 printf("\nHamburger price: $%.2lf", val1);
 31 printf("\nCheeseburger price: $%.2lf", val2);
 32 printf("\nDouble Cheeseburger Price: $%.2f", val3);

put some print statements in the functions so that you can see what they are calculating. OR learn to use your compiler's debugger.

I printed the contents of each individual function and each one returns the proper total. When the total is returned, the value gets messed up and the value is val1, val2, and val3 become huge numbers that don't make sense.

Can you either post the entire program, or PM it to me so that I can test it?

I have attached the zip file. There are two folders. One with the program in separate files and one with all together. The program in the together folder WORKS. The program in the separate is where I'm having issues.

Thanks for your help. I really appreciate it.

In the program that is in separate files you have to prototype the thee functions that return doubles because the compiler doesn't know what they return and assume they return an int, which is not correct.

Put the three prototypes before main()

double hamCalc(double,double);

Solved, thanks again! Have a great night.

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.