0
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "contacts.h"
int main(void)
{
int i = 0;
char middleInitial_ans;
// Declare variables here:
struct Name emp[] = { { 0 } };
struct Address addr[] = { { 0 } };
struct Numbers phone[] = { { 0 } };
// Display the title
puts("Contact Management System");
puts("-------------------------");
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf("%s", emp[i].firstName);
// PRINTING OUT FIRSTNAME
printf("%s\n\n", emp[i].firstName);
printf("Do you want to enter a middle intial(s)? (y or n): ");
scanf("%s", &middleInitial_ans); // Why doesn't the code work when I have %c instead of %s
printf("%c", middleInitial_ans);
if (middleInitial_ans == 'y') {
printf("Please enter the contact's middle initial(s): ");
scanf("%s", emp[i].middleInitial);
printf("\n%s\n", emp[i].middleInitial);
最後に、ユーザーが文字「y」または「n」を入力してミドルネームの頭文字を入力するかどうかを尋ねます。しかし、%cを使用するときには、1文字を入力するので、プログラムはこのコードを完全にスキップします。代わりに%sを使用すると、プログラムはコードを認識して正常に動作します。なぜそれが起こるのだろうか?scanfは入力を待っていません
非常に多くの重複!まだ入力バッファに未読の改行があります。他にもいくつかの問題がありますので、私の[初心者ガイドを 'scanf()'から読んでみてください(http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html) –