我正在创建一个程序,它只允许用户在一个可执行程序中执行多项操作。所以现在我将信息输入到我的struct变量中,当用户输入" P"进入窗口后,他们会想要收回信息。问题在于,当它显示结果时,而不是显示' 1或2'它显示" -0.23455558"和其他愚蠢的符号符号。这是我的代码。
C代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<ctype.h>
typedef struct {
char fname[40];
char lname[40];
int age;
int id;
double gpa;
double price;
}student;
void Addsix(student list[]);
char Getinput();
void PrintToScreen(student list[], int count);
int main() {
student list[100];
int count = 0;
char choice;
Addsix(list);
count = 6;
choice = Getinput();
while (choice != 'Q') {
if (choice == 'P') {
PrintToScreen(list, count);
}
choice = Getinput();
}
return 0;
}
void PrintToScreen(student list[], int count) {
student temp;
int i;
for (i = 0; i < count; i++) {
printf("First Name: %c\n", temp.fname[i]);
printf("Last Name: %c\n", temp.lname[i]);
printf("Age: %d\n", temp.age);
printf("Id: %d\n", temp.id);
printf("Gpa: %.2f", temp.gpa);
printf("Price: %.2f", temp.price);
}
}
char Getinput() {
char pick;
printf("\n***************************************\n");
printf("Please select from the options below:\n");
printf("P....Print the inventory list onto the screen\n");
printf("A....Add a new entry\n");
printf("C....Clear all records\n");
printf("S....Create a current report(save it to a file)\n");
printf("D....Delete an item from the list(inventory)\n");
printf("U....Update ONE OF THE FIELDS (not THE id)\n");
printf("Q - QUIT\n");
printf("What would you like to do: ");
scanf(" %c", &pick);
pick = toupper(pick);
return pick;
}
void Addsix(student list[]) {
strcpy(list[0].fname, "Jon");
strcpy(list[0].lname, "pep");
list[0].age = 19;
list[0].id = 2713;
list[0].gpa = 4.0;
list[0].price = 2.79;
strcpy(list[1].fname, "Amanda");
strcpy(list[1].lname, "Stri");
list[1].age = 25;
list[1].id = 9654;
list[1].gpa = 3.5;
list[1].price = 2.84;
strcpy(list[2].fname, "Patrick");
strcpy(list[2].lname, "Collie");
list[2].age = 42;
list[2].id = 7748;
list[2].gpa = 2.4;
list[2].price = 74.5;
strcpy(list[3].fname, "Kim");
strcpy(list[3].lname, "Campbell");
list[3].age = 21;
list[3].id = 4508;
list[3].gpa = 3.27;
list[3].price = 2.25;
strcpy(list[4].fname, "Micky");
strcpy(list[4].lname, "Peoples");
list[4].age = 37;
list[4].id = 1478;
list[4].gpa = 4.0;
list[4].price = 10.0;
strcpy(list[5].fname, "ako");
strcpy(list[5].lname, "Imo");
list[5].age = 15;
list[5].id = 8520;
list[5].gpa = 2.90;
list[5].price = 96.5;
strcpy(list[6].fname, "Rick");
strcpy(list[6].lname, "Rolle");
list[6].age = 28;
list[6].id = 1379;
list[6].gpa = 3.8;
list[6].price = 55.2;
}
答案 0 :(得分:1)
void PrintToScreen(student list[], int count) {
student temp;
int i;
for (i = 0; i < count; i++) {
printf("First Name: %c\n", temp.fname[i]);
printf("Last Name: %c\n", temp.lname[i]);
printf("Age: %d\n", temp.age);
printf("Id: %d\n", temp.id);
printf("Gpa: %.2f", temp.gpa);
printf("Price: %.2f", temp.price);
}
}
你的问题就在这里。在 no 点,您实际访问main函数中创建的数组中的任何内容(并作为参数传入此函数)。相反,您可以访问在此函数中创建的变量temp
,该变量将在成员中具有任意值。
最重要的是,由于结构的前两个成员是C字符串,您可能希望将它们视为而不是尝试输出单个字符:i
是结构中的索引数组,而不是形成字符串的字符数组。
你最好选择类似的东西(仅限于插图):
void PrintToScreen(student list[], int count) {
int i;
for (i = 0; i < count; i++) {
printf("First Name: %s\n", list[i].fname);
printf("Age: %d\n", list[i].age);
}
}