私は、ユーザーがテキストファイルに格納されている連絡先情報を入力するCで小さなアドレス帳プログラムを作ろうとしています。ユーザーが新しい連絡先を追加することを選択した場合、プログラムは終了時に別の連絡先を追加するかどうかを尋ねる必要があります。私はif文を作成しました。これは、ループが 'n'を入力するとループを終了するか、何か他のものを書くと別の連絡先を続けると考えられます。しかし、ユーザーが 'y'(または何か他のもの)を入力すると、ループは1回だけ終了します。Do Whileループは常に最初のループの後に終了します
void new_contact(void){
printf("Ready for new contact\n");
int enter_new_contact = 1;
char first_name[15];
char second_name[20];
char phone_number[12];
char email[50];
char y_n[1];
do{
printf("Enter the contacts first name.\n");
scanf("%s", first_name);
printf("Enter the contacts second name.\n");
scanf("%s", second_name);
printf("Enter the contacts phone number.\n");
scanf("%s", phone_number);
printf("Enter the contacts email.\n");
scanf("%s", email);
printf("%s\t%s\t%s\t%s\n", first_name, second_name, phone_number, email);
printf("Would you like to add another contact?\n");
scanf("%s", y_n);
printf("%s\n", y_n);
if(strcmp(y_n, "n")==0){
enter_new_contact=0;
}
}while(enter_new_contact==1);
}
ありがとうございます!
まず最初に:そのような 'char型のy_n [1]として未定義の動作を修正;'文字を保持するのに十分でないことと、ヌルターミネータ。 –
'char y_n [1];':このcharバッファは小さすぎます。それは賢明な文字列を保持することはできません。ヌルターミネータのためのスペースが必要です。ユーザーが「はい」または「いいえ」と入力するとどうなりますか? –