problem with nested switches

M

MZaza

Hello,

When I compile it I get an error in line 16, which got case 'c'.

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, y;
double r;
char o;

cout <<"To use the calculator press C" <<endl <<"To quite press Q"
<<endl;

switch (o);
{
case 'c': cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input"
<<endl;
}
break;

case 'q': cout <<"cya" <<endl;
break;
}

system("PAUSE");
return EXIT_SUCCESS;
}


Guys, I just started programming 2 days ago so take it easy on me :).
 
V

Victor Bazarov

MZaza said:
Hello,

When I compile it I get an error in line 16, which got case 'c'.

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, y;
double r;
char o;

cout <<"To use the calculator press C" <<endl <<"To quite press Q"
<<endl;

switch (o);

A semicolon? And what is the value of 'o' here?
 
T

Thomas J. Gritzan

MZaza said:
Hello,

When I compile it I get an error in line 16, which got case 'c'.

It would help if you provided us the error message you got.
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, y;
double r;
char o;

cout <<"To use the calculator press C" <<endl <<"To quite press Q"
<<endl;

switch (o);
^
Try to remove the semicolon here.
{
case 'c': cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;
[...]
 
M

michael.goossens

o doesnt seem to have a value and the ";" behind "switch(o);" doesn't
seem right.

But tbh switches aren't used that much, but guess you are just
learning :). Keep up the good job.
 
M

MZaza

o doesnt seem to have a value and the ";" behind "switch(o);" doesn't
seem right.

But tbh switches aren't used that much, but guess you are just
learning :). Keep up the good job.

Thanks :)
But I got a problem... the program worked faultlessly when I had only
one switch which is the one that got the cases for the mathematical
operations. After I added it in the other switch, which is the one
that got the menu shortcuts it doesn't work probably it just cout this
"To use the calculator press C" <<endl <<"To quite press Q" then it
quits..

Here is a screen shot,
http://img258.imageshack.us/img258/893/98124847qf2.jpg
 
M

michael.goossens

Thanks :)
But I got a problem... the program worked faultlessly when I had only
one switch which is the one that got the cases for the mathematical
operations. After I added it in the other switch, which is the one
that got the menu shortcuts it doesn't work probably it just cout this
"To use the calculator press C" <<endl <<"To quite press Q" then it
quits..

Here is a screen shot,http://img258.imageshack.us/img258/893/98124847qf2.jpg

do it with if {} else if {} else {} =). Those are nestable 100% and
easier.
 
M

MZaza

do it with if {} else if {} else {} =). Those are nestable 100% and
easier.

I've tried the if {} else {} and it's not working probably, it does
the if {} even if the condition doesn't match with it and matches with
the else {}.

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, y;
double r;
char o, c;

cout <<"To use the calculator press C" <<endl <<"To quite press Q"
<<endl;

if (cin >>c)
{
cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}
}

else
{
cout <<"Eorror";
}

system("PAUSE");
return EXIT_SUCCESS;
}
 
M

Micah Cowan

do it with if {} else if {} else {} =). Those are nestable 100% and
easier.

switches are also nestable 100%, and I disagree on the "easier".

AFAICT, the reason switch statements are rarer in C++ than in C is
that most in most of the use cases for it in C, polymorphic typing is
the better idiom in C++. However, for cases such as selecting on a
character value read on input (like this one), it seems like the most
straightforward approach, and I at least would find it more readable
than the equivalent in if/else-if.
 
M

MZaza

I've tried the if {} else {} and it's not working probably, it does
the if {} even if the condition doesn't match with it and matches with
the else {}.

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, y;
double r;
char o, c;

cout <<"To use the calculator press C" <<endl <<"To quite press Q"
<<endl;

if (cin >>c)
{
cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}
}

else
{
cout <<"Eorror";
}

system("PAUSE");
return EXIT_SUCCESS;

}

Any Ideas???
 
M

MZaza

I've tried the if {} else {} and it's not working probably, it does
the if {} even if the condition doesn't match with it and matches with
the else {}.

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, y;
double r;
char o, c;

cout <<"To use the calculator press C" <<endl <<"To quite press Q"
<<endl;

if (cin >>c)
{
cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}
}

else
{
cout <<"Eorror";
}

system("PAUSE");
return EXIT_SUCCESS;

}

Any Ideas???
 
M

MZaza

I've tried the if {} else {} and it's not working probably, it does
the if {} even if the condition doesn't match with it and matches with
the else {}.

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, y;
double r;
char o, c;

cout <<"To use the calculator press C" <<endl <<"To quite press Q"
<<endl;

if (cin >>c)
{
cout <<"Enter the mathmatical operation" <<endl;
cin >>x >>o >>y;

switch (o)
{
case '+': r=x+y;
cout <<"The result is: " <<r <<endl;
break;

case '-': r=x-y;
cout <<"The result is: " <<r <<endl;
break;

case '*': r=x*y;
cout <<"The result is: " <<r <<endl;
break;

case '/': r=x/y;
cout <<"The result is: " <<r <<endl;
break;

case '^': r=pow(x, y);
cout <<"The result is: " <<r <<endl;
break;

default: cout <<"Error: wrong input" <<endl;
}
}

else
{
cout <<"Eorror";
}

system("PAUSE");
return EXIT_SUCCESS;

}

Any Ideas???
 
L

lollinus

Hello,

When I compile it I get an error in line 16, which got case 'c'.

#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
float x, y;
double r;
char o;

cout <<"To use the calculator press C" <<endl <<"To quite press Q"
<<endl;

cin >> o;

You need to acquire first o;
switch (o);
^
There you go. Remove this semicolon and it should work fine.
 
V

Victor Bazarov

MZaza said:
[..]

Any Ideas???

Posted the same message three times within 5 minutes. It seem
you're desperate. But even if it's so there is no need to pollute
Usenet. Patience is a virtue. Newsgroups are not chat rooms.

V
 
M

MZaza

MZaza said:
Any Ideas???

Posted the same message three times within 5 minutes. It seem
you're desperate. But even if it's so there is no need to pollute
Usenet. Patience is a virtue. Newsgroups are not chat rooms.

V

@victor
There was a problem with my browser, when I refreshed to check if
there are new answers it reposted the message again.
 
J

Jerry Coffin

[ ... ]
AFAICT, the reason switch statements are rarer in C++ than in C is
that most in most of the use cases for it in C, polymorphic typing is
the better idiom in C++. However, for cases such as selecting on a
character value read on input (like this one), it seems like the most
straightforward approach, and I at least would find it more readable
than the equivalent in if/else-if.

There is also the fact that unlike C, C++ has things like std::map built
in, so you could do a job like this using code something like:

#include <iostream>
#include <map>
#include <string>

int i_add(int x, int y) { return x+y; }
int i_sub(int x, int y) { return x-y; }
int i_mul(int x, int y) { return x*y; }
int i_div(int x, int y) { return x/y; }

int main() {
int x, y;
std::string o;

typedef int (*op)(int, int);

std::map<std::string, op> ops;
ops["+"] = i_add;
ops["-"] = i_sub;
ops["*"] = i_mul;
ops["/"] = i_div;

std::cin >> x >> o >> y;
std::cout << "The result is: " << ops[o](x,y);
return 0;
}

Of course, this isn't anything like production code -- it's clearly
lacking in error checking and such, but you get the idea; the switch
statement is gone, and the code is (at least arguably) somewhat more
cleanly extensible.

In this case, you can do the job pretty easily without std::map though.
It's basically a sparse array type, but in the case of type char, you
can normally use a dense array without a major problem:

#include <iostream>
#include <map>
#include <limits.h>

int i_add(int x, int y) { return x+y; }
int i_sub(int x, int y) { return x-y; }
int i_mul(int x, int y) { return x*y; }
int i_div(int x, int y) { return x/y; }
int bad(int, int) { std::cout << "Error: Wrong input\n"; return 0; }

int main() {
int x, y;
char o;

typedef int (*op)(int, int);

op ops[CHAR_MAX];

for (int i=0; i<CHAR_MAX; i++)
ops = bad;

ops['+'] = i_add;
ops['-'] = i_sub;
ops['*'] = i_mul;
ops['/'] = i_div;

std::cin >> x >> o >> y;
std::cout << "The result is: " << ops[o](x,y);
return 0;
}

In theory#include <iostream>
#include <map>
#include <limits.h>

int i_add(int x, int y) { return x+y; }
int i_sub(int x, int y) { return x-y; }
int i_mul(int x, int y) { return x*y; }
int i_div(int x, int y) { return x/y; }
int bad(int, int) { std::cout << "Error: Wrong input\n"; return 0; }

int main() {
int x, y;
char o;

typedef int (*op)(int, int);

op ops[CHAR_MAX];

for (int i=0; i<CHAR_MAX; i++)
ops = bad;

ops['+'] = i_add;
ops['-'] = i_sub;
ops['*'] = i_mul;
ops['/'] = i_div;

std::cin >> x >> o >> y;
std::cout << "The result is: " << ops[o](x,y);
return 0;
}

Of course, if you happen to have a 64-bit char, this could be a bit of a
problem...
 
M

Micah Cowan

Jerry Coffin said:
Whoa, didn't realize I need to fix my email address when posting from
work. I _knew_ I should've posted to alt.test...
There is also the fact that unlike C, C++ has things like std::map built
in, so you could do a job like this using code something like:

<snip map-of-functions example>

Yeah. And that came to mind, but:
In this case, you can do the job pretty easily without std::map though.
It's basically a sparse array type, but in the case of type char, you
can normally use a dense array without a major problem:

Exactly. Which is no different from C.

The reason I didn't mention either of these is that they seemed to be
quite overkill for the current case.
 
P

Paavo Helde

Why floats? Default floating-point type is double.

This yields true always if the input succeeded, regardless of what input
it was. The code never checks for C or Q.


As here is no while() loop anywhere, the program exits immediately after
the first calculation.

Typing error.

Just for your information: this is not portable and not needed once you
get the Q check working.
Any Ideas???



hth
Paavo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top