how to write a simple C program

April 15, 2011 at 13:00:24
Specs: Windows Vista
The question requires you to output the following options for the user :
1. Input student list
2. Output student list
3. Add a new student
4. Search a student by Id or last name
5. Delete a student with a given student Id
6. Update a student with a given student Id
7. Sort on last name or average mark
8. Exit
I have started the code but i'm stuck and is running out of ideas, any input in the code will be appreciated deeply....thanks!!!!

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct Student
{
char sId[10]; // student ID
char fName[30]; // student's first name
char lName[30]; // student's last name
int m, p, c; // m = maths, p = physics, c = chemistry
// m, p, and c belong to the range [0..10]
double avg; //average mark with 1 digit after decimal point
};
//---------------------------------------------------------
Student input1Student();
void output1Student(const Student &x);
void inputStudentList(Student list[], int &n);
void outputStudentList(const Student list[], const int &n);
//---------------------------------------------------------
int main(void)
{
int op = 0, op2 = 0;
int n = 0;
Student list[10];

while(op != 8)
{
printf("1. Input student list\n");
printf("2. Output student list\n");
printf("3. Add a new student\n");
printf("4. Search a student by Id or last name\n");
printf("5. Delete a student with a given student Id\n");
printf("6. Update a student with a given student Id\n");
printf("7. Sort on last name or average mark\n");
printf("8. Exit\n");
printf("Enter your option (1-8): ");
scanf("%d", &op);
switch(op)
{
case 1:
inputStudentList(list, n);
break;

case 2:
outputStudentList(list, n);
break;

case 3:

break;

case 4:
printf("\t1. Search by Id\n");
printf("\t2. Search by last name\n");
printf("Select an option (1 or 2): ");
scanf("%d", &op2);
break;

case 5:

break;

case 6:

break;

case 7:
printf("\t1. Sort on last name\n");
printf("\t2. Sort on average mark\n");
printf("Select an option (1 or 2): ");
scanf("%d", &op2);
break;
}
} // while(op != 8)

//system("pause");
return 0;
}
//---------------------------------------------------------
Student input1Student()
{
Student x;

printf("Student ID: ");
scanf("%s", x.sId);
fflush(stdin); // clear '\n' in buffer

printf("First name : ");
fgets(x.fName, 80, stdin);
x.fName[strlen(x.fName) - 1] = '\0';

printf("Last name : ");
fgets(x.lName, 80, stdin);
x.lName[strlen(x.lName) - 1] = '\0';

printf("Maths : ");
scanf("%d", &x.m);

printf("Physics : ");
scanf("%d", &x.p);

printf("Chemistry : ");
scanf("%d", &x.c);
fflush(stdin); // clear '\n' in buffer

x.avg = (double)(x.m + x.p + x.c) / 3;
printf("------------------------------\n");
return x;
}
//---------------------------------------------------------
void output1Student(const Student &x)
{
printf("Student ID : %s\n", x.sId);
printf("First name : %s\n", x.fName);
printf("Last name : %s\n", x.lName);
printf("Maths : %d\n", x.m);
printf("Physics : %d\n", x.p);
printf("Chemistry : %d\n", x.c);
printf("Average mark : %.2f\n", x.avg);
printf("------------------------------\n");
}
//---------------------------------------------------------
void inputStudentList(Student list[], int &n)
{
int i = 0;

printf("Enter the number of students : ");
scanf("%d", &n);

for (i = 0; i < n; i++)
list[i] = input1Student();
}
//---------------------------------------------------------
void outputStudentList(const Student list[], const int &n)
{
int i = 0;

for (i = 0; i < n; i++)
output1Student(list[i]);
}
//---------------------------------------------------------


See More: how to write a simple C program

Report •

#1
April 15, 2011 at 18:52:22
4. Search a student by Id or last name
Go though your list, looking at the relevant variables. Print if you get a match; error if you don't.

5. Delete a student with a given student Id
Same search as 4. Overwrite the matching element with the last element and decrement your active Student count.

6. Update a student with a given student Id
Same search as 4. Prompt for corrections and overwrite the bad data with the new data.

7. Sort on last name or average mark
Just use a simple bubble sort. There's no reason to get fancy.

How To Ask Questions The Smart Way


Report •

#2
April 16, 2011 at 13:34:29
Before you get too far along, you might want to mill over a few things:

Students list[10];

you've hard coded an upper bound, but in the function inputStudentList, you're asking the user to enter a number of records without checking if that number is valid. For example, what happens to list[i] if n > 9 ?

in both fName and lName, you've allocated 30 bytes in your structure, but in the call to fgets, you're accepting up to 80. fgets(x.fName, sizeof x.fName, stdin);

x.fName[strlen(x.fName)-1] = '\0'
I'm guessing this is for the side effect of fgets? the new line may or may not be present depending on how many characters were actually typed.

char *p;
if((p = strchr(x.fName, '\n')) != NULL) *p = '\0';


Report •
Related Solutions


Ask Question