Home > Blockchain >  I am stuck on implementing the WHILE repetition structure and SWITCH selection structure to create a
I am stuck on implementing the WHILE repetition structure and SWITCH selection structure to create a

Time:09-04

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

#define Size_MainFuncArray 5            // Constant

                                 // Data that needs to be inside my txt file:
                                // StudentData.txt: Jane 55 51 78
struct Student {                        // Lungelo 69 84 75
    char StudentName[10];               // Greg 51 44 52
    int StudentMarks[3];                // Thando 23 78 61
    float StudentAverage;               // Bret 44 33 29
};

typedef struct Student Student;

                      // Function Prototypes
int DisplayMenu(void);
void Read_Data(Student Arr[], int Size);
void Calculate_StudentAverage(Student Arr[], int Size);
void Display_Data(Student Arr[], int Size);
void Write_Graph(Student Arr[], int Size);  // I am not being able to identify

                                               // The type like: int, flot etc.
                               // All are just specified as Student Arr[].
                               // My lecture wants it like this

                         // Main Function
int
main(void)
{
    int choice;
    Student ESG206B[Size_MainFuncArray];    // How do I use this array?

    choice = DisplayMenu();

    printf("\nPress any key to continue...\n");
    getch();
    system("cls");

    return 0;
}

     // Function Definitions
int
DisplayMenu(void)
{
    int Select;

    printf("************************************\n");
    printf("*    Welcome to Student Stats      *\n");
    printf("*    Student Number: 123456789     *\n");
    printf("************************************\n");

    printf("\n1. Load Data and Calculate Average\n");
    printf("2. Dispaly Student Data\n");
    printf("3. Save Graph\n");
    printf("4. Exit\n");
    printf("Choice: ");
    fflush(stdin);
    scanf("%d", &Select);

    return Select;
}

void
Read_Data(Student Arr[], int Size)      // I am not being able to finish this
{                                       // functions definition

}

void
Calculate_StudentAverage(Student Arr[], int Size)
{

}

void
Display_Data(Student Arr[], int Size)
{

}

void
Write_Graph(Student Arr[], int Size)
{

}

CodePudding user response:

You could wrap your menu choices' printf and scanf in a do...while loop, whose condition could check if the input is valid (1 <= input <= 4).

Example:

int DisplayMenu(void)
{
    int Select;

    printf("************************************\n");
    printf("*    Welcome to Student Stats      *\n");
    printf("*    Student Number: 123456789     *\n");
    printf("************************************\n");

    do
    {
        printf("\n1. Load Data and Calculate Average\n");
        printf("2. Dispaly Student Data\n");
        printf("3. Save Graph\n");
        printf("4. Exit\n");
        printf("Choice: ");
        fflush(stdin);
        scanf("%d", &Select);
        if (Select < 1 || Select > 4)
        {
            printf("Please select a valid choice.\n");
        }
    } while (Select < 1 || Select > 4);

    return Select;
}

Then you could use the switch structure to separate the different choices.

Example:

choice = DisplayMenu();
switch (choice)
{
case 1:
    // do smth for 1
    break;
case 2:
    // do smth for 2
    break;
case 3:
    // do smth for 3
    break;
case 4:
    // do smth for 4
    break;
default:
    break;
}

CodePudding user response:

You wanted "a while() implementation"? Here is an example using an infinite loop to interact with the user.

It's a good idea to have the "menu" close to the program's responses to that menu items. The reader can compare that menu item #2 appears to call the appropriate function.

There are a few other comments sprinkled into this code.

int
main(void)
{
    Student ESG206B[Size_MainFuncArray];

    printf("************************************\n");
    printf("*    Welcome to Student Stats      *\n");
    printf("*    Student Number: 123456789     *\n");
    printf("************************************\n");

    while( 1 ) { // infinite loop, ends with user input = 4
        putchar( '\n' );
        printf("1. Load Data and Calculate Average\n");
        printf("2. Dispaly Student Data\n");
        printf("3. Save Graph\n");
        printf("4. Exit\n");
        printf("Choice: ");
        // fflush(stdin); // DO NOT DO This!

        int choice = 0; // declare variables close to their use
        scanf( "%d", &choice );
        // should check return value from scanf

        switch( choice ) {
            case 1:
                Read_Data( ESG206B, Size_MainFuncArray );
                Calculate_StudentAverage( ESG206B, Size_MainFuncArray );
                break;

            case 2:
                Display_Data( ESG206B, Size_MainFuncArray );
                break;

            case 3:
                Write_Graph( ESG206B, Size_MainFuncArray );
                break;

            case 4:
                printf( "Bye-bye\n" );
                return 0; // program ends

            default:
                printf( "Invalid selection\n" );
                break;
        }
    }

    return 0;
}

/*
 * The unwritten code is still yours to write.
 */
  • Related