Hello all,
I am new to this forum as well as C programming so please go easy on me.
I have looked through various faqs and other internet sites to find the answer to my current problem but so far I have been unsuccessful. I'm writing a program in C that allows you to enter student records, change test scores, and display all the records currently entered.
So here is my problem. I am trying to store all the records in an array of structures and for the first two student entries it works fine. But for some reason when I try and add a third student the program just quits after I enter all the data and I can't figure out why. You can see in the source code there is a bold line that prints "here." After entering all the third student's data this line is printed and then the program quits so it seems the line that stores the structure in the array is causing the issue.
I thought the problem may have been each structure using too much memory space than the array could hold so I tried increasing the size of the array to 100 but the program still quit.
Any ideas as to why this would happen? Many thanks for any hints or clues as to how I can fix this.
Code:
#include<stdio.h>
struct student{
int studentID;
char name[30];
int prog1, prog2, prog3;
int test1, test2, test3;
};
int addStudent(int cnt, struct student array[10]){
struct student newStudent;
printf("cnt = %d\n", cnt);
printf("Enter student number: ");
scanf("%d", &newStudent.studentID);
printf("Enter student's name: ");
scanf("%s", newStudent.name);
printf("Enter program scores: ");
scanf("%d %d %d", &newStudent.prog1, &newStudent.prog2, &newStudent.prog3);
printf("Enter test scores: ");
scanf("%d %d %d", &newStudent.test1, &newStudent.test2, &newStudent.test3);
printf("here");
array[cnt] = newStudent;
cnt++;
return cnt;
}
void programScores(){
printf("Call to programScores\n");
}
void testScores(){
printf("Call to testScores\n");
}
void deleteStudent(){
printf("Call to deleteStudent\n");
}
void displayRecords(int cnt, struct student array[10]){
int i;
printf("Cnt = %d\n", cnt);
printf("Call to displayRecords\n");
printf("-------------------------------------------------------------------\n");
printf("ID# Name Prog1 Prog2 Prog3 Test1 Test2 Test3\n");
printf("-------------------------------------------------------------------\n");
for(i = 0; i < cnt; i++)
{
printf("%-5d %-18s %-7d %-7d %-7d %-6d %-6d %-6d\n", array[i].studentID,
array[i].name, array[i].prog1, array[i].prog2, array[i].prog3,
array[i].test1, array[i].test2, array[i].test3);
}
}
main()
{
int option = 0;
int count = 0;
int *ptr;
ptr = &count;
printf("ptr value = %d\n", *ptr);
struct student studentArray[10];
while(option != 6)
{
printf("1. Add a student\n2. Enter program scores\n");
printf("3. Enter Test Scores\n4. Delete a student\n");
printf("5. Display all records\n6. Quit\n");
printf("Enter Number: ");
scanf("%d", &option);
switch(option)
{
case 1: count = addStudent(count, &studentArray[10]);
//count++;
printf("Count = %d\n", count);
break;
case 2: programScores();
break;
case 3: testScores();
break;
case 4: deleteStudent();
break;
case 5: displayRecords(count, &studentArray[10]);
break;
case 6: printf("Goodbye");
break;
default: printf("Invalid Option\n");
break;
}
}
exit(0);
}