2014-11-14 30 views
0

该程序背后的思想是为学生信息声明一个数据结构,然后让用户将输入信息输入数据结构的每个字段中,以便输入数量的学生。我的问题是程序停止工作,当我输入名字。哪里不对?感谢您的时间。将用户输入值分配给分配的内存

//Inclusion of necessary header files 
#include <stdio.h> 
#include <stdlib.h> 

//Data structure declaration 
struct student{ 
char firstName[20]; 
char lastName[20]; 
char id[10]; 
char gender; 
int age; 
double gpa; 
}; 

//Function prototypes 
void readStudentsInformation(struct student *, int size); 
void outputStudents(struct student *, int size); 
double averageGPA(struct student *, int size); 
void sortByLastName(struct student *, int size); 
void sortByGPA(struct student *, int size); 


//Entry point 
int main() 
{ 
    //Variable delcaration 
    int size; 
    struct student *ptr; 
    //Input prompt and function 
    printf("How many students?\n"); 
    scanf_s("%d", &size); 

    //Allocation memory for struct student times the number of students and assigning it to a struct student pointer for external function modification 
    ptr = (struct student*)malloc(sizeof(struct student)*size); 

    //readStudentsInformation Function call 
    readStudentsInformation(ptr, size); 

    //Exit sequence 
    return 0; 
} 

//This functions reads the information for all the students from the keyboard, taking the class size through the pointer method and struct student from main 
void readStudentsInformation(struct student *ptr, int size) 
{ 
    //For loop controller declaration 
    int i; 
    //Function message 
    printf("Student Information Form\n"); 
    //This for loop increments the "index" of each students info location where user input is stored 
    for(i=0;i<size;i++) 
    { 
     //Each field has it's appropriate input limit 
     printf("Please enter Student %d's First Name(20 characters).\n", i+1); 
     scanf_s("%20s", ptr[i].firstName); 
     printf("Please enter Student %d's Last Name(20 characters).\n", i+1);  
     scanf_s("%20s", ptr[i].lastName);         
     printf("Please enter Student %d's ID(10 characters).\n",i+1);    
     scanf_s("%10s", ptr[i].id); 
     printf("Please enter Student %d's gender(M/F).\n",i+1); 
     scanf_s("%c", ptr[i].gender); 
     printf("Please enter Student %d's age.\n",i+1); 
     scanf_s("%3d", ptr[i].age); //Only 3 digits can be put in at a time 
     printf("Please enter Student %d's GPA.\n", i+1); 
     scanf_s("%.1lf", ptr[i].gpa); //From the lab it can be seen that no more than one decimal place is featured, so the same is done here 
    } 

    //Exit to main 
    return; 
} 
+0

我已经调整了scanf_s的,但该计划似乎仍在崩溃。我不认为这是正确的处理。如果我声明一个类型为struct student的数组并将其数组大小设置为用户输入,会怎么样? – sourcecodedysfunction

回答

0

你应该做

scanf_s("%20s", ptr[i].firstName); 

读头名。 ptr+i会给你student结构。您想要将数据存储在结构成员中。

更改您的其他scanf s为此。

+0

不,缺少尺寸参数。 “fscanf_s函数等同于fscanf,只是c,s和[转换说明符适用于一对参数...” – chux

0

你可以在你的scanf()的使用

scanf("%s",ptr[i].firstName); 

等的结构成员的其余部分。

0

变化scanf_sscanf并更改该块如下:

scanf("%c", &ptr[i].gender); 
    printf("Please enter Student %d's age.\n",i+1); 
    scanf("%3d", &ptr[i].age); //Only 3 digits can be put in at a time 
    printf("Please enter Student %d's GPA.\n", i+1); 
    scanf("%1lf", &ptr[i].gpa);