doesnt work

Why doesn't this program do what its supposed to do in c but it works in c++?

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream> // declare all of the libraries
#include <cstdlib>
#include <cmath>

using namespace std; // this is for cout and cin

// declare the variables
    double num;
    int operation;
    int howmany = 0;
    int howmanynum;
    int restart;
    int menuchoice;

class setup // a class to organize things
{
public:

    void menu()
{
    cout << endl << "Enter 1 to use the calculater." << endl << "Enter 2 for  important rules you need to know: ";
    cin >> menuchoice;
}

void rules()
{
    cout << endl << "Rule 1 - The calculator doesnt know order of operations so you have to enter the numbers in the correct order." << endl;
    cout << "Rule 2 - The calcualtor doesnt understand parenthesis so dont use them." << endl;
}

void operations()
{
    cout << endl << endl << "Here are your options for operations" << endl << endl; // give the user the operation choices
    cout << "Now enter the operation" << endl;
    cout << "Enter 1 for addition" << endl;
    cout << "Enter 2 for subtraction" << endl;
    cout << "Enter 3 for multiplication" << endl;
    cout << "Enter 4 for division " << endl;
    cout << endl << endl;
}

};

int main()
{

    do{ // begining of do while loop to restart the program

    int total = 0;

    setup callfunctions; // sets up a object of class setup to call functions

    callfunctions.menu(); // calls the function menu

    switch(menuchoice)
    {
    case 1:

    cout << endl << endl;

    callfunctions.operations();

    cout << "How many numbers are in the math problem: "; // asks the user how many numbers are in their math problem
    cin >> howmanynum;
    howmany = howmany + howmanynum;

    cout << endl << "Enter the first number: ";
    cin >> num;
    total = total + num; // adds the first number to the total, otherwise the answer would be incorect

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

    cout << endl << "Enter the number for the operation: "; // gets the operation from the user
    cin >> operation;
    cout << endl;

    cout << "Enter the next number: "; // gets the number from the user
    cin >> num;
    cout << endl;

    switch(operation){ // does all the math stuff
        case 1:
        total = total + num;
        break;

        case 2:
        total = total - num;
        break;

        case 3:
        total = total * num;
        break;

        case 4:
        total = total / num;
        break;

        default:
        cout << "Invalid input" << endl; // tells the user if he/she entered an invalid operation number
        }
    }
    cout << endl << "The answer is " << total << endl; // outputs the answer

    break;

    case 2:
    callfunctions.rules();
    break;
    }

    cout << "Enter 1 to restart or 2 to quit: "; // asks the user to restart or exit
    cin >> restart;
    }while(restart == 1); // ending of do while loop to restart program

    cin.get(); // pauses the program.Dont use system("pause")!!
    return 0; // termanates the 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;
    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;
}
C has no such headers as

1
2
3
#include <iostream> // declare all of the libraries
#include <cstdlib>
#include <cmath> 


and C has no classes.
bottom one is c not the top
Why doesn't this program do what its supposed to do in c but it works in c++?
Because in the C part the next number is (incorrectly) ask after the switch while in the c++ part before.

Means that in the c part you use the first num twice
ok it still doesnt work

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

int main()
{

    double num;
    double firstnum;
    double total = 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", &firstnum);
    total = total + firstnum;

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

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

        printf("\n\nEnter the next number: ");
        scanf("%lf", &num);

        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\nThe total is %lf", total);
    printf("\n\nPress any key to exit");

    getch();
    return 0;
}
?
What does not work?

I checked it with adding 1 2 3 and the result was correctly 6
on the c one? the answer for me is always 0 or -0
Topic archived. No new replies allowed.