HELP Trouble With Switch

Pages: 12
I'm having trouble calculating the score of the game. I'm not sure what's wrong so I posted the whole program. I changed the 10 cg's (computer generated numbers) to be 1 to 1... I think. I also have it set up in the switch so it's comparing the first users number to be 1.. which is the (test), which automatically equals 1. It should equal 1 when I enter one as my number... I'm am also aware that you can't compare number's like I did (n1==c1||c2||c3||)
But shouldn't it atleast output 1 point because of the test valuse... idk... really confused. Any question/help are appreciated.

The problem occurs in the switch... either I did it wrong, or it's not calculating it correctly. Please help, I really want to get this "game" working because it would be my first "big 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
float n1,n2,n3,n4,n5,n6,n7,n8,n9,n10;
void enter10numbers();
void errorscreen();
void finalerrorscreen();
int choice;
int pointcalculation;
int userspointtotal;
float p1=1; //point1
float t1,t2,t3,t4,t5,t6,t7,t8,t9,t10; //total1 etc.
int cg1,cg2,cg3,cg4,cg5,cg6,cg7,cg8,cg9,cg10; // computer's generated number
float test=1;
int main()

{
    string name;
    srand(time(0));
    cg1= rand()%1+1;
    cg2=rand()%1+1;
    cg3=rand()%1+1;
    cg4=rand()%1+1;
    cg5=rand()%1+1;
    cg6=rand()%1+1;
    cg7=rand()%1+1;
    cg8=rand()%1+1;
    cg9=rand()%1+1;
    cg10=rand()%1+1;



    cout<<"Please enter your name:"<<endl;
    cin>>name;
    cout<<name<<" the computer has picked 10 numbers between 1-50."<<endl;
    cout<<"You will choose 10 numbers 1-50, trying to guess what the computer has chosen."<<endl;
    cout<<"(You will be given a point for each number chosen correctly)"<<endl;
    enter10numbers();

{
    switch (pointcalculation){
    case 1:{
     if(n1==test)
    { t1=0+p1;
    }
    break;
    }
    case 2:{
     if(n2==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t2=0+p1;
    }
    break;
    }
    case 3:{
     if(n3==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t3=0+p1;
    }
    break;
    }
    case 4:{
     if(n4==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t4=0+p1;
    }
    break;
    }
      case 5:{
     if(n5==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t5=0+p1;
    }
    break;
    }
      case 6:{
     if(n6==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t6=0+p1;
    }
    break;
    }
      case 7:{
     if(n7==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t7=0+p1;
    }
    break;
    }
      case 8:{
     if(n8==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t8+0+p1;
    }
    break;
    }
      case 9:{
     if(n9==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t9=0+p1;
    }
    break;
    }
      case 10:{
     if(n10==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)
    { t10=0+p1;
    }
    break;
    }
}

    userspointtotal=t1+t2+t3+t4+t5+t6+t7+t8+t9+t10;
    cout<<"You've scored "<< userspointtotal <<" points against the computer!"<<endl;


}

    return 0;
    }
    void enter10numbers()
 {
    cout<<"Please start entering now (note numbers must be between 1-50)(Enter 0 to quit): "<<endl;
    cin>>n1;
    if(n1>50||n1<1)
    errorscreen();
    cin>>n2;
    if(n2>50||n2<1)
    errorscreen();
    cin>>n3;
    if(n3>50||n3<1)
    errorscreen();
    cin>>n4;
    if(n4>50||n4<1)
    errorscreen();
    cin>>n5;
    if(n5>50||n5<1)
    errorscreen();
    cin>>n6;
    if(n6>50||n6<1)
    errorscreen();
    cin>>n7;
    if(n7>50||n7<1)
    errorscreen();
    cin>>n8;
    if(n8>50||n8<1)
    errorscreen();
    cin>>n9;
    if(n9>50||n9<1)
    errorscreen();
    cin>>n10;
    if(n10>50||n10<1)
    errorscreen();



}
void errorscreen()
{

    cout<<"Are you sure you would like to exit the program? 1 for Yes 0 for No:"<<endl;
    cin>>choice;

    if (choice==1)
    exit(0);
    else
    finalerrorscreen();
    }

void finalerrorscreen()
{
    cout<<"Error"<<endl;
    cout<<"One or more numbers you have entered are to large to great."<<endl;
    enter10numbers();

}
Last edited on
So many problems here:
1) You use pointcalculation are your switch variable, but it is uninitialized.
2) You have no default branch in your case.
3) Your random numbers are always going to be 1.
rand()%1 will always return zero.
What's the remainder when you divide a number by 1?
4) Your if statements are broken:
 
if(n2==cg1||cg2||cg3||cg4||cg5||cg6||cg7||cg8||cg9||cg10)


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
Thanks for the help... I fixed the if statements, I'm not really sure what a default is, but I added it to be an invalid response. I changed the randomization to be 1-50. You said my pointcalculation is uninitialized, but I have it globalized at the top? Can you elaborate on what I would put for my defualt and the switch is still not working for me. I don't really want to post all the code, but i'm not getting any errors, just isn't calculating correctly.

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
float n1,n2,n3,n4,n5,n6,n7,n8,n9,n10;
void enter10numbers();
void errorscreen();
void finalerrorscreen();
int choice;
int pointcalculation;
int userspointtotal;
float p1=1;
float t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
int cg1,cg2,cg3,cg4,cg5,cg6,cg7,cg8,cg9,cg10;

int main()

{
    string name;
    srand(time(0));
    cg1= rand()%50+1;
    cg2=rand()%50+1;
    cg3=rand()%50+1;
    cg4=rand()%50+1;
    cg5=rand()%50+1;
    cg6=rand()%50+1;
    cg7=rand()%50+1;
    cg8=rand()%50+1;
    cg9=rand()%50+1;
    cg10=rand()%50+1;



    cout<<"Please enter your name:"<<endl;
    cin>>name;
    cout<<name<<" the computer has picked 10 numbers between 1-50."<<endl;
    cout<<"You will choose 10 numbers 1-50, trying to guess what the computer has chosen."<<endl;
    cout<<"(You will be given a point for each number chosen correctly)"<<endl;
    enter10numbers();


    switch (pointcalculation){
    case 1:{
     if(n1==cg1||n2==cg2||n2==cg3||n2==cg4||n2==cg5||n2==cg6||n2==cg7||n2==cg8||n2==cg9||n2==cg10)
    { t1=0+p1;
    }
    break;
    }
    case 2:{
     if(n2==cg1||n2==cg2||n2==cg3||n2==cg4||n2==cg5||n2==cg6||n2==cg7||n2==cg8||n2==cg9||n2==cg10)
    { t2=0+p1;
    }
    break;
    }
    case 3:{
     if(n3==cg1||n3==cg2||n3==cg3||n3==cg4||n3==cg5||n3==cg6||n3==cg7||n3==cg8||n3==cg9||n3==cg10)
    { t3=0+p1;
    }
    break;
    }
    case 4:{
     if(n4==cg1||n4==cg2||n4==cg3||n4==cg4||n4==cg5||n4==cg6||n4==cg7||n4==cg8||n4==cg9||n4==cg10)
    { t4=0+p1;
    }
    break;
    }
      case 5:{
     if(n5==cg1||n5==cg2||n5==cg3||n5==cg4||n5==cg5||n5==cg6||n5==cg7||n5==cg8||n5==cg9||n5==cg10)
    { t5=0+p1;
    }
    break;
    }
      case 6:{
     if(n6==cg1||n6==cg2||n6==cg3||n6==cg4||n6==cg5||n6==cg6||n6==cg7||n6==cg8||n6==cg9||n6==cg10)
    { t6=0+p1;
    }
    break;
    }
      case 7:{
     if(n7==cg1||n7==cg2||n7==cg3||n7==cg4||n7==cg5||n7==cg6||n7==cg7||n7==cg8||n7==cg9||n7==cg10)
    { t7=0+p1;
    }
    break;
    }
      case 8:{
     if(n8==cg1||n8==cg2||n8==cg3||n8==cg4||n8==cg5||n8==cg6||n8==cg7||n8==cg8||n8==cg9||n8==cg10)
    { t8+0+p1;
    }
    break;
    }
      case 9:{
     if(n9==cg1||n9==cg2||n9==cg3||n9==cg4||n9==cg5||n9==cg6||n9==cg7||n9==cg8||n9==cg9||n9==cg10)
    { t9=0+p1;
    }
    break;
    }
      case 10:{
     if(n10==cg1||n10==cg2||n10==cg3||n10==cg4||n10==cg5||n10==cg6||n10==cg7||n10==cg8||n10==cg9||n10==cg10)
    { t10=0+p1;
    }
    break;
    }
      default:
        cout<<"Invalid Response"<<endl;
}


    userspointtotal=t1+t2+t3+t4+t5+t6+t7+t8+t9+t10;
    cout<<"You've scored "<< userspointtotal <<" points against the computer!"<<endl;




    return 0;
    }
    void enter10numbers()
 {
    cout<<"Please start entering now (note numbers must be between 1-50)(Enter 0 to quit): "<<endl;
    cin>>n1;
    if(n1>50||n1<1)
    errorscreen();
    cin>>n2;
    if(n2>50||n2<1)
    errorscreen();
    cin>>n3;
    if(n3>50||n3<1)
    errorscreen();
    cin>>n4;
    if(n4>50||n4<1)
    errorscreen();
    cin>>n5;
    if(n5>50||n5<1)
    errorscreen();
    cin>>n6;
    if(n6>50||n6<1)
    errorscreen();
    cin>>n7;
    if(n7>50||n7<1)
    errorscreen();
    cin>>n8;
    if(n8>50||n8<1)
    errorscreen();
    cin>>n9;
    if(n9>50||n9<1)
    errorscreen();
    cin>>n10;
    if(n10>50||n10<1)
    errorscreen();



}
void errorscreen()
{

    cout<<"Are you sure you would like to exit the program? 1 for Yes 0 for No:"<<endl;
    cin>>choice;

    if (choice==1)
    exit(0);
    else
    finalerrorscreen();
    }

void finalerrorscreen()
{
    cout<<"Error"<<endl;
    cout<<"One or more numbers you have entered are to large to great."<<endl;
    enter10numbers();

}
I'm not really sure what a default is

A default branch in a switch statement is taken if the value of the switch variable is not covered by any of the cases.

You said my pointcalculation is uninitialized, but I have it globalized at the top

What value does it have? Simply because you made it global, does not ensure that it has a meaningful value.

Can you elaborate on what I would put for my defualt

Your default is fine.
So you're saying it should look like


switch (pointcalculation==1)

or

switch(pointcalculation=1)

Don't even know if that's right, but what's the signifigance of doing this?
Sorry for all the questions.
Neither.
 
switch(pointcalculation)
is the correct way to use it in a switch statement.
What I'm trying to point out is that you have to calculate pointcalculation before using it in a switch statement to determine which brach of the switch statement you want to execute. You're not setting pointcalculation anywhere.


Yes, now I see what you mean by assigning a value to this. Although I think I would put the value in the enter10numbers()... but in this case all I want it to do is calcuate the 10 values and assign points accordinly. I have worked with switches in other programs where assigning a value was more relevant. But i'm finding trouble seeing how I would implement it into this one. I'm very new and am learning out of a book and youtube vids.

Note I want all 10 cases to be executed.
Last edited on
If you want all 10 cases executed. Place the statements sequentially. Don't use a switch.
Thanks.
I used voids for each one and am confused on how to get it to work... I had it set up as if/else, but at some point the user will get point and it won't evaluate the next one. Here's what I have. didn't time to do the <> sorry will fix when i get home

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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
float n1,n2,n3,n4,n5,n6,n7,n8,n9,n10;
void enter10numbers(),errorscreen();
void finalerrorscreen();
void pointcalculation();
void calc1(),calc2(),calc3(),calc4(),calc5(),calc6(),calc7(),calc8(),calc9(),calc10();
int choice;
int userspointtotal;
float p1=1;
float t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
int cg1,cg2,cg3,cg4,cg5,cg6,cg7,cg8,cg9,cg10;

int main()

{
    string name;
    srand(time(0));
    cg1= rand()%2+1;
    cg2=rand()%2+1;
    cg3=rand()%2+1;
    cg4=rand()%2+1;
    cg5=rand()%2+1;
    cg6=rand()%2+1;
    cg7=rand()%2+1;
    cg8=rand()%2+1;
    cg9=rand()%2+1;
    cg10=rand()%2+1;



    cout<<"Please enter your name:"<<endl;
    cin>>name;
    cout<<name<<" the computer has picked 10 numbers between 1-50."<<endl;
    cout<<"You will choose 10 numbers 1-50, trying to guess what the computer has chosen."<<endl;
    cout<<"(You will be given a point for each number chosen correctly)"<<endl;
    enter10numbers();


    return 0;
    }
    void enter10numbers()
 {
    cout<<"Please start entering now (note numbers must be between 1-50)(Enter 0 to quit): "<<endl;
    cin>>n1;
    if(n1>50||n1<1)
    errorscreen();
    cin>>n2;
    if(n2>50||n2<1)
    errorscreen();
    cin>>n3;
    if(n3>50||n3<1)
    errorscreen();
    cin>>n4;
    if(n4>50||n4<1)
    errorscreen();
    cin>>n5;
    if(n5>50||n5<1)
    errorscreen();
    cin>>n6;
    if(n6>50||n6<1)
    errorscreen();
    cin>>n7;
    if(n7>50||n7<1)
    errorscreen();
    cin>>n8;
    if(n8>50||n8<1)
    errorscreen();
    cin>>n9;
    if(n9>50||n9<1)
    errorscreen();
    cin>>n10;
    if(n10>50||n10<1)
    errorscreen();

       calc1();

}
void errorscreen()
{

    cout<<"Are you sure you would like to exit the program? 1 for Yes 0 for No:"<<endl;
    cin>>choice;

    if (choice==1)
    exit(0);
    else
    finalerrorscreen();
    }

void finalerrorscreen()
{
    cout<<"Error"<<endl;
    cout<<"One or more numbers you have entered are to large to great."<<endl;
    enter10numbers();

}
void calc1()
{


     if(n1==cg1||n1==cg2||n1==cg3||n1==cg4||n1==cg5||n1==cg6||n1==cg7||n1==cg8||n1==cg9||n1==cg10)
        t1=0+p1;

        calc2();
}
void calc2()
{
    if(n2==cg1||n2==cg2||n2==cg3||n2==cg4||n2==cg5||n2==cg6||n2==cg7||n2==cg8||n2==cg9||n2==cg10)
        t2=0+p1;

        calc3();
}
void calc3()
{
    if(n3==cg1||n3==cg2||n3==cg3||n3==cg4||n3==cg5||n3==cg6||n3==cg7||n3==cg8||n3==cg9||n3==cg10)
        t3=0+p1;

        calc4();
}
void calc4()
{
    if(n4==cg1||n4==cg2||n4==cg3||n4==cg4||n4==cg5||n4==cg6||n4==cg7||n4==cg8||n4==cg9||n4==cg10)
        t4=0+p1;

        calc5();
}
void calc5()
{
    if(n5==cg1||n5==cg2||n5==cg3||n5==cg4||n5==cg5||n5==cg6||n5==cg7||n5==cg8||n5==cg9||n5==cg10)
        t5=0+p1;

        calc6();
}
void calc6()
{
    if(n6==cg1||n6==cg2||n6==cg3||n6==cg4||n6==cg5||n6==cg6||n6==cg7||n6==cg8||n6==cg9||n6==cg10)
        t6=0+p1;

        calc7();
}
void calc7()
{
    if(n7==cg1||n7==cg2||n7==cg3||n7==cg4||n7==cg5||n7==cg6||n7==cg7||n7==cg8||n7==cg9||n7==cg10)
        t7=0+p1;

        calc8();
}
void calc8()
{
    if(n8==cg1||n8==cg2||n8==cg3||n8==cg4||n8==cg5||n8==cg6||n8==cg7||n8==cg8||n8==cg9||n8==cg10)
        t8=0+p1;

        calc9();
}
void calc9()
{
   if(n9==cg1||n9==cg2||n9==cg3||n9==cg4||n9==cg5||n9==cg6||n9==cg7||n9==cg8||n9==cg9||n9==cg10)
    t9=0+p1;

        calc10();
}
void calc10()
{
   if(n10==cg1||n10==cg2||n10==cg3||n10==cg4||n10==cg5||n10==cg6||n10==cg7||n10==cg8||n10==cg9||n10==cg10)
    t10=0+p1;

    pointcalculation();

}
void pointcalculation()
{
     userspointtotal=t1+t2+t3+t4+t5+t6+t7+t8+t9+t10;
    cout<<"You've scored "<< userspointtotal <<" points against the computer!"<<endl;

}
Last edited on
I'm not going to comment further until you edit your post and apply code tags.
Last edited on
I'll have to wait till i get home
One glaring thing is the need for your program to use arrays - send a reference to them to your functions so that the values will change. That way you can use for loops to do the processing. What if your program had to handle 100 or 1,000 numbers? Are you going to write 1,000's of lines of code?

This is a concept that lies at the very heart of programming.

If you have lots of repetition in your code, it usually means there is a better way of doing things.

Do you have all the warnings turned on in your compiler? My compiler would have issued heaps of warnings about "variables not being declared at this scope". If you do have compiler warnings or errors always post them along with your code. Find out how to set them, and put them at their maximum level. I use the gcc compiler, and I use these warnings :

g++ -std=c++11 -Wall -Wextra -pedantic  ........... your stuff here


If I want to be particularly pedantic I can use -Werror , which promotes warnings to errors.

Anyway I will leave you in the very good hands of AbstractionAnon (Once you have done the code tags that is) :)
I haven't learned arrays yet.
No worries, have a look at the documentation section, top left of this page, there is a whole tutorial there for you to work through. There is also the reference section, which has details about all the functions & containers & algorithms.

Have a go with the arrays & for loops - it will make you life so much easier.

I didn't mean to sound really harsh in my previous post - I realise everyone has to start somewhere and programming can be a bit daunting to start with. Any way hope it all goes well for you & have fun !! :-D
Thanks, I'll definitly check out that tutorial. But right now i'm more worried about getting this program to work with the knowledge I know now, even though I bet I could shorten the lines and make it more compact.

I'm currently in c++ programming at my highschool and we're supposed to be making a game using randomization, which is what i'm trying to do. I don't really want to work ahead of the class and learn about arrays when we haven't covered them yet.

Thanks for the advice.
I've edited the code so it's readable. @AbstractionAnon
It works now, thanks for the help guys.
I know you marked this solved, but I really have to ask. Why the 10 nested levels of calls to the calc functions? You're really just executing the if statements sequentially.
I don't see any point to putting them into the calc functions.
We just learned voids like a weeks ago so I'm using them a lot now. But yeah I think I did that because it wasn't working before and I just tried everything and eventually just decided to evaluate them 1 by 1. Noob mistake.
Pages: 12