Thread: Switch Statement - Possible Infinite Loop Problem?

  1. #1
    C Newbie
    Join Date
    Oct 2011
    Posts
    59

    Question Switch Statement - Need a better way to write a loop.?

    Code:
    #include <stdio.h>#include <math.h>
    
    
    int display_menu();
    
    
    int main()
    {
        int n, choice;
        int v, c;
    
    
        do
        {
            printf("Enter a whole number, n, between 1-20: ");
            scanf_s("%d", &n); //I'm still not sure why I have to do scanf_s.
        } while(n < 1 || n > 20);
        
        display_menu();
    
    
        printf("\nWhat would you like to do? ");
        scanf_s("%d", &choice);
    
    
        switch(choice)
        {
        case 1:
            for(c = 1; c <= n; c++)
            {
                printf("%d ", c);
            }
            break;
        case 2:
            for(c = n, v = 0; c >= 1; c--)
            {
                v += c;
            }
            printf("The sum of 1+2+3+...n = %d", v);
            break;
        case 3:
            c = 2;
            v = 0;
            do
            {
                v += pow(c,2);
                c + 2;
            }while(c <= n);
            printf("The sum of the square of all the even numbers 0 to n is %d", v);
            break;
        }
    
    
        getch();
        }
    
    
    int display_menu()
    {
        printf("\nMenu of Options:\n");
        printf("1.Display numbers 1 to n.\n");
        printf("2.Add up all the numbers 1 to n.\n");
        printf("3.Add up square of all the even numbers 0 to n.\n");
        printf("4.Add up 1 + 1 /2 + ... 1/n.\n");
        printf("5.Compute n!\n");
        printf("6.Quit\n");
        //Input: None.
        //Output: None.
        //Task: Display a "Menu of Options".
    }

    When I run the program and chose case 3, it idles there and doesn't print the desired answer, much like it is running through the loop infinitely. Any suggestions?
    Above is sovled.

    Code:
    case 4:
    		c = 0;
    		v = 0;
    		do
    		{
    			v += (1 / c);
    			c++;
    		}while(c <= n);
    		printf("The sum of 1 + 1/2 + ... 1/n. is: %f", v);
    		break;
    I am sure there is a better way of doing this, this being "The sum of 1 + 1/2 + ... 1/n", and would like to know what you guys think.
    Last edited by Alan Gott; 10-25-2011 at 06:26 PM. Reason: New Problem

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
            do
            {
                v += pow(c,2);
                c + 2;
            }while(c <= n);
    c + 2 doesn't change the value of c. You want c += 2 instead.
    If you understand what you're doing, you're not learning anything.

  3. #3
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by itsme86 View Post
    Code:
            do
            {
                v += pow(c,2);
                c + 2;
            }while(c <= n);
    c + 2 doesn't change the value of c. You want c += 2 instead.
    *facepalm* I should have caught that, no wonder it was infinite. Thanks for the fast reply!

  4. #4
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Ok, I am having problem with my next case now. (Edited OP).

  5. #5
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    http://i.imgur.com/kv32d.png Here's a pic to show what I am having problems with.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Could you post the code?
    If you understand what you're doing, you're not learning anything.

  7. #7
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by itsme86 View Post
    Could you post the code?
    Yea sure

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int display_menu(void);
    double compute_factorial(n);
    
    
    int main()
    {
        int n, choice;
        double v, c;
    
    
            printf("Enter a whole number, n, between 1-20: ");
            scanf_s("%d", &n); //I'm still not sure why I have to do scanf_s.
            if(n < 1 || n > 20)
            {
            do
            {
                printf("Invalid option: try again. ");
                scanf_s("%d", &n);
            }while(n < 1 || n > 20);
            }
        
        display_menu();
    
    
        printf("\nWhat would you like to do? ");
        scanf_s("%d", &choice);
    
    
        if(choice < 1 || choice > 6)
        {
        do
            {
                printf("Invalid option: try again. ");
                scanf_s("%d", &choice);
            }while(choice < 1 || choice > 6);
        }
    
    
        switch(choice)
        {
        case 1:
            for(c = 1; c <= n; c++)
            {
                printf("%.0lf ", c);
            }
            break;
        case 2:
            for(c = n, v = 0; c >= 1; c--)
            {
                v += c;
            }
            printf("The sum of all whole numbers 0 to n is: %.0lf", v);
            break;
        case 3:
            c = 2;
            v = 0;
            do
            {
                v += (c * c);
                c += 2;
            }while(c <= n);
            printf("The sum of the square of all the even numbers 0 to n is: %.0lf", v);
            break;
        case 4:
            c = 0;
            v = 0;
            do
            {
                v += (1 / c);
                c++;
            }while(c <= n);
            printf("The sum of 1 + 1/2 + ... 1/n. is: %lf", v);
            break;
        case 5:
            v = compute_factorial(n);
            printf("n! is: %.0lf", v);
            break;
        case 6:
            printf("Program will end now.");
            return 0;
            break;
        }
    
    
        getch();
        }
    
    
    int display_menu(void)
    {
        printf("\nMenu of Options:\n");
        printf("1.Display numbers 1 to n.\n");
        printf("2.Add up all the numbers 1 to n.\n");
        printf("3.Add up square of all the even numbers 0 to n.\n");
        printf("4.Add up 1 + 1 /2 + ... 1/n.\n");
        printf("5.Compute n!\n");
        printf("6.Quit\n");
        return 0;
        //Input: None.
        //Output: None.
        //Task: Display a "Menu of Options".
    }
    
    
    double compute_factorial(n)
    {
        int c;
        double v;
    
    
        for(c = 1, v = 1; c <= n; c++)
        {
            v *= c;
        }
        return v;
        //Input Parameter: A whole number
        //Output: Computed n!
        //Task: Compute N!
    }
    case 4 is what I am having problems with. It ends up saving 1.INF00 for v. :S

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Because you are dividing by zero on the first loop.
    Code:
    while(!asleep) {
       sheep++;
    }

  9. #9
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by TheBigH View Post
    Because you are dividing by zero on the first loop.
    /FACEPALM! I did that on a previous lab aswell. I should l2usedebug...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-30-2011, 01:59 PM
  2. infinite while loop inside a switch statement.
    By tummala_005 in forum C Programming
    Replies: 6
    Last Post: 12-15-2008, 05:46 PM
  3. switch-case,Infinite while-loop ,break
    By babu198649 in forum C++ Programming
    Replies: 8
    Last Post: 09-20-2008, 08:12 AM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. switch statement that uses loop
    By mike in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2002, 05:47 PM