Thread: switch loop not working

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  4. #4
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    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;
    Last edited by ch4; 10-29-2008 at 10:43 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    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.
    Last edited by dakarn; 10-29-2008 at 11:43 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read Mats' reply again, and recognize that '1' and 1 are not, in fact, the same thing.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    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.
    Last edited by dakarn; 10-29-2008 at 11:50 AM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [edit] 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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User ch4's Avatar
    Join Date
    Jan 2007
    Posts
    154
    Quote Originally Posted by dakarn View Post
    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".
    Better post your code again.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    ...and please follow Salem's lead by posting indented code

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I replaced the 'x' with x, and it behaves as I expect from the code I see. As I don't know exactly what it actually is intended to do, I don't know if the results are sane or not - it does look a bit wrong with getting 0.42 as an approximation when the "correct" result is -0.53, but I don't know for sure.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to loop in switch??
    By pczafer in forum C++ Programming
    Replies: 6
    Last Post: 05-04-2009, 01:53 AM
  2. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 02:13 AM
  3. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 05:46 PM
  4. case switch not working
    By AmbliKai in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 06:42 AM
  5. loop not working
    By Jarrette in forum C++ Programming
    Replies: 12
    Last Post: 04-27-2003, 11:34 PM