If and else issues

I've made a command-line program in Xcode using C++. The program generates a random integer number between 1-10. If the value matches any of the variables a-j, the program will generate a new random number until it does not match any of the a-j variables, and will then move on to the switch. If the sum of the a-j variables equals 55 (i.e. all categories in the switch have been executed), the program will reset the a-j variables to 0. I expect the program to execute all ten cases of code in the switch for the variable random, and then continue doing so over and over until I stop the program. However, when I try executing the program, it stops after only executing the ten cases once.

Here is my code:

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
#include <iostream>
#include <stdio.h>      /* printf, scanf, puts, NULL */
#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */
#include <cmath>
#include <ctime>
#include <math.h>

using namespace std;

int main(int argc, const char * argv[])
{
    srand(unsigned(time(0)));
    int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, random, totalt;
    
slumpa:
    random = (rand()%10)+1;
    totalt = a + b + c + d + e + f + g + h + i + j;
    if (random == a || random == b || random == c || random == d || random == e || random == f || random == g || random == h || random == i || random == j) {
        goto slumpa;
    } else if (totalt == 55) {
        a = 0;
        b = 0;
        c = 0;
        d = 0;
        e = 0;
        f = 0;
        g = 0;
        h = 0;
        i = 0;
        j = 0;
    } else {
        
    }
    
    switch (random) {
        case 1:
            cout<<"Category 1"<<endl;
            a = 1;
            goto slumpa;
            break;
        case 2:
            cout<<"Category 2"<<endl;
            b = 2;
            goto slumpa;
            break;
        case 3:
            cout<<"Category 3"<<endl;
            c = 3;
            goto slumpa;
            break;
        case 4:
            cout<<"Category 4"<<endl;
            d = 4;
            goto slumpa;
            break;
        case 5:
            cout<<"Category 5"<<endl;
            e = 5;
            goto slumpa;
            break;
        case 6:
            cout<<"Category 6"<<endl;
            f = 6;
            goto slumpa;
            break;
        case 7:
            cout<<"Category 7"<<endl;
            g = 7;
            goto slumpa;
            break;
        case 8:
            cout<<"Category 8"<<endl;
            h = 8;
            goto slumpa;
            break;
        case 9:
            cout<<"Category 9"<<endl;
            i = 9;
            goto slumpa;
            break;
        case 10:
            cout<<"Category 10"<<endl;
            j = 10;
            goto slumpa;
            break;
    }
}


What could the problem be?

UPDATE: I just found out what caused the loop. After the initial execution of all ten cases in the switch, the program started a loop at "goto slumpa;" in the "if (random == a || random == b..." statement (because the variables a-j had set values in the range 1-10, the random number would obviously equal any of those values.). I fixed this by moving up the "if (totalt == 55)..." statement above the other if statement, and the program now works as expected. I'm marking this thread as resolved.
Last edited on
at line 32 nothing is inside else u might wanted to create switch inside else

and also goto function is something people doesnt advices
Last edited on
The empty else at line 32 is intentional. It should execute the switch even if any of the first two if statements (line 19 and 21) are true.
the mistake you did is an algoritmic one.
you said when you run the code it is doing it for once and not diong it over again and stops right. that means your program keeps running but it is stuck at some point. and if you look carefully. you will see it stucks here

1
2
3
4
5
if (random == a || random == b || random == c || random == d ||
              random == e || random == f || random == g || random == h ||
              random == i || random == j) {
        goto slumpa;
    }


when it comes there because values arent equal to zero it goes back to slumpa and never checks if your totalt is equal to 55.

so it should be like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (totalt == 55) {
        a = 0;
        b = 0;
        c = 0;
        d = 0;
        e = 0;
        f = 0;
        g = 0;
        h = 0;
        i = 0;
        j = 0;
    }else if (random == a || random == b || random == c || random == d ||
                 random == e || random == f || random == g || random == h || 
                 random == i || random == j) {
        goto slumpa;
    }


checking for isDone functions always should be at the top
and this is why people doesnt advices goto function
Last edited on
Topic archived. No new replies allowed.