I just want to ask if is smart or even possible to use a switch case statement in a program where the user will not be required to input anything, but instead the program gets the input from a *.txt file.

This is a suggestion by my friend, I just wanted to get an experts opinion because I think its lunacy.

Recommended Answers

All 4 Replies

I just want to ask if is smart or even possible to use a switch case statement in a program where the user will not be required to input anything, but instead the program gets the input from a *.txt file.

This is a suggestion by my friend, I just wanted to get an experts opinion because I think its lunacy.

Sure. Why not? I've written many functions where the input was passed to the function and I used a switch statement inside of a function. How the value got into the variable I was "switching" on was something that my function didn't know about and didn't care about and was irrelevant in deciding whether to use a switch statement. I'd say your friend is right on this one.:)

Uh, I don't know what you mean, but here's one of the codes I'm talking about

fscanf(src, "%s", set);
        temp = fgetc(src);
        if(temp == EOF) break;
        if(strcmp(reg[0], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[1], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[2], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[3], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[4], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[5], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(acc[y], set)==0) fprintf(tok,"<acc>\n");
        else if(strcmp(bit[y], set)==0) fprintf(tok,"<bit>\n");
        else if(strcmp(rel[y], set)==0) fprintf(tok,"<rel>\n");
        else if(strcmp(byte[y], set)==0) fprintf(tok,"<byte>\n");
        else if(strcmp(data[y], set)==0) fprintf(tok,"<data>\n");
        else if(strcmp(flag[y], set)==0) fprintf(tok,"<flag>\n");
        else if(strcmp(addr[y], set)==0) fprintf(tok,"<addr>\n");
        else if(strcmp(comma[y], set)==0) fprintf(tok,"<comma>\n");
        else if(strcmp(memadd[0], set)==0) fprintf(tok,"<memadd>\n");
        else if(strcmp(memadd[1], set)==0) fprintf(tok,"<memadd>\n");
        else if(strcmp(ins[0], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[1], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[2], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[3], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[4], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[5], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[6], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[7], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[8], set)==0) fprintf(tok,"<ins>\n");
        else fprintf(tok,"<var>\n");

Is it possible to change the if else here into a switch case?

Uh, I don't know what you mean, but here's one of the codes I'm talking about

fscanf(src, "%s", set);
        temp = fgetc(src);
        if(temp == EOF) break;
        if(strcmp(reg[0], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[1], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[2], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[3], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[4], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(reg[5], set)==0) fprintf(tok,"<reg>\n");
        else if(strcmp(acc[y], set)==0) fprintf(tok,"<acc>\n");
        else if(strcmp(bit[y], set)==0) fprintf(tok,"<bit>\n");
        else if(strcmp(rel[y], set)==0) fprintf(tok,"<rel>\n");
        else if(strcmp(byte[y], set)==0) fprintf(tok,"<byte>\n");
        else if(strcmp(data[y], set)==0) fprintf(tok,"<data>\n");
        else if(strcmp(flag[y], set)==0) fprintf(tok,"<flag>\n");
        else if(strcmp(addr[y], set)==0) fprintf(tok,"<addr>\n");
        else if(strcmp(comma[y], set)==0) fprintf(tok,"<comma>\n");
        else if(strcmp(memadd[0], set)==0) fprintf(tok,"<memadd>\n");
        else if(strcmp(memadd[1], set)==0) fprintf(tok,"<memadd>\n");
        else if(strcmp(ins[0], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[1], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[2], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[3], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[4], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[5], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[6], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[7], set)==0) fprintf(tok,"<ins>\n");
        else if(strcmp(ins[8], set)==0) fprintf(tok,"<ins>\n");
        else fprintf(tok,"<var>\n");

Is it possible to change the if else here into a switch case?

I don't think switch would be a good fit here. In order to use "switch" on a variable, the variable's type must be an ordinal type. You are comparing strings, and string is not an ordinal type, so I don't see a switch statement working here.

In this case, no. A switch statement relies on different case statement values and your if statements do not have different values.

One problem I see in your if structure is what if 5 of the values in fact match set. This will only find 1 and stop. This may be what you want, but I thought I'd point it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.