-2

I'm having a problem with a C program crashing upon the first user input. It should be right, I think; the problem I'm having is that in runs properly with no errors given, but as soon as you give the first input, it just immediately crashes. Any ideas?

# define DOUGH_PER_SQFT 0.75
# define INCHES_PER_FEET 12
# define PI 3.141592654

# include <stdio.h>
# include <math.h>

int main() {

    // Get user inputs.

    int small, medium, large;

    printf("What is the radius of your small pizza, in inches?\n");
    scanf("%d", small);
    printf("What is the radius of your medium pizza, in inches?\n");
    scanf("%d", medium);
    printf("What is the radius of your large pizza, in inches?\n");
    scanf("%d", large);

    int smallfeet, mediumfeet, largefeet;

    smallfeet = ("%d/12", small);
    mediumfeet = ("%d/12", medium);
    largefeet = ("%d/12", large);

    // Find the amount of dough, in pounds, that will need to be ordered

    int surface_small, surface_medium, surface_large;

    surface_small = (PI*pow(smallfeet,2));
    surface_medium = (PI*pow(mediumfeet,2));
    surface_large = (PI*pow(largefeet,2));

    int smallweight, medweight, largeweight, doughneeded;

    smallweight = surface_small*DOUGH_PER_SQFT;
    medweight = surface_medium*DOUGH_PER_SQFT;
    largeweight = surface_large*DOUGH_PER_SQFT;

    int dough_for_smalls, dough_for_mediums, dough_for_larges, dough_needed;

    dough_for_smalls = smallweight*small;
    dough_for_mediums = medweight*medium;
    dough_for_larges = largeweight*large;
    dough_needed = dough_for_smalls + dough_for_mediums + dough_for_larges;

    // Find the answer.

    printf("You will need to buy ",dough_needed,"pounds of dough for this week");

    return 0;

}
lurker
  • 56,987
  • 9
  • 69
  • 103
  • 1
    What's the purpose behind statements like, ` smallfeet = ("%d/12", small)`? And `printf("You will need to buy ",dough_needed,"pounds of dough for this week");` will not print the value of `dough_needed`. You really should read a C book or tutorial. – lurker Feb 14 '15 at 04:03
  • I think you would benefit if you sat down and read [K&R](https://en.wikipedia.org/wiki/The_C_Programming_Language). It’s short and easy to read. – yellowantphil Feb 14 '15 at 04:14

2 Answers2

4

You need to pass the address of the variable for scanf

scanf("%d", &small);

Similarly others

P0W
  • 46,614
  • 9
  • 72
  • 119
  • 2
    Wow. I really suck. You're the best – Jordan Barden Feb 14 '15 at 04:00
  • One last thing - at this point, it's taking the user inputs for size, but then performing no calculations. It's going straight to the final printf, but since it didn't perform any calculations, it's just blank. – Jordan Barden Feb 14 '15 at 04:03
  • @JordanBarden Study about [comma operator](http://stackoverflow.com/q/52550/1870232), every places you're using it wrongly. Turn on warnings while compiling. – P0W Feb 14 '15 at 04:08
1

Change

scanf("%d", small); //and
scanf("%d", medium); //and
scanf("%d", large);

To

scanf("%d", &small); //and
scanf("%d", &medium); //and
scanf("%d", &large);

This is done because the %d format specifier in scanf expects an int*, but you provide an int which caused the crash.

There are also other problems in the code. To fix them, change

smallfeet = ("%d/12", small);
mediumfeet = ("%d/12", medium);
largefeet = ("%d/12", large);

To

smallfeet = small/12;
mediumfeet = medium/12";
largefeet = large/12;

And

printf("You will need to buy ",dough_needed,"pounds of dough for this week");

To

printf("You will need to buy %d pounds of dough for this week",dough_needed);
Spikatrix
  • 20,225
  • 7
  • 37
  • 83