Program help. Assigning wrong values

Hi I'm new to C programming and i was wondering if you could help me with this,
in my program im asking for three subjects to enroll and total it every time i choose a switch case but the addition seems wrong please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
 #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;
}
 
line 13 :
Total = (none+ntwo+nthree);
you are using those variables without initialization.

i guess you want to move this line and place it right after line 61.
I suppose Total is printing the wrong number?

Look at what you're doing:
1
2
3
   
    int n,none,ntwo,nthree,Total;
    Total = (none+ntwo+nthree);


The compiler sees a calculation, not an expression/function, and it's evaluated at this exact moment. However, at this moment, none, ntwo and nthree are uninitialized (= garbage data). The sum of garbage is garbage.

Two ways to fix this:
1) Move the calculation to the end. Once none, ntwo and nthree have been set, you can calculate it (move line 13 to line 62).
2) You can keep your conceptual idea and replace the variable Total with a function Total:

1
2
3
4
5
6
 
// Line 13:
int Total() { return none + ntwo + nthree; }  // Total is now a function

// Line 62:
printf("Total Tuition Price = %.2f \n",Total()); // Total must be called as a function 
Hello. Thank you for your suggestion but it seems the problem still occurs instead of adding the values. for example i chose case 1 for "none" and case 2 for "ntwo" and case 3 for "nthree" the total doesn't add them just shows the price for the last case, "nthree".
closed account (28poGNh0)
This what @Rechard3 means

pay attention to comments they can help you too
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# include <stdio.h>
# include <stdlib.h>


int main()
{
    //You dont use those variables n,password[20],username[8] so delete them
    int none=0,ntwo=0,nthree=0;//You should initialize none, ntwo and nthree because you used them to calculate total
    double Total;//total must be double
    float Algebra,Trigonometry,Calculus,Engiana,Physics;

    Algebra = 100;
    Trigonometry = 300;
    Calculus = 500;
    Engiana = 750;
    Physics = 1500;

    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;
    }

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


also your switches has the same statements ,maybe you consider to implement a function for them
Last edited on
Total should be an int, because it's the sum of ints.

none, ntwo and nthree don't necessarily need to be initialized, as long as the calculation is done at the right moment. Please refer to my previous post.
Sorry for my ignorance. Thank you for the help! :) @Techno01 sorry but the code only adds the choices it comes out as 6 instead of 100+300+500 if i chose case 1 case 2 case 3 for none,ntwo, and nthree respectively
closed account (28poGNh0)
Do you want the total to be 100+300+500?

@Gaminic That's what I tought but @Justin Segovia wants the "Total tuition price to be and .2f , and I only did what he request
What i wanted to do is let the ("none","ntwo", and "nthree") assume the values for each subject.
e.g expected output
Select First Course to Enroll
1
You Enrolled Algebra
Total Tuition Price = 100
Select Second Course to Enroll
3
You Enrolled Calculus
Total Tuition Price = 600
closed account (28poGNh0)
To facilitate the job I implemented a function
hope that's what are you looking for

PS : you dont need to turn your variables to float or double in this case just int will do the job

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# include <stdio.h>
# include <stdlib.h>

int Algebra = 100,
    Trigonometry = 300,
    Calculus = 500,
    Engiana = 750,
    Physics = 1500;

int enrollFunc(int);

int main()
{

    int none=0,ntwo=0,nthree=0,Total;

    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             %d \n",Algebra);
    printf("2. Trigonometry        %d \n",Trigonometry);
    printf("3. Calculus            %d \n",Calculus);
    printf("4. Engiana             %d \n",Engiana);
    printf("5. Physics             %d \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);

    none = enrollFunc(none);

    printf("\n Select Second Course to Enroll \n");
    scanf("%d", &ntwo);

    ntwo = enrollFunc(ntwo);

    printf("\n Select Third Course to Enroll \n");
    scanf("%d", &nthree);

    nthree = enrollFunc(nthree);

    Total = (none+ntwo+nthree);

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

int enrollFunc(int choice)
{
    switch(choice)
    {
        case 1:
            printf(" You Enrolled Algebra %d \n",Algebra);
        return Algebra;
        case 2:
            printf(" You Enrolled Trigonometry %d \n", Trigonometry);
        return Trigonometry;
        case 3:
            printf(" You Enrolled Calculus %d \n", Calculus);
        return Calculus;
        case 4:
            printf(" You Enrolled Engiana %d \n", Engiana);
        return Engiana;
        case 5:
            printf(" You Enrolled Physics %d \n", Physics);
        return Physics;
        default:
            printf(" The Course you entered is not valid \n");
        break;
    }exit(-1);// Cause the program to exit
}


hope that helps
Thank you so much! :) You're of great help. Sorry for the troubles i have caused. Have a nice day :)
Topic archived. No new replies allowed.