0

I am trying to run the following code in Visual stuido. Please execute this code and then read my experience in below the code typed here.

#include <stdio.h>
#include <conio.h>
main()
{
int i;
struct book
{
    char name;
    float price;
    int pages;
};
struct book b[3];
printf("Enter the names prices & no. of pages of 3 books \n");
for (i = 0; i<=2; i++)
{
    printf("name of book %d : ", i +1);
    scanf("%c", &b[i].name);
    printf("price of book %d : ", i +1);
    scanf("%f", &b[i].price);
    printf("pages in book %d : ", i +1);
    scanf("%d", &b[i].pages);
}
for (i = 0; i<=2; i++)
{
    printf("Name of book : %c, Price of book: %f, Pages in book : %d \n", b[i].name, b[i].price, b[i].pages);
}
printf("Press any key to continue");
getch();
 }
void linkfloat()
{
    float a =0, *b;
    b = &a;
    a = *b;
}

As you can see it asks user the book name, pages nos and price, but it so happens that when you run code in visual basic, it doesn not allow to type name for book b2 onwards while it allows user to type price and page no for the same book b[i], moving forward it prints a blank space for book name where it did not allow user to type the name.

6
  • While not being perfect, ommitting return type and parameters is perfectly fine for main (with MSVC; iirc it will treat it as void main(void);. Commented Apr 23, 2012 at 9:09
  • 1
    Go and get yourself a more modern book. Your main isn't one of the canonical forms, and conio/getch should have disappeared about the same time as Borland/Inprise/Enchilada or whatever they call themselves nowadays. Commented Apr 23, 2012 at 9:15
  • Technically, conio is still around then. :) @paxdiablo Commented Apr 23, 2012 at 9:50
  • @paxdiablo : I didn't get you. Commented Apr 23, 2012 at 10:10
  • @vin, I mean conio is an anachronism from days long past. If you're reading code that contains it, it's way out of date. Commented Apr 23, 2012 at 10:59

3 Answers 3

2

This is one of the reasons you shouldn't rely on scanf() for your input, because wrong inputs might screw up everything. I'm not sure what compiler you used before, but this code shouldn't work in any standards compliant c compiler.

When reading or printing strings, you have to use the format tag %s. %c stands for a single character only, so entering any name longer than one character will screw up all input requests following (unless handled properly, e.g. by flushing stdin).

In a similar fashion, your name member might only store one character - not a complete name. Change it to an array, e.g. char name[64]. Make sure it's long enough to store the complete name (and to avoid buffer overruns).

There might be other mistakes, but I think those are the most significant ones keeping you from finding any other issues (if there are any).

Edit: Tried the code and the issues happen due to the line break (from hitting return) still sitting in stdin. When reading an integer or float, it is skipped (due to not forming valid input), but it's accepted for %c (didn't check it, but if you do, you should notice the value read should be equal to \n).

To fix this, always call fflush(stdin); after you've read using scanf(). Doing it right before reading the character should be enough, but it's still a bit error prone.

Sign up to request clarification or add additional context in comments.

4 Comments

I understand your point but I am using a single character only, it does not work.
@vin: Even when the input is limited to single character book titles, the problem you have is that the newline from the inputting the number of pages gets read as the one character book title for the next book (since the newline isn't part of the int read in for the pages member, it stays in the stream and gets read by the %c format). Hence, "it doesn't not allow to type name for book b2 onwards". This is yet another reason why using scanf() for arbitrary input is a tricky feat. Just another reason why I prefer to avoid scanf(), as Mario suggests.
@Michael: So, what do you suggest should be used instead of scanf() ?
You can use it, just ensure the buffer is clean before doing so (unless you expect input being there; e.g. passed through a pipe). Just see my edit above.
2

char only provides space for a character. Similarly, I believe that %c only reads a character.

You either change it to a char array big enough to hold the book name, or you change it to char * and use malloc to get memory to store the name.

1 Comment

the code being used for trial purpose I enter sinlge letter only, what compiler do you use? can you run it on yours and see if same issue arises??
-1

In addition to the fundamental problem SJuan76 pointed out it'll probably work better, and be easier to read if you end your lines:

printf("name of book %d:\n", i + 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.