Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

my program is not reading sccanf function properly

+1 vote
asked Nov 6, 2020 by Ajit Kumar (130 points)
#include<stdio.h>

    main()  {

   char name[30];

   char size;

   int personItServes;

   float price;

printf("enter pizza name: ");

scanf(" %[^\n]",name);  

printf("enter size of pizza as either S or M or L ");

     size=getchar(); // scanf("%c",&size);                                                                 

printf("enter the number of person you wanna serve ");

scanf("%d",&personItServes);

printf("what is your budget ");

scanf("%f",&price);

printf("congratulations your order has been placed for %c of size %C and it will serve %d person. kindly pay %f",name,size,personItServes,price);

}

2 Answers

0 votes
answered Nov 7, 2020 by xDELLx (10,520 points)
edited Nov 7, 2020 by xDELLx

Found below from the man page for scanf ,looks like we need to add an extra space .before the format when scanning for characters.Changing this in the code makes the code work correct.


[ c ]--> Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, and

 there  must  be enough room for all the characters (no terminating null byte is added).  The usual skip of leading white space is suppressed.  To skip white space first, use an explicit space in the format.


Changes:

     //size=getchar(); // Commented getchar func call

     scanf(" %c",&size);    //Added space before the %c in the format specifier

Also last printf in the code requires a small change to print pizza name ,please check

0 votes
answered Nov 7, 2020 by LiOS (6,420 points)
As xDELLx correctly mentioned, there is a need for a space before the %c in the format specifier within the scanf.

Some ideas to improve your code is to consider how you take the user input (what if the user enters greater than 30 characters etc). Also, perhaps check the user has entered s, m or l (case insensitive).

There's a few other ideas but the above is a great start!

Below is code with space in scanf and %s in final printf in order to print the pizza name rather than the first character:

#include<stdio.h>

void main(){
    
    char name[30];
    char size;
    
    int personItServes;
    float price;
    
    printf("enter pizza name: ");
    scanf(" %[^\n]",name);  
    
    
    printf("enter size of pizza as either S or M or L ");
    //size = getchar();
    scanf(" %c",&size);                                                                 
    
    printf("enter the number of person you wanna serve ");
    scanf("%d",&personItServes);
    
    printf("what is your budget ");
    scanf("%f",&price);
    
    printf("congratulations your order has been placed for %s of size %C and it will serve %d person. kindly pay %f",name,size,personItServes,price);

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...