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
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).
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.
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?
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.
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.