Question:
I want to use swith case in vC++ using char * is it possible or not and how?
mgpwagle
2007-08-20 05:04:38 UTC
char *NS;
NS = section[i].nominal_diameter;

switch(NS)
{
case '0':
case '1-1/4':
in this
section[i] in retun int
and is char nominal_diameter
this (NS) vaule come from one class;

....
...
Four answers:
Novatica
2007-08-20 05:17:27 UTC
No you can't!



From INCITS ISO/IEC 14882:2003 (2nd Ed.):

"Any statement within the switch statement can be labeled with one or more case labels as follows:

case constant-expression :

where the constant-expression shall be an integral constant-expression."
cruppstahl
2007-08-20 14:10:30 UTC
no, you can only switch with numerical integer data types - but that's ok for you. your problem is this:



char *NS;

NS = section[i].nominal_diameter;

switch(NS) {

case '0':



in the switch clause, you compare char * with a char literal ('0') - that's your problem. You have to compare char to char, not char * to char.



but this works (not tested):

switch (*NS) {

case '0':



or this:

char NS=*section[i].nominal_diameter;

switch (NS) {

case '0':
Sachin
2007-08-20 12:09:09 UTC
You cannot have

char *NS;

switch(NS)

{

case '0':

}



you can do this-

char NS;

switch(NS)

{

case '0':

}
2007-08-20 12:19:59 UTC
It's not possible ..



Replace the nominal_diameter with a enum data type..


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...