1

This is just a basic program. We will enter the name of the book(1 char name), price(float) and the number of pages(int) ant output should be the same as input, but it's not.

#include<stdio.h>

int main()
{
    struct book
    {
        char name;
        float price;
        int pages;
    };

    struct book b1,b2,b3;
    printf("Enter names,prices & no. of pages of 3 books\n");
    scanf("%c %f %d",&b1.name,&b1.price,&b1.pages);
    scanf("%c %f %d",&b2.name,&b2.price,&b2.pages);
    scanf("%c %f %d",&b3.name,&b3.price,&b3.pages);
    printf("And this is what you entered\n");
    printf("%c %f %d\n",b1.name,b1.price,b1.pages);
    printf("%c %f %d\n",b2.name,b2.price,b2.pages);
    printf("%c %f %d\n",b3.name,b3.price,b3.pages);
    return 0;
}

enter image description here

UTPAL
  • 31
  • 11
  • 1
    Forgive the self-promotion, but my answer [here](https://stackoverflow.com/a/59790401/10871073) addresses your problem. – Adrian Mole Apr 13 '20 at 15:08
  • 1
    Here's a link to a 'canonical' duplicate: https://stackoverflow.com/q/5240789/10871073 – Adrian Mole Apr 13 '20 at 15:10
  • 1
    As a quick fix, you can add a space at the beginning of each of your `scanf` formats, like this: `scanf(" %c %f %d", &b1.name, &b1.price, &b1.pages);` - this will skip any 'left-over' newlines from previous calls. – Adrian Mole Apr 13 '20 at 15:25
  • Please do not add pictures of your output if it is plain text. Copy&paste it as text instead. – Gerhardh Apr 13 '20 at 16:04
  • Understood. I just had to add space. – UTPAL Apr 13 '20 at 16:52

2 Answers2

1

This is what you've entered. Floating points are more of approximations than actual values. That's why when you've entered 123.134, computer rounded it to a nearest possible value, 123.124003. It works for engineering, graphic and statistics, where such minor disrepancies are rounded before presenting back to humans. But when you want exact values, floating point variables are completely unsuitable.

In finances, we store 10 dollars as "1000 cents", in ints. Trying to use float or double for monetary values is a big no-no.

What you want is a decimal type, but there is no such type in C.

Agent_L
  • 4,960
  • 28
  • 30
  • 1
    This does *not* address the OP's problem - which is that the program is not asking for the data for the third book. – Adrian Mole Apr 13 '20 at 15:22
  • 1
    @AdrianMole `output should be the same as input, but it's not` Apparently OP has more than one problem, if two of us spotted 2 different problems. – Agent_L Apr 13 '20 at 15:59
  • 1
    The problem is I have made a program to scan name, price, and number of pages of 3 books and then print the same data. But you can see that I am not able to enter details of the third book(You can check the picture I added). The value I entered for a second book is getting printed for the third one. And some garbage value is getting printed for second one. – UTPAL Apr 13 '20 at 16:33
  • @UTPAL then I believe Adrian's answer is the one you're looking for. Stay clear from floats/doubles tho : ) – Agent_L Apr 14 '20 at 08:36
0

You are using your program wrong.

"%c %f %d" means: scan 1 character, then a space, then a float, then a space, then an int. There is no Enter. I ran your program and it works ok - as long as I don't press Enter:

Enter names,prices & no. of pages of 3 books                                                                                                                                       
a 123.33 555b 124.44 666c125.555 777                                                                                                                                               
And this is what you entered                                                                                                                                                       
a 123.330002 555                                                                                                                                                                   
b 124.440002 666                                                                                                                                                                   
c 125.555000 777

You can see how scanf detects change between last %d to first %c by just figuring out "hmm, 'b' doesn't fit as another digit of '555' so let's finish the %d." and then the next line is ran. If you press Enter, then it's the newline(Enter) that goes to the title of the next book. And when then you press 'b', it doesn't fit neither %f nor %d, so those are set to zero and then your 'b' finally gets matched as the title of a third book. so you got 3 books: a, Enter and b.

If you want to ask for Enter then add "expect Enter": scanf("%c %f %d\n" Now your program runs like that:

Enter names,prices & no. of pages of 3 books                                                                                                                                       
a 123.33 555                                                                                                                                                                       
b 124.44 666                                                                                                                                                                       
c 125.55 777                                                                                                                                                                       
And this is what you entered                                                                                                                                                       
a 123.330002 555                                                                                                                                                                   
b 124.440002 666                                                                                                                                                                   
c 125.550003 9  

scanf just does exactly as you tell it to.

Unfortunately, both linked answers are not very good. The problem is not at the beginning of the second line, it's at the and of the first one.

Agent_L
  • 4,960
  • 28
  • 30
  • @AdrianMole Your answer hints in the right direction, but I believe this is a bit more explicit and less error prone. – Agent_L Apr 14 '20 at 09:45
  • I changed it to this and now it doesn't show printf but takes the values for it eventually. what's the explanation for this here ->>> printf ("Enter names, prices & no. of pages of 1st Book\n"); scanf ("%c %f %d\n",&b1.name,&b1.price,&b1.pages); printf ("Enter names, prices & no. of pages of 2nd Book\n"); scanf ("%c %f %d\n",&b2.name,&b2.price,&b2.pages); printf ("Enter names, prices & no. of pages of 3rd Book\n"); scanf ("%c %f %d",&b3.name,&b3.price,&b3.pages); Enter names, prices & no. of pages of 1st Book A 123.33 555 B 124.44 666 Enter names, prices & no. of pages of 2nd – SunMan Aug 21 '20 at 12:32