Jump to content

Need your help in C assignment

VAP810

in this assignment i have to make an app to store list of students, its information :

- sId: The student ID of the form like GTxxxxx or GCxxxxx (x: is a digit)
- sName: Student name
- sDoB: Student date of birth
- sEmail: Student email
- sPhone: Student phone number
- sAddress: Student address
This application will need to provide following functionalities via a menu
=======================
1.    Add new student
2.    View all students
3.    Search students
4.    Delete students
5.    Update student
6.    Exit
=======================
Please choose: 

When user chooses 1, program will prompt user to input student’s information (specified previously). After that, program will validate the input data and if they are all valid, program will add a new student to the current list of students. Program should inform to the user corresponding messages.
When user chooses 2, the program will list all the students to the screen, each student in a row and student’s data fields are separated by ‘|’.
When user chooses 3, the program will ask user to input student’s name to search for, the user can just type part of the name in order to search for complete student information.
When user chooses 4, program will ask user to input student id to delete the student with the specified id if it exists, otherwise, it will display a message to inform users that the student with such id doesn’t exist.
When user chooses 5, program will first ask user to input student id to update, once inserted and a student with the inserted id exists, it will display current data for each field of the student and user can type in new data to update or just press enter to keep the current data for the field.
When user chooses 6, program will exit.
 

So here is my code 

/*
 ============================================================================
 Name        : jj.c
 Author      :
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <malloc.h>
#include <string.h>

int i=0;
void menu(){
system("cls");
printf("=======================\n");
printf("1.Add new student\n");
printf("2.View all students\n");
printf("3.Search students\n");
printf("4.Delete students\n");
printf("5.Update student\n");
printf("6.Exit\n");
printf("=======================\n");
printf("Please select:");
int mn;
while(1){
    scanf("%d",&mn);
    system("cls");
    if (mn ==1) addstudent();
    else if(mn == 2) studentlist();
    else if(mn == 3) searchstudent();
    else if(mn == 4) deletestudent();
    else if(mn == 5) updatestudent();
    else if(mn == 6) exit(0);
    menu();}
}
 typedef struct student{
    char sname[40];
    char sid[8];
    char semail[20];
    char sphone[12];
    char sdob[11];
    char saddress[50];
}student;
student stlist[1000];
int n;
void ignoreEnter() {
	char rmEnter[2];
	gets(rmEnter);
}
void readgetid(){
 int length;
	while (1) {
            gets(stlist[i].sid);
            length = strlen(stlist[i].sid);
            if (length !=7)
                printf("The id length is wrong, please input id again:");
            else {printf("id length correct\n");
            } int result1,result2,result3;
            result1 = strncmp(stlist[i].sid, "GC", 2);
			result2 = strncmp(stlist[i].sid, "GT", 2);
            int l = 2;
			do {
				result3 = isdigit(stlist[i].sid[l]);
				l++;
				if (result3 == 0)
					break;
			} while(l<strlen(stlist[i].sid));

			if ((result1 == 0) || (result2 == 0)) {
				printf("The first 2 letters are true\n");
				if (result3 != 0) {
					printf("id is correct\n");
					break;
				}
				else printf("The remaining letters are wrong, please enter the id again(the remain letters must be digit): ");
			} else printf("The first 2 letters are wrong, please enter id again(the letters must be GC or GT): ");
		}
}


void addstudent(student *stlist) {
    char count;
    do {
    printf("Enter information of students:\n");
    printf("Enter student id:");
    fflush(stdin);
	readgetid();
    gets(stlist[1000].sid);
    printf("Enter name: ");
    gets(stlist[1000].sname);
    printf("Enter date of birth: ");
    gets(stlist[1000].sdob);
    printf("Enter email:");
    gets(stlist[1000].semail);
    printf("Enter phone number:");
    gets(stlist[1000].sphone);
    printf("Enter address:");
    gets(stlist[1000].saddress);
    n++;
    printf("Do you want to continue input student? y/n\n");
    char c; scanf("%c", &c);
	ignoreEnter();
    if(c == 'n')
        break;
        }
while(1);
}
void printstudent() {
	printf("=============================\n");
	printf("Student id: "); puts(stlist[1000].sid);
	printf("Student name: "); puts(stlist[1000].sname);
	printf("Student date of birth: "); puts(stlist[i].sdob);
	printf("Student email: "); puts(stlist[1000].semail);
	printf("Student phone: "); puts(stlist[1000].sphone);
	printf("Student address: "); puts(stlist[1000].saddress);
	printf("=============================\n");
}
void studentlist() {
    int i;
	for(i = 0; i < n; i++)
		printstudent(1000);

	getch();
}
void searchstudent() {
     studentlist();
     system("cls");
     char search[20];
    int i;
	printf("Enter student name : ");
	fflush(stdin);
	gets(search);
	printf("Students which have the name %s is: \n",search);
	for(i = 0; i < n; i++)
		if(strstr(stlist[1000].sname, search))
			printstudent(1000);
    printf("\n\n");
	getch();
}
void deletestudent() {
    system("cls");
	printf("Student id: ");
	char sid[7];
	gets(sid);
	int i, k;
	for(i = 0; i < n; i++)
		if(strcmp(sid, stlist[1000].sid) == 0) {
			for(k = i + 1; k < n; k++)
				stlist[k - 1] = stlist[k];
			n--;
			i--;

	getch();
 } else{
    printf("The student with such id doesn’t exist");

	getch();
	}
}
void updatestudent() {
    system("cls");
	printf("Student id: ");
	char sid[7];
	gets(sid);
	int i;
	for(i = 0; i < n; i++)
		if(strcmp(sid, stlist[1000].sid) == 0) {
			addstudent(&stlist[1000]);
			break;
		}

	getch();
}
int main(void){
	menu();
}





the "Add new student" part when i input the data it responding the information but the output view all student part didnt save it. So the "delete student" and the "update student" part dont work. Do you guys have any changes to make it work, i will appreciate if you guys make any changes to correct my code. Thanks :)

Link to comment
Share on other sites

Link to post
Share on other sites

Why there is 1000 everywhere as an index of stlist? (Your indentation is a mess) You probably meant to put there n (You may also want to initialize it with 0) but don't mindlessly replace 1000 with n now because there are cases (deletestudent) where you want index by i variable, and this is even messier:

for(i = 0; i < n; i++)
	if(strcmp(sid, stlist[1000].sid) == 0) {
		for(k = i + 1; k < n; k++)
			stlist[k - 1] = stlist[k];
		n--;
		i--;

		getch();
	} else{
	    printf("The student with such id doesn’t exist");
		getch();
	}

Even after some "prettifying", I would never recommend to not use brackets {} for loops (and ifs, even when it is one line).

 

There is a loot of errors where you define your function takes argument, but you don't providing it on call, and where you provide argument to function that is not take any.

 

You won't be able to compile it also because you refer to a function before you actually declare it, declare (or define) it before you use it.

You do declaration by putting just function header with ; at the end, and then you can use it for example in main function, and you can define (declaration with initialziation) it after main function, example:

#include <stdio.h>

void exampleFunction(int a, int b); //or even void exampleFunction(int, int);

int main(){
	
	exampleFunction(10, 20);
	
	return 0;
}

void exampleFunction(int a, int b){
	printf("a is %d\n", a);
	printf("b is %d\n", b);
	printf("a + b = %d", a + b);
}

This would not work because there is exampleFunction reference in main function and it takse place before exampleFunction is declared so compiler doesn't know what to do there:

#include <stdio.h>

int main(){
	
	exampleFunction(10, 20);
	
	return 0;
}

void exampleFunction(int a, int b){
	printf("a is %d\n", a);
	printf("b is %d\n", b);
	printf("a + b = %d", a + b);
}

This will work:

#include <stdio.h>

void exampleFunction(int a, int b){
	printf("a is %d\n", a);
	printf("b is %d\n", b);
	printf("a + b = %d", a + b);
}

int main(){
	
	exampleFunction(10, 20);
	
	return 0;
}


But when you have a loot of functions that one referees to another you yous do as in first example.

#include <stdio.h>

void foo1();
void foo2();
void foo3();

void foo1(){
	printf("Function 1\n");
	foo3();
}

void foo2(){
	printf("Function 2\n");
}

void foo3(){
	printf("Function 3\n");
	foo2();
}

int main(){
	
	foo1();
	
	return 0;
}

In above example, although all functions are above main function, foo1 function calls foo3 function before it is declared. So I declared all function on top and then I defined them, so all are aware of each other.

Link to comment
Share on other sites

Link to post
Share on other sites

You have many problems here...

 

Your function:

void addstudent(student *stlist)

expects to be passed a student pointer when called. But you call it as such:

if (mn ==1) addstudent(); //Where's the student pointer parameter ?

That should not even compile, or at least generate serious warnings, you should not ignore them.

Like @Mr_KoKa states you only ever seem to access element 1000 in the array.

You need to put a counter variable in the menu function that stores how many students are already in the array. Then you can use this counter to pass a pointer to the correct array element to your functions.

void menu()
{
//...snip...
int mn;
int StudentCount = 0;  //Counter for amount of students
while(1)
{
    scanf("%d",&mn);
    system("cls");
    if (mn ==1) 
    {
      addstudent(stlist + StudentCount); //Send pointer to array element to put new student into
      ++StudentCount;
    }
    //...snip...
    menu(); //<< You are needlessly calling the menu function 
  	    // recursively here, remove this, as it will eventually fill
  	    // the stack and crash the program.
}

You will have to move the struct definition above the menu function for this, otherwise "stlist" will not be known yet.

Now, inside the addstudent function, use the given pointer:

void addstudent(student *stlist) 
{
    char count;
    do 
    {
    	printf("Enter information of students:\n");
    	printf("Enter student id:");
    	fflush(stdin);
		readgetid();
    	gets(stlist->sid);
    	printf("Enter name: ");
    	gets(stlist->sname);
    	printf("Enter date of birth: ");
    	gets(stlist->sdob);
  	//...etc
    	n++; //Remove this, don't use global variables if you don't have to
      	     //Use the StudentCount variable we made in the menu function
      	     //Pass it as a parameter if a sub-function needs it.
   	 printf("Do you want to continue input student? y/n\n");
    	char c; 
      	scanf("%c", &c);
		ignoreEnter();
    	if(c == 'n')
        	break;
        } while(1);
}

You will have to edit the print, update student and delete student in a similar way, as they both need to know which student to print, update or delete. Also, you will need to add a way to mark certain array elements as used or deleted. Currently you can add 10 students, then delete student 5 and 7 for example and your StudentCount would be 8. Your program needs to know that it should skip 5 and 7 when printing or searching because they were deleted.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×