Trí Dương
Trí Dương

Reputation: 1

My program exit without running any coded functions even though it doesn't return any error (C Language)

this small project is for a assignment of mine and it seems to be not working properly even though the IDE doesn't return any error. After I successfully compiled the program, the program exit right away without running any line of codes

[this is what happened after I run the program][1]

Below are the codes of the program, thank you very much for the answer in advance!

(Edit): I have shortened down the program and fixed the problem Craig mentioned, here is the easier to view version:

In addition, there is a new problem popped up: there are a few scanf lines that got cut off and doesn't work. here is the image of said problem [The program does not let me to type in student's name][2]

(Edit 2): Thanks to Danelu that my program ran so much smoother now and then here comes another problem: my add function doesn't work as intended and it only takes 1 (or 2 but not all) input per loop (Eg. it takes the name data at the 1st loop and skipped to the 2nd loop, then the 2nd loop only takes studentID and skipped the rest to the 3rd loop) Can someone help me out with this problem? Much appreciated!

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

struct student{
    char name[50], studentID[50];
    float grade;
} std[30];

int n;

void add(){
    
    for (int i=0; i<n; i++){
        printf("\nAdd the students details no%d", i+1);
        printf("\n**********************************");
        printf("\nStudent's name: ");
        scanf("%s", &std[i].name);
        printf("\nStudent's ID: ");
        scanf("%s", &std[i].studentID);
        printf("\nStudent's grade: ");
        scanf("%f", &std[i].grade);
        system("cls");
    }
}

void displayRecords(){
    for (int i=0; i<n; ++i){
        printf("\n**************************");
        printf("\nStudent's name: %c", std[i].name);
        printf("\nStudent's ID: %c", std[i].studentID);
        printf("\nStudent's grade: %.2f", std[i].grade);
        printf("\n**************************\n");
    }
}

float max(){
    float maxGrade = std[0].grade;
    for (int i=0; i<n; i++){
        if(std[i].grade > maxGrade);{
            maxGrade = std[i].grade;
        }
    }
    return maxGrade;
}

float min(){
    float minGrade = std[0].grade;
    for (int i=0; i<n; i++){
        if(std[i].grade < minGrade);{
            minGrade = std[i].grade;
        }
    }
    return minGrade;
}

int main() {
    while(1){
    int a;
        printf("\t\t\t\t----------Welcome to SMS----------");
        printf("\n\t\t\t\t\tPlease choose your action");
        printf("\n\t\t\t\t1.Create new record");
        printf("\n\t\t\t\t2.View a record");
        printf("\n\t\t\t\t3.Find max grade");
        printf("\n\t\t\t\t4.Find min grade");
        printf("\n\t\t\t\t5.Exit");
        printf("\n\n\n\t\t\t\tYour choice: ");
        scanf("%d", &a);
    
    switch(a){
        case 1:
            system("cls");
            printf("Please enter the amount of records you want to add <1-30>: ");
            scanf("%d", &n);
            add(n);
            break;
        case 2:
            system("cls");
            displayRecords(n);
            break;
        case 3:
            system("cls");
            max(n);
            break;
        case 4:
            system("cls");
            min(n);
            break;
        case 5:
            system("cls");
            printf("\n\t\t\t\t\t\tSee you again next time!");
            return 0;
        default:
            system("cls");
            printf("Error! Please enter again");
            
        }
    }
}


  [1]: https://i.sstatic.net/VcP1O.png
  [2]: https://i.sstatic.net/YKuK2.png

Upvotes: 0

Views: 631

Answers (1)

Danelu
Danelu

Reputation: 71

Let me point out some things:

  1. Your struct members might not be what you want. Name and studentID are declared as char, but char has only 1 byte which is not enough to hold more than one character. So, for you I would recommend declaring both name and studentID as arrays of a fixed size.

    char name[50], studentID[50]

  2. If you want to print/scan more than one character at a time you have to use the %s format, not %c. Format specifiers

  3. When you call your functions you pass std as an argument, yet your functions do not accept any parameter. Depending on what you want to do, you can either declare the functions with one parameter or delete the argument in the called functions.

  4. I suppose you'd want to be able to access the students info after you saved them. Right now, each function has its own array of students that are not shared between them. This is called the scope of a variable, you can take a look at this Scope rules. The easy solution for you would be to create a global student array ( declared outside of functions ) and use it everywhere ( it's not always recommended to share data via a global variable, but in your case it won't hurt ).

Edit:
Hello again. There are a few more improvements to be made. Here are some quick improvements that should make your code runnable. This should be a good starting point.

  • std is a namespace and in my case the code would not compile as long as the student array would be named std, so I changed it to stud.

  • Again, your functions do not accept any parameter, yet you call them with one argument. What I've done is add one unsigned char parameter named noOfStudents that is used in the stop condition of the loop.

    • Why unsigned char? The number of records would never be a negative number and never higher than 30, so it's a good practice to use the smallest data type possible
  • Parameter added to the displayRecords function and %c changed to %s for name and studentID

  • Parameter added to the max and min functions and changed the initial value of the iterators i from 0 to 1 since the grade of the first student is already saved in the maxGrade and minGrade, comparing it to itself is pointless

  • Moved int n = 0 inside the main function. This means that the value of n is "visible" only inside main.

  • For the case 1 I added a do-while to check that the input value doesn't exceed your range of 1-30. You can find more about loops here

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

    struct student{
        char name[50], studentID[50];
        float grade;
    };
    
    struct student stud[30];

    void add(unsigned char noOfStudents){
        
        for (int i=0; i<noOfStudents; i++){
            printf("\nAdd the students details no%d", i+1);
            printf("\n**********************************");
            printf("\nStudent's name: ");
            scanf("%s", &stud[i].name);
            printf("\nStudent's ID: ");
            scanf("%s", &stud[i].studentID);
            printf("\nStudent's grade: ");
            scanf("%f", &stud[i].grade);
            system("cls");
        }
    }

    void displayRecords(unsigned char noOfStudents){
        for (int i=0; i<noOfStudents; ++i){
            printf("\n**************************");
            printf("\nStudent's name: %s", stud[i].name);
            printf("\nStudent's ID: %s", stud[i].studentID);
            printf("\nStudent's grade: %.2f", stud[i].grade);
            printf("\n**************************\n");
        }
    }

    float max(unsigned char noOfStudents){
        float maxGrade = stud[0].grade;
        for (int i=1; i<noOfStudents; i++){
            if(stud[i].grade > maxGrade);{
                maxGrade = stud[i].grade;
            }
        }
        return maxGrade;
    }
    
    float min(unsigned char noOfStudents){
        float minGrade = stud[0].grade;
        for (int i=1; i<noOfStudents; i++){
            if(stud[i].grade < minGrade);{
                minGrade = stud[i].grade;
            }
        }
        return minGrade;
    }

    int main() {
        while(1){
        int a = 0; /*user's choice*/
        int n = 0; /*no of records*/
        float minGrade = 0, maxGrade = 0;
            printf("\t\t\t\t----------Welcome to SMS----------");
            printf("\n\t\t\t\t\tPlease choose your action");
            printf("\n\t\t\t\t1.Create new record");
            printf("\n\t\t\t\t2.View a record");
            printf("\n\t\t\t\t3.Find max grade");
            printf("\n\t\t\t\t4.Find min grade");
            printf("\n\t\t\t\t5.Exit");
            printf("\n\n\n\t\t\t\tYour choice: ");
            scanf("%d", &a);
        
        switch(a){
            case 1:
                /*Always check what input the user gives you*/
                do
                {
                    system("cls");
                    printf("Please enter the amount of records you want to add <1-30>: ");
                    scanf("%d", &n);
                }while((n < 0) || (n > 30));
    
                add(n);
                break;
            case 2:
                system("cls");
                displayRecords(n);
                break;
            case 3:
                system("cls");
                /*max functions returns a value, save it into a local variable and print it*/
                maxGrade = max(n);
                printf("Max grade is: %f", maxGrade);
                break;
            case 4:
                system("cls");
                /*min functions returns a value, save it into a local variable and print it*/
                minGrade = min(n);
                printf("Min grade is: %f", minGrade);
                break;
            case 5:
                system("cls");
                printf("\n\t\t\t\t\t\tSee you again next time!");
                return 0;
            default:
                system("cls");
                printf("Error! Please enter again");
                
            }
        }
    }

Upvotes: 1

Related Questions