Can anyone help me with switch loop?

The code compiles and I don't get any error the problem is the loop when you choose for the first time the program does what is asked to, but starting at the second loop begins to jump over the cases instead of adding is dividing the numbers.. and in a few loops instead to infinite run it breaks..
What is it wrong.. ?

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

int n1, n2;
int add, sub, mult;
double diviz;
int options;

int addNumbers(int a, int b);
int substrNumbers(int a, int b);
int multNumbers(int a, int b);
int divNumbers(int a, int b);
void menu()
{
    printf("\n\tInput 2 numbers: \n");
    printf("\n\ta = ");
    scanf("%d", &n1);
    printf("\n\tb = ");
    scanf("%d", &n2);

    printf("\n\tChose an option:\n"
           "\n1. Add numbers"
           "\n2. Subtract numbers"
           "\n3. Multiply numbers"
           "\n4. Divide numbers\n\n\t");
    scanf("%d", &options);
}
int main()
{
    menu();
    switch(options)
    {
        case 1:
            {
                add = addNumbers(n1, n2);
                printf("\na + b = %d\n", add);
                Sleep(2000);
                system("cls");
                menu();
            }
        case 2:
            {
                sub = substrNumbers(n1, n2);
                printf("\na - b = %d\n", sub);
                Sleep(2000);
                system("cls");
                menu();
            }
        case 3:
            {
                mult = multNumbers(n1, n2);
                printf("\na X b = %d\n", mult);
                Sleep(2000);
                system("cls");
                menu();
            }
        case 4:
            {
                diviz = divNumbers(n1, n2);
                printf("\na : b = %lf\n", diviz);
                Sleep(2000);
                system("cls");
                menu();
            }
        default:
            printf("\nNo such option !\n");
            Sleep(2000);
            system("cls");
            menu();
    }
    menu();
}
int addNumbers(int a, int b)
{
    int result;
    result = a + b;
    return result;
}
int substrNumbers(int a, int b)
{
    int result;
    result = a - b;
    return result;
}
int multNumbers(int a, int b)
{
    int result;
    result = a * b;
    return result;
}
int divNumbers(int a, int b)
{
    double result;
    result = a / b;
    return result;
}
but starting at the second loop begins to jump over the cases instead of adding is dividing the numbers


I don't see any loops in main(). Can you point out the line # where you think the loop begins and ends?

Also, you should know that the way you've written C++ is C-like (in terms of how user input is gathered). Have you used cin, see [2]? If not, why not?

If you *must* use C-style input/output then before using scanf to get user input, read [1]. Scanf has a lot of gotchas and I wouldn't recommend it for beginners (you need to know about format specifier characters, some regular expression, and must be aware of buffer overflows).

[1] http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
[2] https://www.w3schools.com/cpp/cpp_user_input.asp
Last edited on
there is no loop in that code.
calling menu does not move you back to the top of the program; it calls menu and then resumes from where it was called.

if you want a loop, you need a loop:
for
while
do while
are the loops in c++

also, divide is broken. integer division results in an integer, which you cast to a double and then back to an integer. 3/4 is still zero. if you want .75 you need to return the double, not int, and you need to force division as doubles, by casing a or b either one to double.

you also don't need all those stray variables.
consider:

double divNumbers(int a, int b)
{
return a/(double)b;
}

is this intended to be C or C++; it mostly looks like C?
Last edited on
It is C not C++ , I was refer to the switch statement when I was saying loop... But yaah that's not a loop.. sorry my mistake..

I'll try to replace it with a while loop.. thank you both for the advice.
I did a mistake I just erased the code..
Last edited on
ok I think I solve it..

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

double n1, n2;
int add, sub, mult;
double diviz;
int options;

int addNumbers(int a, int b);
int substrNumbers(int a, int b);
int multNumbers(int a, int b);
double divNumbers(double a, double b);
void menu()
{
    printf("\n\tInput 2 numbers: \n");
    printf("\n\ta = ");
    scanf("%lf", &n1);
    printf("\n\tb = ");
    scanf("%lf", &n2);

    printf("\n\tChose an option:\n"
           "\n1. Add numbers"
           "\n2. Subtract numbers"
           "\n3. Multiply numbers"
           "\n4. Divide numbers\n\n\t");
    scanf("%d", &options);
}
int main()
{
    menu();
    int i;
    for(i = 0; i < options;)
        if(options == 1)
            {
                add = addNumbers(n1, n2);
                printf("\na + b = %d\n", add);
                Sleep(2000);
                system("cls");
                menu();
            }
        else if(options == 2)
            {
                sub = substrNumbers(n1, n2);
                printf("\na - b = %d\n", sub);
                Sleep(2000);
                system("cls");
                menu();
            }
        else if(options == 3)
            {
                mult = multNumbers(n1, n2);
                printf("\na X b = %d\n", mult);
                Sleep(2000);
                system("cls");
                menu();
            }
        else if(options == 4)
            {
                diviz = divNumbers(n1, n2);
                printf("\na : b = %lf\n", diviz);
                Sleep(2000);
                system("cls");
                menu();
            }
        else
            {
                printf("\nNo such option !\n");
                Sleep(2000);
                system("cls");
                menu();
            }
    menu();
}
int addNumbers(int a, int b)
{
    int result;
    result = a + b;
    return result;
}
int substrNumbers(int a, int b)
{
    int result;
    result = a - b;
    return result;
}
int multNumbers(int a, int b)
{
    int result;
    result = a * b;
    return result;
}
double divNumbers(double a, double b)
{
    double result;
    result = a / b;
    return result;
}


for me works.. so thank you for your time. :)
glad you got it.

FYI c++ does not recommend C-strings and printf, scanf, C headers like stdio.h (its <cstdio> for c++),

and you still don't need all those result variables but its fine, just harmless bloat.
Last edited on
Topic archived. No new replies allowed.