Printf和Scanf无法正常输入第二个字符串输入

时间:2016-06-11 11:01:30

标签: c

我试图简单地读取用户的输入并将CD记录存储到某些变量中。除了char的{​​{1}}的第二个数组之外,所有变量的所有变量详细信息都正确打印出来,而artist不打印任何内容。我已经尝试通过在scanf()中的每个格式化字符串的前面引入空格来处理额外的字符间距,但是还没有修复它。

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price);

int main()
{
    char title[61];
    char artist[41];
    short noOfTracks = 0;
    int isAlbum = 0;
    float price = 0.0;
    char albumOrSingleResponse;

    printf("Please enter the title: ");
    scanf(" %s", title);

    printf("Please enter the artist: ");
    scanf(" %s", artist);

    printf("Please enter the number of records: ");
    scanf(" %d", &noOfTracks);

    printf("Please enter whether or not the cd is an album/single (A or S): ");
    scanf(" %c", &albumOrSingleResponse);

    if(albumOrSingleResponse == 'A')
    {
        isAlbum = 1;
    }

    printf("Please enter the price of the CD: ");
    scanf(" %f", &price);

   displayCDInfo(title, artist, noOfTracks, isAlbum, price);

   return 0;
}

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price)
{
    printf("\nThe title of the CD is %s", title);
    printf("\nThe name of the artist is %s", artist);
    printf("\nThe number of tracks on this CD are %d", noOfTracks);
    printf("\nThe CD is an %s", (isAlbum == 1) ? "album" : "single");
    printf("\nThe price of the cd is $%.2f", price);
}

3 个答案:

答案 0 :(得分:1)

删除空格:scanf("%s", ...)
并添加空格:scanf(" %c", ..)

int main()
{
    char title[61];
    char artist[41];
    short noOfTracks = 0;
    int isAlbum = 0;
    float price = 0.0;
    char albumOrSingleResponse;

    printf("Please enter the title: ");

scanf("%s", title);

printf("Please enter the artist: ");
scanf("%s", artist);

printf("Please enter the number of records: ");
scanf("%hu", &noOfTracks);

printf("Please enter whether or not the cd is an album/single (A or S): ");
scanf(" %c", &albumOrSingleResponse);

if(albumOrSingleResponse == 'A')
{
    isAlbum = 1;


   }

    printf("Please enter the price of the CD: ");
    scanf("%f", &price);

   displayCDInfo(title, artist, noOfTracks, isAlbum, price);

   return 0;
}

因为当您在" %c"之前添加空格时,它会消耗使用先前输入输入的换行符。


编辑:从this,您可以使用"%hu"打印short以避免任何警告。

答案 1 :(得分:1)

这会奏效。只需从主体顶部移动char数组声明的位置。

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price);

int main()
{
    short noOfTracks = 0;
    int isAlbum = 0;
    float price = 0.0;
    char albumOrSingleResponse;
    char title[61];
    char artist[41];

    printf("Please enter the title: ");
    scanf(" %s", title);

    printf("Please enter the artist: ");
    scanf(" %s", artist);

    printf("Please enter the number of records: ");
    scanf(" %d", &noOfTracks);

    printf("Please enter whether or not the cd is an album/single (A or S): ");
    scanf(" %c", &albumOrSingleResponse);

    if(albumOrSingleResponse == 'A')
    {
        isAlbum = 1;
    }

    printf("Please enter the price of the CD: ");
    scanf(" %f", &price);

   displayCDInfo(title, artist, noOfTracks, isAlbum, price);

   return 0;
}

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price)
{
    printf("\nThe title of the CD is %s", title);
    printf("\nThe name of the artist is %s", artist);
    printf("\nThe number of tracks on this CD are %d", noOfTracks);
    printf("\nThe CD is an %s", (isAlbum == 1) ? "album" : "single");
    printf("\nThe price of the cd is $%.2f", price);
}

答案 2 :(得分:1)

我想出了如何解决这个问题,同时保持noOfTracks为短。由于我在轨道数变量上使用"%d"进行扫描,因此我覆盖了为前一个变量(艺术家)分配的空间,因此没有显示它的原因。要解决这个问题,我必须将"%d"更改为"%hu"以获得短整数。

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price);

int main()
{
    char title[61];
    char artist[41];
    short noOfTracks = 0;
    int isAlbum = 0;
    float price = 0.0;
    char albumOrSingleResponse;

    printf("Please enter the title: ");
    scanf("%s", title);

    printf("Please enter the artist: ");
    scanf("%s", artist);

    printf("Please enter the number of records: ");
    scanf("%hu", &noOfTracks);

    printf("Please enter whether or not the cd is an album/single (A or S): ");
    scanf(" %c", &albumOrSingleResponse);

    if(albumOrSingleResponse == 'A')
    {
        isAlbum = 1;
    }

    printf("Please enter the price of the CD: ");
    scanf(" %f", &price);

    displayCDInfo(title, artist, noOfTracks, isAlbum, price);

    return 0;
}

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price)
{
    printf("\nThe title of the CD is %s", title);
    printf("\nThe name of the artist is %s", artist);
    printf("\nThe number of tracks on this CD are %hu", noOfTracks);
    printf("\nThe CD is an %s", (isAlbum == 1) ? "album" : "single");
    printf("\nThe price of the cd is $%.2f", price);
}