user2695709
user2695709

Reputation: 11

Addition and Assigned Values Wrong

I'm having a problem with my code. My program is an enrollment system and everytime I choose a switch case it should display the total price, but after picking the last subject to enroll, the total price addition seems wrong. Please help.

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

int main() 
{
    int n, none, ntwo, nthree, Total;
    float Algebra, Trigonometry, Calculus, Engiana, Physics;
    char password[20], username[8];
    Algebra = 100;
    Trigonometry = 300;
    Calculus = 500;
    Engiana = 750;
    Physics = 1500;
    Total = (none + ntwo + nthree);

    printf("Welcome to the Enrollment System \n");
    printf("Here is the list of Available Subjects \n");
    printf("\n");
    printf("Course Code         Price\n");
    printf("\n");
    printf("1. Algebra             %.2f \n",Algebra);
    printf("2. Trigonometry        %.2f \n",Trigonometry);
    printf("3. Calculus            %.2f \n",Calculus);
    printf("4. Engiana             %.2f \n",Engiana);
    printf("5. Physics             %.2f \n",Physics);

    printf("You can only select three courses to enroll for the Term \n");
    printf("\n Select First Course to Enroll \n");
    scanf("%d", &none);

    switch(none) 
    {
        case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause
        case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case
        case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); break;
        case 4: printf(" You Enrolled Engiana %.2f \n", Engiana); break;
        case 5: printf(" You Enrolled Physics %.2f \n", Physics); break;
        default: printf(" The Course you entered is not valid \n"); break;
    }
    printf("\n Select Second Course to Enroll \n");
    scanf("%d", &ntwo);

    switch(ntwo) 
    {
        case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause
        case 2: printf(" You Enrolled Trigonometry %.2f\n", Trigonometry); break; //if conditions not met goes to another case
        case 3: printf(" You Enrolled Calculus %.2f\n", Calculus); break;
        case 4: printf(" You Enrolled Engiana %.2f\n", Engiana); break;
        case 5: printf(" You Enrolled Physics %.2f \n", Physics); break;
        default: printf(" The Course you entered is not valid \n"); break;
    }

    printf("\n Select Third Course to Enroll \n");
    scanf("%d", &nthree);
    switch(nthree) 
    {
        case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause
        case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case
        case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); break;
        case 4: printf(" You Enrolled Engiana %.2f \n", Engiana); break;
        case 5: printf(" You Enrolled Physics %.2f \n", Physics); break;
        default: printf(" The Course you entered is not valid \n"); break;
    }

    printf("Total Tuition Price = %.2f \n",Total);
    system("PAUSE");
    return 0;
}

Upvotes: 1

Views: 148

Answers (3)

999k
999k

Reputation: 6565

There are mainly 2 mistakes

your Total variable is int datatype. So while printing you should use %d. For using %.2f you should make Total variable float

For finding total tuition price you should add Tuition fee in each case like this

 case 1:Total += Algebra; printf(" You Enrolled Algebra %.2f \n",Algebra); break; //if conditions met , immediately goes to system pause
 case 2:Total += Trigonometry;  printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); break; //if conditions not met goes to another case
 case 3:Total += Calculus;  printf(" You Enrolled Calculus %.2f \n", Calculus); break;
 case 4:Total += Engiana;  printf(" You Enrolled Engiana %.2f \n", Engiana); break;
 case 5:Total += Physics;  printf(" You Enrolled Physics %.2f \n", Physics); break;
 default: printf(" The Course you entered is not valid \n"); break;

Also you should intialise Total with 0 instead of (none + ntwo + nthree)

Upvotes: 1

Tyro Hunter
Tyro Hunter

Reputation: 755

Youre having a logical error. You need to place your assignment statement of your variable Total every after your scanf. But you have to update Total with the right amount in your switch statement.

example:

    case 1: printf(" You Enrolled Algebra %.2f \n",Algebra); Total += 100; break; //if conditions met , immediately goes to system pause
     case 2: printf(" You Enrolled Trigonometry %.2f \n", Trigonometry); Total += 300; break; //if conditions not met goes to another case
     case 3: printf(" You Enrolled Calculus %.2f \n", Calculus); Total += 500; break;
     case 4: printf(" You Enrolled Engiana %.2f \n", Engiana);Total += 750; break;
     case 5: printf(" You Enrolled Physics %.2f \n", Physics); Total += 1500; break;
     default: printf(" The Course you entered is not valid \n"); break;
     }
        printf("\n Select Second Course to Enroll \n");
        scanf("%d", &ntwo);`

PS: your code can be improved using loops..

Upvotes: 1

Karthik T
Karthik T

Reputation: 31952

Total = (none + ntwo + nthree);

This line should come last.

i.e the bottom of your code should look like this

    Total = (none + ntwo + nthree);
    printf("Total Tuition Price = %.2f \n",Total);
    system("PAUSE");
    return 0;
}

Remember that these instructions are executed in the order they are written, and in your original code, Total was calculated before the user had a chance to answer.

Btw I understand you are likely just starting to learn, but this is a maxim that I follow, If you are copy pasting more than a line of your own code, you can do better. Once you get to functions and arrays and structs, perhaps you will revisit this and try to cut out any sort of duplication of code.

The other two answerers have pointed out an equally valid problem, you are currently just adding the users input, and you probably want to be adding the cost (?) of the course themselves.

Upvotes: 2

Related Questions