Thread: C loop

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    1

    C loop

    Code:
    #include <stdio.h>
    //starting//
    int main (void)
    {
        char code;//variables and declaration//
        int num, num1, total;
        for ( ; code!=0; )
        printf("Menu\n");
        printf("--------------------------------------\n");
        printf("0 = Quit\n");
        printf("1 = odd or even number\n");
        printf("2 = Count up\n");
        printf("3 = Count down\n");
        printf("Enter a menu number:");
        scanf(" %c", &code);
            
    switch (code)
    {
        
        case '1':{
            printf("Enter a number to determine odd or even:");
            scanf("%d", &num);
            
            if (num%2==0)
                printf("%d is an even number", num);
            else 
                printf("%d is a odd number:", num);
            }break;
        case '2':
            {printf("Enter the 1st number:\n");
            scanf("%d", &num);
            printf("Enter the 2nd number:\n");
            scanf("%d", &num1);
            
            if (num>num1)
                printf("Sorry, the 1st number should be smaller for this task.\n");
            else
            {    
                printf("Increasing sequence is\n");
                for (; num<=num1; num++)
                {
                    printf(" %d", num);
                }
                total=((num1-num+1)*0.5)*(num+num1);
                printf("Sum is %d", total);
            }
            }break;
        case '3':
            {    printf("Enter the 1st number:\n");
                scanf("%d", &num);
                printf("Enter the 2nd number:\n");
                scanf("%d", &num1);
                if (num<num1)
                    printf("Sorry, the 1st number should be bigger for this task.\n");
                else
                {
                printf("Decreasing sequence is ");
                for (; num>=num1; num--)
                    {
                    printf(" %d", num );
                    }
                total=((num-num1+1)*0.5)*(num+num1);
                printf("Sum is %d", total);
                }
                }break;
        case'0':
            printf("Program terminated. Thanks. ");break;
    return (0);
    }
    }
    /
    i cant place the loop..where should i place it?i want it to loop the menu back when the user is done.and also the sum of the increasing sequence..please help me >.<

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Normally, you have main() calling your menu() function. Don't have to, of course, it's just normal.

    Then you display your menu options to the user. Get his choice, and use your switch statement to:

    1) Do what the user's choice would need

    and then

    2) break. The break statement just stops the switch statement from "falling through" to all the lower options.

    End result of the user choosing anything but the quit option on the menu, is that the menu is again displayed, anew.

    So a BIG loop is needed, extending before and after the menu() options and the switch statement. Pseudo-code:

    Code:
    do {
    
       print "menu options: Please choose one"
       print user's choices 1, 2, 3, etc. 5 to quit.
       switch statement for the user's choices, here
       (other functions typically would be called to handle the different user's choices)
       (but they all come back here, inside the do while loop.)
    
    } while(choice != 5);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM