You are here:

C++/hello! i have now created...

Advertisement


Question
hello!
i have now created a structure:
struct gateType {
      char type[5];
      int input1PinOrder;
      int input2PinOrder;
      int outputPinOrder;
      };
      
struct gateType myGates[8][3];

but when i write the following:
void gemismaboard(int i,int j) {
  switch (myGates[j][i].type)      {
     case "and":
     ....;
     break;
     case "or":
     ...;
     break;

     default:
     printf("wrong!");
     break;   
     }
}

the compiler says it's wrong because :"the switch quantity is not an integer and "case label does not reduce to an integer constant"
what should i do in order to check out what the myGates[j][i].type includes?? thanks!

Answer
Maki,
Sorry for the delay in my response - drama with my roommate and the girls he deals with tends to throw me outta wack. This is a strange error. While I never personally have tried to use a switch statement with text as oppose to numbers, I don't see anything wrong with the code. However, it's possible that a switch statement won't use letters, it will only use numbers (which logically doesn't make sense).

It's possible that the switch statement can't take data from within a struct as an input for the statement. What I would do is try to use the switch statement with just a variable of type char. If that works, then the issue is trying to take the data from within the struct and use it in the switch statement. If this is the problem, copy the data out of the struct, then use it in the switch statement. It's not the most effective solution, but it should work. If you need any more help, please let me know. Things are finally quiet over here. :)

Best Of Luck,
Alan

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Alan F.

Expertise

Having problems with any basic or intermediate parts of C++? I can answer those questions for you using simple English. I have been taking C++ in school for a couple years, so I still remember what it was like to be a beginner (especially with C++, which is a difficult language)!

Experience

I have a C++ web page that discuses the basics of C++. The address is: http://www.afweb.net/complang/

©2016 About.com. All rights reserved.