【问题标题】:Printf and Scanf not working properly for second string inputPrintf 和 Scanf 对于第二个字符串输入无法正常工作
【发布时间】:2016-06-11 11:01:30
【问题描述】:

我试图简单地读取用户的输入并将 CD 记录存储到一些变量中。除了 char 的第二个数组 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);
}

【问题讨论】:

  • 也许使用 %s 不是一个好主意 - 请参阅 scanf - 假设某些标题是多个单词
  • 只给第二个和第四个scanf语句空间
  • %s 连一个词都不能用,gets 和 puts 都证明了同样的问题
  • 作为起点将scanf(" %s", title);更改为scanf("%s", title);
  • 永远不要使用 scanf("%s", ..); (为什么有这么多初学者?)。最好使用 fgets(),或者使用 scanf("%"MAXLEN"s",...)。否则你会得到一个数组溢出和 UB 真的很快。

标签: c


【解决方案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】:

    我现在想出了如何解决这个问题,同时保持noOfTracks 为短。由于我使用 "%d" 在 Number of tracking 变量上进行 scanf,因此我覆盖了为前一个变量(即艺术家)分配的空间,因此它没有被显示。为了解决这个问题,我不得不将 "%d" 更改为 "%hu" 简称为 int。

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

    【讨论】:

    • 当您将noOfTracks 声明为short 但使用"%d" (int) 格式打印时,您似乎收到了警告。附带说明:您始终可以使用以下命令编译程序:gcc -Wall -Wextra your_file.c,它会给您allextra 隐藏警告:)
    【解决方案3】:

    删除空格: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 以避免任何警告。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 2016-08-12
      相关资源
      最近更新 更多