c - Printf 和 Scanf 对于第二个字符串输入无法正常工作

标签 c

我试图简单地读取用户的输入并将 CD 记录存储到一些变量中。除了 artist 的第二个 char 数组没有打印任何内容外,所有变量的所有变量详细信息都已正确打印出来。我试图通过在 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);
}

最佳答案

这会起作用。只需将 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);
}

关于c - Printf 和 Scanf 对于第二个字符串输入无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37762957/

相关文章:

c - 在 C 中使用 Bitwise 时,我遇到一个问题,如果 x 的任何位 = 0,则返回 1,否则返回 0

c - strcmp 忽略重音,输出 false

c - 为什么我的程序中第七个变量是6?

c++ - C/C++ 函数定义

c - 如何在现代opengl中制作示例一维纹理?

C - 动态内存分配 - 复制字符串

c - 尝试使用 pdcurses 扩展库时出现与 gdi32 相关的错误

c - 我的 C 堆栈指针在初始化期间可以指向的最大内存位置

C 读取重新分配缓冲区

c - 在 C 中写入多个文件并迭代其名称