0

Ive tried just making a lot of lines of different variables for entering courses, then the courses being assigned to credits and output on a receipt. But instead of having the 300+ lines of switches I wanted to make a switch function that I could call. I figured I would make a for loop to run the switch function based on the amount of courses the person chose, but when I run it the code seems to just run forever without even hitting a return 0 to end the program. Any tips for calling the function or if im doing it poorly?

int studentId, var, i;
float first;
int course, course1, course2;
float second, amount;
float credit1, credit2, credit3, credit4, credit5, credit6, credit7, credit8;
float paymentA, paymentB, paymentC, paymentD, total;
float creditA, creditB;
char *a;


void courseInfo(int myCourse){
    switch(myCourse)
    {
             case 4587:
                credit1 = 4;
                a = "MAT 236";
                break;
            case 4599:
                credit1 = 3;
                a = "COP 220";
                break;
            case 8997:
                credit1 = 1;
                a = "GOL 124";
                break;
            case 9696:
                credit1 = 3;
                a = "COP 100";
                break;
            case 4580:
                credit1 = 3;
                a = "MAT 230";
                break;
            case 4581:
                credit1 = 4;
                a = "MAT 231";
                break;
            case 4582:
                credit1 = 2;
                a = "MAT 232";
                break;
            case 4583:
                credit1 = 2;
                a = "MAT 233";
                break;
            case 3587:
                credit1 = 4;
                a = "MAT 256";
                break;
            case 4519:
                credit1 = 3;
                a = "COP 420";
                break;
            case 6997:
                credit1 = 1;
                a = "GOL 127";
                break;
            case 9494:
                credit1 = 3;
                a = "COP 101";
                break;
            default:
                printf("Sorry invalid entry!\n\n");
                }
        printf("%.2d\t%.2c\t%.2f\t\t$ %.2f\n", course, a, credit1 , credit1*120.25);
}

int main()
{


/*taking in the variables amounts*/





printf("Please enter Student ID:\n");
scanf("%i", &studentId);
printf("Enter how may courses-up to 3:\n");
scanf("%f", &amount);

for(i = 1; i = amount; i++){
    printf("Enter the %.2f course number(s)\n", amount);
    scanf("%i %i %i", &course, &course1, &course2);
    courseInfo(course);
    courseInfo(course1);
    courseInfo(course2);
    }

I just wanted the input to say 3 courses for example, you would input the 3 courses CRNs you wanted to take, it would run into the function and then print out the prices and what you were taking, then repeat the loop for the next 3. I figured if nothing was input like if someone chose 1 course, the course1 and course2 variables would not be assigned a value thus skipping the switch

6
  • 2
    Your exit condition for your for loop should be "i <= amount", not "i = amount" Commented Feb 6, 2019 at 3:14
  • 2
    You seem to default to using global variables, and float ones at that. I highly recommend you default to local integer variables. Commented Feb 6, 2019 at 3:16
  • 2
    Also, your "default" case in the switch should have "return" in there, as otherwise it goes to the printf when a and course1 are undefined (or not updated). Commented Feb 6, 2019 at 3:18
  • 1
    %i is (almost) always wrong, you want to use %d. Commented Feb 6, 2019 at 3:49
  • 1
    @AnttiHaapala "Wrong" is such a strong word. One should just be aware that %i interprets input prefixed with 0x as hex and input prefixed with 0 as octal. And it's also quite hard to confuse them ... general "%i"nteger, but %decimal. Commented Feb 6, 2019 at 4:43

2 Answers 2

1

Avoid global variables. Declare/define variables as close to where they're used as possible. In CourseInfo() you are using a uninitialized if the myCourse passed to the function does not match a case. Dereferencing an invalid pointer value is undefined behaviour in C and C++. Also the 2nd conversion specifier "%c" of the format string within the call to printf() inside CourseInfo() does not match the parameter passed (a), which is a pointer to char - format "%s".

Also

scanf("%i %i %i", &course, &course1, &course2);

is undefined behaviour since the three parameters are floats, not ints. The types of the parameters must match the conversion specifiers of the format string.

for(i = 1; i = amount; i++){
    printf("Enter the %.2f course number(s)\n", amount);
    scanf("%i %i %i", &course, &course1, &course2);
    courseInfo(course);
    courseInfo(course1);
    courseInfo(course2);
}

To ask the user to enter up to three courses:

int amount;

// ...

printf("Enter the %i course number(s):\n", amount);

for (int i = 0; i < amount; ++i) {
    int course;
    if (scanf(" %i", &course) == 1)
        courseInfo(course);    
}

If you want to save the course numbers for further use, use an array of int, make sure you don't write more elements than the size of that array and use it in the for-loop:

int courses[amount] = { 0 };  // initialize with all zeros to be able to tell
                              // later if a input operation failed.

for (int i = 0; i < amount; ++i) {
    int course;
    if (scanf(" %i", &courses[i]) == 1)
        courseInfo(course);    
}

With your current design there is no way of knowing outside of courseInfo() if a given course number is valid. Perhaps courseInfo() should return a bool (<stdbool.h>) depending on wheter the passed value is valid.

Sign up to request clarification or add additional context in comments.

Comments

0

change for loop

for(i = 1; i = amount; i++){

to

for(i = 1; i <= amount; i++){

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.