-5

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));
    }
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
NSwanson7
  • 173
  • 1
  • 13
  • 1
    For one thing, as-written `struct studentDataType * s = &studentData[i];` won't even *compile*, as `studentData` is a pointer-to-int, and obviously `s` is *not*. So the immediate answer to your question in the title is: "because the code won't compile." And the last `%d` in the input format string doesn't match the data type of `s->phone` which is a `long long`, so you have undefined behavior there, assuming you fix the broken compilation. – WhozCraig Nov 01 '13 at 16:17
  • If you couldn't tell I'm very new to this, so could you please give me a solution. – NSwanson7 Nov 01 '13 at 16:20
  • Since the code doesn't compile, look at the error output for information about what is wrong. – KevinDTimm Nov 01 '13 at 16:22
  • Remove the declaration of integer i from the for loop and make it above the for loop(where you have declared other variables), these type of initialization is allowed only in C99 standard. – sumitb.mdi Nov 01 '13 at 16:23
  • 3
    "give me a solution" - um.. If the only thing you want to fix being "new at" is asking people for solutions, I suppose I could do that. I'm fairly certain that isn't the stated goal of the class you're taking. I already pointed out two significant issues. Work on those. Between [this, question](http://stackoverflow.com/questions/19727857/do-i-need-to-create-three-separate-arrays-for-my-assignment), [this question](http://stackoverflow.com/questions/19728660/how-do-i-use-a-structure), and the current question, you've had you lion's share of spoon-feeding. – WhozCraig Nov 01 '13 at 16:26
  • I guess I should specify that I am using c99 mode. Sorry – NSwanson7 Nov 01 '13 at 16:29
  • @WhozCraig: C has some rather relaxed rules for implicit type conversion, so it does compile. – Thomas Padron-McCarthy Nov 01 '13 at 16:30
  • @ThomasPadron-McCarthy. Not on my clang chain, and it is C99 compliant (in ways MS could only dream of). And the very point I identified is problematic even if it did compile. Of course, I always use pedantic warnings, a plethora of additional stringent warning enablements, and warn-as-error, so it could be that, admittedly. – WhozCraig Nov 01 '13 at 16:34
  • @WhozCraig: Well, yes, if you tell your compiler to treat warnings as errors, some valid C code will not compile. (Not that that is a bad thing.) But I do believe the standard allows it. GCC 4.5.2 does, with a warning. – Thomas Padron-McCarthy Nov 01 '13 at 16:41

3 Answers3

0

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);
    }
}
djbuijs
  • 266
  • 1
  • 2
  • 10
  • I'm still getting the same problem of not being able to enter any of the students information. What am I doing wrong here? – NSwanson7 Nov 01 '13 at 16:37
  • Never mind I figured it out thank you – NSwanson7 Nov 01 '13 at 16:45
  • Glad you solved it! I fixed a typo: for "i < students" which was "i != students". – djbuijs Nov 01 '13 at 16:50
  • How would I create a loop that would search through the students and give me back the id of the oldest student? – NSwanson7 Nov 01 '13 at 16:51
  • @djbuijs - as soon as execution flow leaves the for block, s goes away. It should be defined and allocated before the for block, based on number of students. – ryyker Nov 01 '13 at 17:11
  • @ryyker - I am not sure I get your point: s is just a temporary pointer which is re-assigned on every for iteration. That said, there are cleaner methods of doing this. I will edit the code in a sec. – djbuijs Nov 01 '13 at 17:15
0

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();
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
0
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;
}
Artur
  • 7,038
  • 2
  • 25
  • 39