2

This program is expected to do a few things: 1. Add new student using ID 2. Search for student by ID 3. Search for student by name 4. List all the students 5. Display list of students who achieved 80 marks and above

This is what I have done so far

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 200
int i;
int id;

/*Declaration of structures*/
typedef struct student{
    char studID[20];
    char name[50];
    char address[80];
    char tel_no[15];
    float marks; 
} stud;

int add_Name ();
int menu_Display ();
int get_Menu ();
int get_Option ();
int stud_id();
int stud_Name();

int main() {
    menu_Display ();
    get_Menu ();
    get_Option ();
    getchar();

    return 0;
}

int menu_Display () {
    int choice=0;
    while (choice!=6) {
        get_Menu ();
        choice = get_Option();
    }
}

int get_Menu (){
    int i;
    for (i=0; i<1; i++) {
        printf ("\t1. Insert new student\n\n");
        printf ("\t2. Search for a particular student by ID\n\n");
        printf ("\t3. Search for a particular student by name\n\n");
        printf ("\t4. Display a list of all students \n\n");
        printf ("\t5. Display a list of students achieving 80.0 marks and above\n\n");
        printf ("\t6. Exit\n\n");
    }
}

int get_Option () {
    int option;
    printf ("Please enter your option...");
    scanf ("%d", &option);
}

int menu_Option (int option) {
    int i;
    switch (option) {
        case 1 : add_ID();
        break;
        case 2 : stud_id();
        break;
        case 3 : stud_Name();
        break;
        case 4 : list_students();
        break;
        case 5 : students_marks();
        break;
        case 6 : printf("Exit\n");
        break;
        default : printf ("Wrong input");
        i=getchar();
        break;
    }
}

int add_ID () {
    int i;
    struct student stud[200];
    for (i=0; i<1; i++){
        strcpy(stud[i].studID,"ID00");

        printf("Enter Name:\n");
        scanf("%s", &(stud[i].name)) ;

        printf("Enter address:\n");
        scanf("%s", &(stud[i].address));

        printf("Enter telephone number:\n");
        scanf("%s", &(stud[i].tel_no));

        printf("Enter marks:\n"); 
        scanf("%f", &(stud[i].marks));
    }      
}

int stud_Name() {
    int i;
    int result;
    char name[20];

    printf("Please enter the name of the student\n");
    gets(name);

    for (i = 0; i < MAX; i++) {
        if ((result = strcmp(stud[i].name, name)) == 0){
            printf("%s \t %s \t %s \t %s \t %.2f \n", stud[i].studID, stud[i].name, stud[i].address, stud[i].tel_no, stud[i].marks);
        }

    if (i == MAX) {
        printf ("Match not found\n");
    }

    i=getchar();
    }
}

int stud_id() {
    int i;
    int result;
    char ID[20];

    printf("Please enter the Student ID which you are searching for\n");
    gets(ID);

    for (i = 0; i < MAX; i++) {
        if ((result = strcmp(stud[i].studID, ID)) == 0){
            printf("%s \t %s \t %s \t %s \t %.2f \n", stud[i].studID, stud[i].name, stud[i].address, stud[i].tel_no, stud[i].marks);
        }

    if (i == MAX) {
        printf ("Match not found\n");
    }

    i=getchar();
    }
}

int list_Students(){
    printf("Student ID \t Name \t Address \t Telephone number \t Marks \n");

    for (i = 0; i < MAX; i++) {
        printf("%s \t %s \t %s \t %s \t %.2f \n", stud[i].studID, stud[i].name, stud[i].address, stud[i].tel_no, stud[i].marks);

    }   
}

I have encountered a few errors while trying to compile the program. The error shown was "Expected expression before 'stud'" in functions stud_Name, stud_id and list_Students. I have tried to modify the code but nothing works. Am I using strcmp the right way? Thanks

littlepony
  • 23
  • 1
  • 6
  • As a side note, you'd better have your compiler with the highest warning settings (it seems you're using GCC/Clang so `-Wall -Wextra`). It'll help you spot (lots of) subtle mistakes that you're unfortunately committing (`gets`, wrong parameter specifier for `scanf`, etc.) – edmz Mar 13 '16 at 14:33
  • several of the posted functions have a signature that says they return an `int`, but the actual bodies of the function do not end with a `return value;` statement. – user3629249 Mar 14 '16 at 22:39
  • When compiling, always enable all the warnings, then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` I also use: `-Wconversion -std=gnu99` – user3629249 Mar 14 '16 at 22:40
  • in C, when referencing an array name, the result is the address of the first byte of the array, so when calling `scanf()` with a second parameter of an array name, do not prepend the name with `&`. – user3629249 Mar 14 '16 at 22:44
  • When calling `scanf()` and the format string (first parameter) contains a '%s' format specifier, a max length modifier must be used otherwise, the user can easily overflow the input buffer, resulting in undefined behaviour. Since the `scanf()` function will append a NUL byte to the input buffer, the max length modifier must be 1 less than the length of the input buffer. The '%s' format specifier will not read past any 'white space', so the format string should contain a leading space. As an example: `scanf("%s", &(stud[i].name)) ;` should be: `scanf(" %49s", stud[i].name) ;` – user3629249 Mar 14 '16 at 22:49
  • do NOT name your variables the same as a intrinsic nor typedef defined type name. I.E. do not name your variable(s) `stud` – user3629249 Mar 14 '16 at 22:52
  • Why is the code giving all the students the same ID? the `marks` field could contain a GPA but is not suitable for entering the individual test scores. the `name` field is nicely sized, but is not suitable for entering firstName middleName lastName. Suggest three fields and three calls to `scanf()` .vs. the one call to `scanf()` and only able to enter a single name] – user3629249 Mar 14 '16 at 22:57
  • the function prototypes, for functions that take no parameters, should have `void` between the parens, otherwise the compiler expects a variable list of parameters. – user3629249 Mar 14 '16 at 23:01
  • Function names should indicate purpose, usually with a active verb followed by a noun. The function: `get_Menu()` is not indicating what the function does. Suggest something like: `display_Menu()` because that is what it does. The function: `menu_Display` does not display the menu. Rather it does handle the menu processing. Suggest `process_Menu()`. the function: `get_Option()` is well named. The function: `menu_Option()` would be better named `process_MenuSelection()` because that is much closer to what the function does. – user3629249 Mar 14 '16 at 23:07
  • certain functions, like `get_Option()` are called in more than one place, however the program logic indicates they should only be called in one place. There are several places in the posted code that contain: `i=getchar();` however, the variable `i` new contents are not used and there is no need to input an extra char from stdin. Suggest removing those unneeded calls to `getchar()` – user3629249 Mar 14 '16 at 23:11

2 Answers2

1

stud is an alias of struct type student, using the [] operator on it doesn't make sense. You probably want an array of student structs, MAX of them. Which can be done with

student stud[MAX];

Side note : using gets is considered bad practice because it can easily be harmful, consider fgets.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
  • `gets()` is not even in the latest C standard. The main reason it is considered harmful is because it does not limit the length of the input so any input buffer can be easily overflowed, resulting in undefined behaviour leading to a seg fault event. – user3629249 Mar 14 '16 at 23:14
1

You have used typedef to name your struct to stud. typedef does not create variable, it allows you to create variable without word struct.

For example:

typedef struct NameOfStruct { } ShortName;
struct NameOfStruct var1; // Creates variable named var1 
ShortName var2; // Creates variable named var2

It seems you want to use stud as a global variable. In such case, you should remove line struct student stud[200]; from add_ID().

You can create global variable stud like this (note that this does not use word typedef):

struct student{
    char studID[20];
    char name[50];
    char address[80];
    char tel_no[15];
    float marks; 
} stud[200];
VLL
  • 9,634
  • 1
  • 29
  • 54