### 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".

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170`` ``````#include #include #include #include #include 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:"<>name; cout<>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:"<>choice; if (choice==1) exit(0); else finalerrorscreen(); } void finalerrorscreen() { cout<<"Error"<
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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174`` ``````#include #include #include #include #include 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:"<>name; cout<>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:"<>choice; if (choice==1) exit(0); else finalerrorscreen(); } void finalerrorscreen() { cout<<"Error"<
 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

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180`` ``````#include #include #include #include #include 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:"<>name; cout<>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:"<>choice; if (choice==1) exit(0); else finalerrorscreen(); } void finalerrorscreen() { cout<<"Error"<
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.

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