# switch loop not working

• 10-29-2008
dakarn
switch loop not working
My switch statement embedded in my do loop doesn't seem to execute correctly:

Code:

```#include <stdio.h> #include <math.h> int main() { double x, y, y2; int n_term; printf("Enter -1<x<1 : "); scanf("%lf",&x); y = log (x); printf("\nTrue value of log(1+x) = %.5f\n\n",y); do { printf("Enter an integer 1-4; 0 to Exit\n"); scanf("%d",&n_term); switch (n_term){ case '1':     printf("\n1 term approximation\n");     y2 = x;     printf("Approximate log(1+x) = %.5f",y2);     break; case '2': printf("\n2 term approximation\n"); y2 = x - (pow(x,2)/2); printf("Approximate log(1+x) = %.5f",y2); break; case '3': printf("\n2 term approximation\n"); y2 = x - (pow(x,2)/2) + (pow(x,3)/3); break; printf("Approximate log(1+x) = %.5f",y2); case '4': y2 = x - (pow(x,2)/2) + (pow(x,3)/3) - (pow(x,4)/4); break; case '0': break; default:           printf("unrecognized operator");           break;           } } while (n_term != 0); scanf("%lf",&x); }```
Instead of correctly recognizing the "#" term approximation, it prints a blank line. Anyone have some input?
• 10-29-2008
matsp
Actually, I would have expected it to work correctly if you enter 49, 50, 51 or 52.

I also expect it to print:
Code:

`printf("unrecognized operator");`
However, since that line doesn't end in a newline, it may not ACTUALLY get printed.

--
Mats
• 10-29-2008
tabstop
None of your printfs inside the case end with a \n character, so you probably won't ever see them until the end, when all the output gets flushed.
• 10-29-2008
ch4
Is this a problem for your algorithm ? Printf located after break
Code:

```y2 = x - (pow(x,2)/2) + (pow(x,3)/3); break; printf("Approximate log(1+x) = &#37;.5f",y2); case '4': y2 = x - (pow(x,2)/2) + (pow(x,3)/3) - (pow(x,4)/4); break;```
Check your cases, you read integer and you compare it with char.

Code:

```case '4': y2 = x - (pow(x,2)/2) + (pow(x,3)/3) - (pow(x,4)/4);//Printf expected break;```
• 10-29-2008
dakarn
Thanks for the input so far guys.

I modified the printfs to include a \n at the end of each one.

Now the problem is that it won't recognize the operator. This is the output:

Enter -1<x<1: .25

True value of log(1 + x) = -1.38629

Enter an integer 1-4; 0 to exit.
1
unrecognized operator
Enter an integer 1-4; 0 to exit.
2
unrecognized operator
...

etc

How can I allow my program to recognize my input?

edit: also when I remove the ' in my case '1', it still displays the same output.
• 10-29-2008
Salem
Consider learning how to indent before typing much more code.
Code:

```#include <stdio.h> #include <math.h> int main() {     double x, y, y2;     int n_term;     printf("Enter -1<x<1 : ");     scanf("&#37;lf",&x);     y = log (x);     printf("\nTrue value of log(1+x) = %.5f\n\n",y);     do {         printf("Enter an integer 1-4; 0 to Exit\n");         scanf("%d",&n_term);         switch (n_term){         case '1':             printf("\n1 term approximation\n");             y2 = x;             printf("Approximate log(1+x) = %.5f",y2);             break;         case '2':             printf("\n2 term approximation\n");             y2 = x - (pow(x,2)/2);             printf("Approximate log(1+x) = %.5f",y2);             break;         case '3':             printf("\n2 term approximation\n");             y2 = x - (pow(x,2)/2) + (pow(x,3)/3);             break;             printf("Approximate log(1+x) = %.5f",y2);         case '4':             y2 = x - (pow(x,2)/2) + (pow(x,3)/3) - (pow(x,4)/4);             break;         case '0':             break;         default:             printf("unrecognized operator");             break;         }     }     while (n_term != 0);     scanf("%lf",&x); }```
Clearer?
• 10-29-2008
tabstop
Read Mats' reply again, and recognize that '1' and 1 are not, in fact, the same thing.
• 10-29-2008
dakarn
I changed the "case '1'" to a "case 1:" but the output still only prints "unrecognized operator". Any way to circumvent this problem?

Thanks for the indents Salem! I'm coding on putty so I gave up on the proper indenting halfway though.
• 10-29-2008
dwks
 It should work, assuming you also changed case '2' to case 2 and so on. And keep in mind that any printf()s after break statements will not be executed. [/edit]

Also, since you're using a switch statement, you could use a fall-through switch to simplify the calculation and reduce some duplicate code. That's probably not the best idea, though, since you could get the same behaviour, but much more extensible, with a for loop. For example:
Code:

```for(i = 2; i < n; i ++) {     y2 -= (-1 * i) * (pow(i, x) / x); }```
Just a thought. And I'm sure you can come up with a more readable loop than I have provided.
• 10-29-2008
ch4
Quote:

Originally Posted by dakarn
I changed the "case '1'" to a "case 1:" but the output still only prints "unrecognized operator".

I changed it too but output doesn't include "unrecognized operator".