need help with nested switch

i have a problem with a nested switch.
Copy and paste into c++ and see what i mean, questions need to be over 20 though so you can see when things go wrong.
[code]
switch(op1)
{
case 0: sum = n1 + n2; break;
case 1: sum = n1 - n2; break;
case 2: sum = n1 * n2; break;
case 3: sum = n1 / n2; break;
}

}
else if ((i>=20)&&(i<40))
{
switch(op1)
{
case 0: {sum = n1 + n2;}
switch(op2)
{
case 0: sum=sum+n3;break;
case 1: sum=sum-n3;break;
case 2: sum=sum*n3;break;
case 3: sum=sum/n3;break;
}
case 1: {sum = n1 - n2;}
switch(op2)
{
case 0: sum=sum+n3;break;
case 1: sum=sum-n3;break;
case 2: sum=sum*n3;break;
case 3: sum=sum/n3;break;
}
case 2: {sum = n1 * n2;}
switch(op2)
{
case 0: sum=sum+n3;break;
case 1: sum=sum-n3;break;
case 2: sum=sum*n3;break;
case 3: sum=sum/n3;break;
}
case 3: {sum = n1 / n2;}
switch(op2)
{
case 0: sum=sum+n3;break;
case 1: sum=sum-n3;break;
case 2: sum=sum*n3;break;
case 3: sum=sum/n3;break;
}
}
The sum seems right but the answer is always wrong :S
Last edited on
closed account (3TXyhbRD)
Is variable1 declared in the instruction block or it's just an assignment?
i have reposted the switch code. copy and paste and see what i mean.
Last edited on
closed account (3TXyhbRD)
Found the error. When you make the nested switch, you don't break after any case for switch(op1).
have you copied and pasted it? it works fine until the 20th question. Where should i put the break then? what line? Thank you for your help btw

-EDIT-
i found the lines. Such an easy mistake :P
Thank you for that.
Last edited on
closed account (3TXyhbRD)
This is from line 36, commented where I added breaks.
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
switch(op1)
{
case 0: {sum = n1 + n2;}
    switch(op2)
    {
    case 0: sum=sum+n3;break;
    case 1: sum=sum-n3;break;
    case 2: sum=sum*n3;break;
    case 3: sum=sum/n3;break;
    }
    break; // Here
case 1:  {sum = n1 - n2;}
    switch(op2)
    {
    case 0: sum=sum+n3;break;
    case 1: sum=sum-n3;break;
    case 2: sum=sum*n3;break;
    case 3: sum=sum/n3;break;
    }
    break; // Here
case 2: {sum = n1 * n2;}
    switch(op2)
    {
    case 0: sum=sum+n3;break;
    case 1: sum=sum-n3;break;
    case 2: sum=sum*n3;break;
    case 3: sum=sum/n3;break;
    }
    break; // Here
case 3: {sum = n1 / n2;}
    switch(op2)
    {
    case 0: sum=sum+n3;break;
    case 1: sum=sum-n3;break;
    case 2: sum=sum*n3;break;
    case 3: sum=sum/n3;break;
    }
    break; // And here
}


And yea, I copied and pasted the code. However VS gives some errors about op2 being used without being initialized. For testing the stuff I called your function (find_Sum).
I dont get any error like that. I am using eclipse on a mac
closed account (3TXyhbRD)
It may be a VS thing. VS also generates warning if I specify, for a method, what type of exceptions may be thrown from it. Or maybe it's Eclipse that's missing something. I had a go at Eclipse with CDT, but it's buggy and switched to MonoDevelop on Ubuntu then Qt Creator.
Eclipse is nice, however it has it's issues with C++ sometimes.
i have only just started c++ in the last couple of months. I have only used eclipse for things i have done.
closed account (3TXyhbRD)
Ah, well in that case it's still good. Problems I got in Eclipse were when including files (sometimes it refuses to include files, must be something about the file, magic special characters or something) and when using templates, it wouldn't recognize the template.
Funny thing about Eclipse is that if I copy the code and paste it instead of using #include, it works. Weird behavior, that's why I decided to stop using it and look for something, less buggy... It may be because of UNIX/Windows file format (lately I use both Windows and a UNIX distribution) and mixing files may bug Eclipse, however VS doesn't have this issue, nor does Qt Creator.
What I can say about Eclipse is that it's an awesome IDE for Python, it really works well with it.
Are you at high school/college or university? Or just learning C++ in spare time?
University but i try and do my own programs as well. how about you? it seems like you know a lot about this. btw do you think there's a way i could so this programme simpler?
closed account (3TXyhbRD)
Same here, University year 1 out of 3. However I have meet with C/C++ in high school and when I got to uni it wasn't all news to me.
About making the program simpler, either split it into functions (small functions, if they are to long they may be to complicated to understand). Or, since it's C++, use objects, I don't know how far you are into C++, but it offers strong support for OOP. But all at their time, jumping right into OOP may skip some important steps about how C++ works and will surely give headaches (and not just easy ones).
Have you learned other programming languages before?
i have messed around with java and made a Lamborghini car laps game and i just started the programming course this spring and i have my final on tuesday. I will finish the programme as it is but if i have enough time i will try the way you told me.
closed account (3TXyhbRD)
Nice! I have a Java book around here, however I haven't found the time to read it yet (shame on me). Good luck on the final! I start somewhere in June.
Topic archived. No new replies allowed.