c program not working

I know this is a c++ forum but i couldn't find a c forum. so here's my question why is the total always 0 at the end of this program.

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
#include <stdio.h>
#include <conio.h>

int main()
{

    double num;
    double total = 0.0;
    int howmanynum;
    int operation;

    printf("Here ore your options for operations for when you'll need them later:\n");
    printf("1 for addition\n2 for subtraction\n3 for multiplication\n4 for division");

    printf("\n\nNow enter how many numbers are in your math problem: ");
    scanf("%d", &howmanynum);

    printf("\n\nEnter the first number: ");
    scanf("%lf", &num);
    total = total + num;

    for(int i = 1; i < howmanynum; i++){

        printf("\n\nEnter the operation: ");
        scanf("%d", &operation);

        switch(operation)
        {
            case 1:
            total = total + num;
            break;

            case 2:
            total = total - num;
            break;

            case 3:
            total = total * num;
            break;

            case 4:
            total = total / num;
            break;
        }

        printf("\n\nEnter the next number: ");
        scanf("%lf", &num);
    }
    printf("\n\nThe total is %lf", total);
    printf("\n\nPress any key to exit");

    getch();
    return 0;
}
It's not.

If you enter 1,1 the total is 1. If you enter 2,1,1,2, the total is 2.

Your algorithm is broken, though. You need to rip it up and start again.

Last edited on
what do you mean its broken.
Topic archived. No new replies allowed.