#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);
所以最后我要求用户输入一个字符&#39; y&#39;或者&#39; n&#39;看他们是否想进入他们的中间名词。但是当我使用%c,因为他们将输入单个字符,程序完全跳过此代码。当我使用%s代替程序识别代码并正常工作。我想知道为什么会这样?
答案 0 :(得分:0)
&#39;&amp;&#39;在C中基本上是通过值传递的变量,它具有存储存储值的存储器地址的能力。
你需要更大的记忆力。像一个数组或缓冲区。 %s期望相应的参数为char *类型,对于scanf
char middleInitial_ans2 [];
将此用于%s