vin
vin

Reputation: 879

The code programmed in C does not work with Visual Studio

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.

Upvotes: 0

Views: 1120

Answers (3)

Mario
Mario

Reputation: 36537

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.

Upvotes: 2

Kristof Provost
Kristof Provost

Reputation: 26332

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);

Upvotes: -1

SJuan76
SJuan76

Reputation: 24895

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.

Upvotes: 2

Related Questions