-4

I have been trying to solve the reason for my code being skipped when entering user input using scanf, however it only accepts the first two questions as answers from the user. No matter how I arrange the code, either the order of it, it still seems to skip the rest of the questions after the first 2 questions are answered. How do I solve this?

Please, instruct as much detail as possible I am a newbie.

Thank you,

Sincerely, some newbie teenager coding in C

#include <stdio.h>

int 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("What is your favorite pizza topping?\n") ;
    scanf(" %s", topping) ;
    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("How many slices of %s on the 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);
}
jett8998
  • 888
  • 2
  • 15
  • 1
    Welcome to SO. Please do not add pictures of code or links to pictures of code. Instead copy the code directly into your question. – Gerhardh Jul 06 '22 at 10:35
  • 1
    Please also provide your exact input and output together with expected output. – Gerhardh Jul 06 '22 at 10:35
  • 1
    Are you aware that you must enter `$`? – Gerhardh Jul 06 '22 at 10:40
  • You may want to read this: [Why not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel Jul 06 '22 at 10:54
  • @Palmtreesaway I can't see your code, but I suspect you're probably breaking one of the [secret unpublished rules of `scanf` usage](https://stackoverflow.com/questions/72178518#72178652). – Steve Summit Jul 06 '22 at 11:02
  • @jett8998: Did you type the entire code from the image yourself in order to submit your edit proposal? Or did you use some kind of [optical character recognition](https://en.wikipedia.org/wiki/Optical_character_recognition)? – Andreas Wenzel Jul 06 '22 at 11:15
  • @AndreasWenzel I used apple's internal OCR software but I checked it completely for grammar and usage differences. Didn't check for spaces etc. – jett8998 Jul 06 '22 at 11:37
  • @Palmtreesaway on macOS monteray with homebrew gcc-11&c11 it works perfectly. – jett8998 Jul 06 '22 at 11:38
  • 1
    You are supposed to respond to comments and questions. – Gerhardh Jul 06 '22 at 13:40

1 Answers1

0

On the line 19, the $ sign is causing the error remove that and you are good to go.

scanf("$%f", &cost); to scanf("%f", &cost);

Full code


#include <stdio.h>

int 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("What is your favorite pizza topping?\n");

scanf(" %s", topping);

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("How many slices of %s on the 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);

}

Shashank Deepak
  • 163
  • 1
  • 8