0

I copied the following code exactly as it was printed out in Miller's C Programming Absolute Beginner's Guide.

Everything works fine until it gets to the 4th scanf where it's supposed to ask for a date. The program stop working and the following error pops up:

enter image description here

// Example program #2 from Chapter 8 of Absolute Beginner's Guide to
// C, 3rd Edition
// File Chapter8ex2.c

/* This is a sample program that asks users for some basic data and prints it on
screen in order to show what was entered */

#include <stdio.h>

main()
{
    char topping[24];
    int slices;
    int month, day, year;
    float cost;

// The first scanf will look for a floating-point variable, the cost
// of a pizza
// If the user doesn't enter a $ before the cost, it could cause
// problems

    printf("How much does a pizza cost in your area?");
    printf("enter as $XX.XX)\n");
    scanf(" $%f", &cost);

// The pizza topping is a string, so your scanf doesn't need an &

    printf("What is your favorite one-word pizza topping?\n");
    scanf(" %s", topping);

    printf("How many slices of %s pizza", topping);
    printf("can you eat in one sitting?\n");
    scanf(" %d", slices);

    printf("What is today's date (enter it in XX/XX/XX format).\n");
    scanf(" %d/%d/%d", &month, &day, &year);

    printf("\n\nWhy not treat yourself to dinner on %d/%d/%d", month, day, year);
    printf("\nand have %d slices of %s pizza!\n", slices, topping);
    printf("It will only cost you $%.2f!\n\n\n", cost);

    return (0);
    }

  • 2
    *I copied the following code exactly as it was printed*. Probably not. `scanf(" %d", slices);` should be `scanf(" %d", &slices);` – kaylum Dec 12 '20 at 04:07
  • Also, that prototype for `main` is invalid. (I do hope that wasn't in the book!) See [here](https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function) for the acceptable prototypes. – costaparas Dec 12 '20 at 04:08
  • It's time to turn on more warnings, like *all* of them: `-Wall`. This will help spot simple, easily overlooked mistakes. – tadman Dec 12 '20 at 04:53
  • Wow, thanks. What an oversight, I thought everything was fine 'cause there were no errors during compilation. – Josuer L. Bague Dec 12 '20 at 05:06

1 Answers1

1

You forgot to convert some parameters to pointers. Replace

    printf("How many slices of %s pizza", topping);
    printf("can you eat in one sitting?\n");
    scanf(" %d", slices);

with

    printf("How many slices of %s pizza", topping);
    printf("can you eat in one sitting?\n");
    scanf(" %d", &slices);
Ilya Maximov
  • 149
  • 1
  • 8