简体   繁体   中英

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.

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.

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, eg by flushing stdin ).

In a similar fashion, your name member might only store one character - not a complete name. Change it to an array, eg 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.

除了SJuan76指出的基本问题之外,它可能会更好地工作,并且如果您结束行,则更容易阅读:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM