'calculating the amount of cupcakes per person

#include <stdio.h>

int main()
{
    int c,peo,pie,rem_pie;
    pie = (int) c/peo;
    rem_pie = (int) c%peo;

    printf("Enter number of cupcakes:");
    scanf("%d",&c);
    printf("Enter number of people:");
    scanf("%d",&peo);
    printf("Everyone will get %d pieces.",&pie);
    printf("\nThere are %d remaining pieces.",&rem_pie);

}

After I put 21 as c and 5 as peo the answer just messed up and I don't know which part I do wrong.

This is the result I got from it:

enter image description here

c


Solution 1:[1]

So hoping this clears things up:

#include <stdio.h>

int main()
{
  // None of those variable habe any value jet.
  // They are filled with what ever random garbage was in memory at this point.
  // BAD int c, peo, pie, rem_pie;
  // If you want to make sure they are properly filled try to give them     an initial value:
  int c = 0,
  peo = 0,
  pie = 0,
  rem_pie = 0;

  // If you would calculate with them now nothing good would come out of it.
  // Because now they are either still zero or filled with crap like in your case
  // BAD pie = (int) c/peo;
  // BAD rem_pie = (int) c%peo;

  // Let the user fill them first

  printf("Enter number of cupcakes: "); // Lets also put a space between output ent input
  scanf("%d", &c);
  printf("Enter number of people: "); // same here
  scanf("%d", &peo);
  // In those cases you are putting an & befor the values to give the address in memory to the scanf function
  // that means you are not passing in the variable rather than passing in the location where to put the entered values.
  // keep this in mind for later when using printf

  // Now the variables have the right values and are ready for calculation
  // If you want to know the remaining cupcakes that means they shouldn't be split up.
  // So take them away first.
  // Example:
  // 5 cupcakes and 2 persons
  // Each person could eat 2 and a half cupcakes
  // No cupcakes left
  // But im guessing you would like to only give full cupcakes to each person
  // that means 1 cupcake left 2 for each person
  // the calculation is: (5 - 1) / 2
  // Or: (cupcakes - remaining_cupcakes)  / persons
  // You can calculate like this:

  rem_pie = c % peo;
  pie = (c - rem_pie) / peo;

  // Now when using printf you need to pass over the values
  // Not the variable memory address unlike using scanf
  // Thats means shouldn't put an & befor that.
  printf("Everyone will get %d pieces.\n", pie);
  printf("There are %d remaining pieces.\n", rem_pie);
  // Lets also put the line breaks at the end of each line to make the output a bit nicer ;)

  // Also your main function is returning an int so lets return an int zero to tell our parent programm that there are no errors.
  return 0;
}

Solution 2:[2]

You calculate before the initialization. c and peo do not have a proper value yet when you use them in your formula. Suggestion:

#include <stdio.h>

int main()
{
    int c,peo,pie,rem_pie;

    printf("Enter number of cupcakes:");
    if (scanf("%d", &c) != 1)
    {
        /* Handle the error. */
    }
    printf("Enter number of people:");
    if (scanf("%d", &peo) != 1)
    {
        /* Handle the error. */
    }
    pie = c/peo;
    rem_pie = c%peo;
    printf("Everyone will get %d pieces.",pie);
    printf("\nThere are %d remaining pieces.",rem_pie);

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phillip
Solution 2