私はC言語を新しくしていて、入力を持つライブラリを作ってみたいのですが、タイトル、著者、本の出版年です。出力には、棚コード(短縮タイトル)、ブックコード(書籍名の最初の文字と著者の最初の文字)、タイトル、著者と書籍の出版年があります。タイトルに "How to Programming"と入力すれば、棚番が "CHtP"になります。問題は、私が棚番を印刷するときに、私のプログラムに奇妙な記号が印刷されていることです。助けてください..奇妙な配列の文字列の出力
int main()
{
char title[5][200], author[5][200], shelf[5][200], bookcode[5][200], temp[200];
int year;
printf("Welcome to Blues Library\n");
printf("============================\n\n");
for(i = 0; i < 2; i++)
{
printf("Book's Title = ");
scanf("%[^\n]s", &title[i]); fflush(stdin);
printf("Book's Author = ");
scanf("%[^\n]s", &author[i]); fflush(stdin);
printf("Book's publishing year = ");
scanf("%d", &year); fflush(stdin);
printf("\n");
}
printf("\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < strlen(title[i]); j++)
{
if(j == 0)
{
shelf[i][j] = title[i][j];
}
else if(title[i][j-1] == ' ')
{
shelf[i][j] = title[i][j];
}
}
}
//print
for(i = 0; i < 2; i++)
{
printf("Book's Title = %s\n", title[i]);
printf("Book's Author = %s\n", author[i]);
printf("Book's publishing year = %d\n", year);
printf("Shelf Code = %s\n", shelf[i]);
}
}
'にfflush(STDIN)'悪い(未定義の動作)です。 –
さらに悪い: 'scanf("%[^ \ n] s "、&title [i])'は 'scanf("%[^ \ n] s "、title [i])'でなければなりません。 'author'とすべての文字列の場合と同じことBTW –
まず作業プログラムを貼り付けるのが良いです:-)問題は' scanf'にあります。 'title'と' author'(文字配列で指定されています)の文字列を入力しているので、 'scanf'の前に'& 'は必要ありません。 @PaulRは 'fflush(stdin)'を使うべきではないと言ったので...それは必要ありません –