Home > Net >  C: Initialize variable to random # b/w 50 & 100 so they can be used in Switch statement if user sele
C: Initialize variable to random # b/w 50 & 100 so they can be used in Switch statement if user sele

Time:10-11

So i'm very much a beginner and this is an assignment for class, but i'm not looking to have someone do the assignment for me or anything. Just help on a part i'm having trouble with. I'm not posting my code as it's an ongoing assignment and I don't want someone to happen upon it and copy it ): But the gist of it is I need to display a menu to the user and create a switch statement. Each case has a corresponding function prototype that executes the choice the user made from the menu.

1 Enter 3 grades

2 Show average (with 3 grades) and letter grade

3 Show highest grade

4 Show lowest grade

5 Exit

I've done pretty much all of the assignment, but the one requirement I can't figure out is how to initialize the 3 grade variables to random numbers between 50 and 100, so if the user chooses menu options 2 3 or 4 first then those random #'s are what is used in my prototypes. But if the user chooses menu option 1, my functions should use the 3 values input by the user from that point until exit, or if they hit 1 again to input new values.

Since I couldnt figure it out I just had each prototype prompt the user to insert 3 grades then proceed to do its assigned task using those values.

We were also instructed to not use arrays as we havent gotten to that yet.

If no one is able to figure it out without seeing the code i'll wait until after the due date and post what I was able to do. i'm honestly just wanting to learn and my professor doesn't really post any videos or lectures (online class) so we just go off our textbook and good ol google.

Thank you to whoever can help(:

CodePudding user response:

If you want a variable with a random standard value, initialize it with a random value. You can generate a random integer between two numbers using the random() function in the stdlib.h header.

Your code can be structured like this.

#include<stdlib.h>
#include<time.h>

int random_range(int start, int end) {
    return start   (int) ((double) random() / RAND_MAX * (end - start));
}

int main() {
    srandom(time());
    int grade1 = random_range(50, 100);
    int grade2 = random_range(50, 100);
    int grade3 = random_range(50, 100);
    ...
}

Now the three grade variables are always initialized and can be used. I recommend, you also read the man page for the random() function.

CodePudding user response:

Not a good idea, doing processing with some randomly assigned data simply to not do the work of controlling the user's options. Imagine that one option is to write the current record into persistent storage. Do you want to risk random valued records polluting the company's database? There are stories of "test versions" that have been released "into the wild" (on the day the coder was home with a cold, but management applied pressure to ship) that... well, that would curl your toes.

Here is a sketch whereby the user has two options only: enter the data or quit the program. Presuming valid data has been accepted, the menu features more options.

Do not process junk. It'll come back to bite you.

/* Usual header files */
int main() {
    char buf[ 64 ];

    int hiMenuOpt = 1;
    do {
        printf(
            "Here's menu options\n"
            "0: Quit\n"
            "1: Enter data\n"
            );
        if( hiMenuOpt > 1 )
            printf(
                "2: Something\n"
                "3: Something else\n"
                "4: Something other\n"
                );

        printf( "Select 0-%d : ", hiMenuOpt );
        fgets( buf, sizeof buf, stdin );
        int val = strtol( buf, NULL, 10 ); // Edit fix. Thanks to Lundin
        if( val < 0 || hiMenuOpt < val ) {
            printf( "Bad entry\n" );
            continue;
        }
        switch( val ) {
            case 0:
                hiMenuOpt = 0;
                break;
            case 1:
                puts( "Hi from one\n" );
                /* yadda yadda */
                hiMenuOpt = 4;
                break;
            /* More stuff */
        }
    } while( hiMenuOpt );

    return 0;
}
Here's menu options
0: Quit
1: Enter data
Select 0-1 : 1
Hi from one

Here's menu options
0: Quit
1: Enter data
2: Something
3: Something else
4: Something other
Select 0-4 :

Notice that Quit is now item 0. New menu items may be added, and old ones removed. The only constant is exiting the program. It makes sense, imo, to make that the first item on the list.

  • Related