我的代码工作正常,除了一行不断打印出应该用于下一行的数据。
输出应该看起来像这样:
Address Details
Street number: 100
Street name: Bedrock
Apartment: 14
Postal code: Z8Z 7R7
City: Markham
相反,我的输出是这样的:
Address Details
Street number: 100
Street name: Bedrock
Apartment: 14
Postal code: Z8Z 7R7Markham
City: Markham
如您所见,Markham
沿着同一行邮政编码打印。
以下是可能导致此错误的潜在文件。非常感谢所有帮助!
a1ms4.c文件:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"
int main(void)
{
// Declare variables here:
// Create a variable of type Contact and call it something self-describing like "contact"
// - HINT: Be sure to initialize the values to 0 and empty C strings
struct Contact contact = {
{ "", "", "" },
{0," ", 0, " ", " "},
{ "", "", "" }
};
// Display the title
printf("Contact Management System\n");
printf("-------------------------\n");
// Call the Contact function getName to store the values for the Name member
getName(&contact.name);
// Call the Contact function getAddress to store the values for the Address member
getAddress(&contact.address);
// Call the Contact function getNumbers to store the values for the Numbers member
getNumbers(&contact.numbers);
// Display Contact summary details
printf("\nContact Details\n");
printf("---------------\n");
printf("Name Details\n");
printf("First name: %s", contact.name.firstName);
printf("\n");
printf("Middle initial(s): %s", contact.name.middleInitial);
printf("\n");
printf("Last name: %s", contact.name.lastName);
printf("\n");
printf("\n");
printf("Address Details\n");
printf("Street number: %d", contact.address.streetNumber);
printf("\n");
printf("Street name: %s", contact.address.street);
printf("\n");
printf("Apartment: %d", contact.address.apartmentNumber);
printf("\n");
printf("Postal code: %s", contact.address.postalCode);
printf("\n");
printf("City: %s", contact.address.city);
printf("\n");
printf("\n");
printf("Phone Numbers:");
printf("\n");
printf("Cell phone number: %s", contact.numbers.cell);
printf("\n");
printf("Home phone number: %s", contact.numbers.home);
printf("\n");
printf("Business phone number: %s", contact.numbers.business);
printf("\n");
printf("\n");
// Display Completion Message
printf("Structure test for Contact using functions done!\n");
return 0;
}
contacts.c文件
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// This source file needs to "know about" the structures you declared
// in the header file before referring to those new types:
// HINT: put the header file name in double quotes so the compiler knows
// to look for it in the same directory/folder as this source file
// #include your contacts header file on the next line:
#include "contacts.h"
// Get and store from standard input the values for Name
// Put your code here that defines the Contact getName function:
void getName(struct Name * name) {
//Variable/Struct declaration:
char notify;
// Contact Name Input:
printf("Please enter the contact's first name: ");
scanf("%30s", name->firstName);
//Prompts the user to see if they want to enter a middle name using y, yes or n, no.
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's middle initial(s): ");
scanf("%6s", name->middleInitial);
}
printf("Please enter the contact's last name: ");
scanf("%30s", name->lastName);
}
// Get and store from standard input the values for Address
// Put your code here that defines the Contact getAddress function:
void getAddress(struct Address * address) {
//Variable/Structure declaration:
char notify;
// Contact Address Input:
printf("Please enter the contact's street number: ");
scanf("%d",& (address->streetNumber));
printf("Please enter the contact's street name: ");
scanf("%40s", address->street);
//Prompts the user to see if they want to enter an apartment number using yes, or no.
printf("Do you want to enter an apartment number? (y or n): ");
scanf("%s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's apartment number: ");
scanf("%d",& (address->apartmentNumber));
}
printf("Please enter the contact's postal code: ");
scanf(" %[^\n]", address->postalCode);
printf("Please enter the contact's city: ");
scanf(" %40s", address->city);
}
// Get and store from standard input the values for Numbers
// Put your code here that defines the Contact getNumbers function:
void getNumbers(struct Numbers *numbers) {
//Variable/Structure declaration:
char notify;
// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or n): "); //Prompt the user to see if they want to enter a cellphone.
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's cell phone number: ");
scanf("%20s", numbers->cell);
}
printf("Do you want to enter a home phone number? (y or n): ");
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's home phone number: ");
scanf("%20s", numbers->home);
}
printf("Do you want to enter a business phone number? (y or n): ");
scanf(" %s", ¬ify);
if (notify == 'y' || notify == 'Y')
{
printf("Please enter the contact's business phone number: ");
scanf("%20s", numbers->business);
}
}
contacts.h
// Structure type Name declaration (Milestone 1)
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
// Structure type Address declaration
// Place your code here... (from Milestone 1)
struct Address {
int streetNumber;
char street[40];
int apartmentNumber;
char postalCode[7];
char city[40];
};
// Structure type Numbers declaration
// Place your code here... (from Milestone 1)
struct Numbers {
char cell[20], home[20], business[20];
};
答案 0 :(得分:1)
你应该在邮政编码的末尾有一个空终结符:
struct Address {
int streetNumber;
char street[40];
int apartmentNumber;
char postalCode[8];// add space for the null terminator
char city[40];
};
printf("Please enter the contact's postal code: ");
scanf(" %[^\n]", address->postalCode);
address->postalCode[7] = '\0'; // add a null terminator
这应该足以解决问题