问题
Ok firstly I'll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is figuring out the correct way to work my assignment. For my assignment I need to create a program that prompt the user to enter how many students they have then ask for the following information; Student ID, Birthdate, and Phone number. I need to use a loop to prompt the user to enter all the students information. I need to create a loop that will scan through all the student IDs and find the oldest student using their birthdate (The loop must be able scan through more then 3 students).
Here is my code, I've gotten some suggestions and even bits of code from you guys, but it's not allowing me to enter the students information when it gets to the for loop it just ends the program. Help
Thank you.
#include <stdio.h>
#include <stdlib.h>
struct studentDataType
{
int studentID;
int year;
int month;
int day;
long long phone;
};
int main (void)
{
int * studentData= NULL;
int * studentDataType;
int students;
int studentID;
int year;
int month;
int day;
long long phone;
printf("How many students are you entering records for:\n");
scanf("%d", &students);
studentData= malloc((sizeof(int)*students));
struct studentDataType *studentRecords = malloc(sizeof(struct studentDataType) * students);
for (int i = 0 ; i != students ; ++i) {
printf("Enter information for student %d\n", i+1);
struct studentDataType * s = &studentData[i];
scanf("%d%d%d%d%d", &(s->studentID), &(s->year), &(s->month), &(s->day), &(s->phone));
}
}
回答1:
struct studentDataType
{
int studentID;
int year;
int month;
int day;
long long phone;
};
int _tmain(int argc, _TCHAR* argv[])
{
int students;
printf("How many students are you entering records for:\n");
scanf("%d", &students);
struct studentDataType *studentRecords = (struct studentDataType *)malloc(sizeof(struct studentDataType) * students);
struct studentDataType *student = studentRecords;
for (int i = 0; i < students; i++)
{
printf("Enter information for student #%d\n", i+1);
scanf("%d#%d#%d#%d#%d", &(student->studentID),
&(student->year),
&(student->month),
&(student->day),
&(student->phone));
student++; // move pointer to next student
}
// print info
student = studentRecords;
for (int i = 0; i < students; i++)
{
printf("%d#%d#%d#%d#%d\n", student->studentID,
student->year,
student->month,
student->day,
student->phone);
student++; // move pointer to next student
}
getchar();
return 0;
}
回答2:
There are many issues in your current code. The struct member phone (i.e. a long long will not hold a phone number correctly eg. 555-5555), and the way you are allocating memory for the number of students are just two of the issues. I made some changes that should illustrate how you can loop on a number of students, and collect that information into the struct.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int studentID;
int year;
int month;
int day;
char phone[20];//this size should accommodate local, long dist, and intn'l
}studentDataType;
studentDataType s, *studentRecords;
int main (void)
{
// int * studentData= NULL;
//int * studentDataType;
int students;
//int studentID;
//int year;
//int month;
//int day;
//long long phone;
printf("How many students are you entering records for:\n");
scanf("%d", &students);
studentRecords = malloc(sizeof(s)*students);
//studentData= malloc((sizeof(int)*students));
//struct studentDataType *studentRecords = malloc(sizeof(struct studentDataType) * students);
for (int i = 0 ; i != students ; i++) {
printf("Enter information for student %d\n", i);
//struct studentDataType * s = &studentData[i];
scanf("%d%d%d%d%s", &studentRecords[i].studentID,
&studentRecords[i].year,
&studentRecords[i].month,
&studentRecords[i].day,
studentRecords[i].phone);
}
getchar();
}
回答3:
EDIT: Changed the record iterator and added some error checking on the malloc() result.
You have several issues in your code, so I am just posting something I believe should work and you can ask specific questions if you'd like. Try the following:
#include <stdio.h>
#include <stdlib.h>
struct studentDataType
{
int studentID;
int year;
int month;
int day;
long long phone;
};
int main (void)
{
struct studentDataType *studentRecords=NULL;
unsigned int students;
unsigned int studentID;
unsigned int year;
unsigned int month;
unsigned int day;
unsigned long phone;
printf("How many students are you entering records for:\n");
scanf("%d", &students);
studentRecords = malloc(sizeof(struct studentDataType) * students);
// Check whether malloc succeeded.
if(studentRecords != NULL)
{
struct studentDataType *current_record = &studentRecords[0];
for (int i = 0 ; i < students ; ++i, current_record++) {
printf("Enter information for student %d\n", i+1);
scanf("%u %u %u %u %u", &(current_record->studentID), &(current_record->year), &(current_records->month), &(current_record->day), &(current_records->phone));
}
free(studentRecords);
}
}
来源:https://stackoverflow.com/questions/19730765/why-isnt-this-program-allowing-me-to-enter-information-when-i-need-too