2016-06-11 59 views
0

我试图简单地从用户读取输入并将CD记录存储到某些变量中。除了char的第二个阵列artist(不打印任何内容)外,所有变量的详细信息都已正确输出。我试图通过在scanf()的每个格式化字符串的前面引入空格来处理额外的字符间距,但这并没有解决它。Printf和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); 
} 
+0

也许使用'%s'是不是一个好主意 - 参见[scanf函数(http://linux.die.net/man/3/scanf) - 一个假设有些标题是多个单词 –

+0

只有为第二个和第四个scanf语句提供空间 – Cherubim

+0

%s即使只有一个单词也不工作,并且获取和放入正在演示相同的问题 –

回答

1

我想通了如何解决这个问题,同时保留noOfTracks作为短。因为我使用"%d"作为number of tracks变量的scanf,所以我覆盖了为前一个艺术家变量分配的空间,因此它没有被显示出来。为了解决这个问题,我不得不将"%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); 
} 
+0

你应该将此标记为答案。 –

+0

看起来,当您将'noOfTracks'声明为'short'但使用''%d“'('int')格式来打印它时,您会收到警告。作为一个方面说明:你可以随时编译你的程序:'gcc -Wall -Wextra your_file.c',它会给你'all'和'extra'隐藏警告:) –

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,以避免任何警告。

+0

谢谢你这个作品。 –

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

感谢这也很好。 :) –