Thread: do while loop error

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    6

    Wink do while loop error

    I'm not quite sure on what I'm doing wrong, it runs but just keeps displaying the displayMenu with any number and once I choose any number larger than 4 its displays my favorite food. This is my very first coding class ever so if anyone can dumb down the explanation it would be very helpful

    Here is the assignment
    Use a do/while statement to display a menu.
    - loop in main function
    - calls displayMenu function to display menu
    - based on the choice entered call the corresponding function
    - 1 to display your name
    - 2 to display your favorite color
    - 3 to display your favorite food
    - exit the loop when user enters ANY number except 1 through 3 ( complex condition)
    - displayMenu
    - take no arguments, returns integer (choice selected by the user)
    - menu options
    - 1 name
    - 2 favorite color
    - 3 favorite food


    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    
    int displayMenu(int answer);
    void displayName();
    void displayColor();
    void displayFood();
    int main(void)
    {
                     unsigned int counter = 0;
                     int option = 0;
                     int num = 0;
    
                     do 
                     {
                                   option = displayMenu(num);
                      } 
            while (option <= 3)
                     {
                       if (option == 1)
                                 {
                                                   displayName();
                       }
                                    else if (option == 2)
                       {
                                                   displayColor();
                        }
                                    else if (option = 3)
                       {
                                                   displayFood();
                                   }
                                                 else
                                                 {
                                                        printf("\n");
                              }
                                   }
    }
    int displayMenu(int answer) 
    {
             int choice;
    
                        puts("Choose a number to display:");
                        printf("%s", "1-display name, 2-to dsiplay favorite color, 3-   
                                   display favorite food, any other number to quit:");
                        scanf("%d", &choice);
    
                        return choice;
    }
    void displayName()
    {
                        puts("Tiffany");
    }
    void displayColor()
    {
                        puts("Red");
    }
    void displayFood()
    {
                         puts("Anything Japanese");
    }

  2. #2
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Does this code compile? On my system, it doesn't. I can see a few problems with your code. Your style of indentation makes it a bit hard to read. First off, I believe your do while loop is incorrect. A good example and explanation of a do / while loop is shown here: For, While and Do While Loops in C - Cprogramming.com
    It says the format is:
    Code:
    do {
    } while ( condition );
    Notice the semicolon at the end of the while ( condition ); there. You shouldn't be using the brackets like you are. You should have your if statements inside the do block after you call the DisplayMenu function. The condition would be where you test to see if the number is something other than 1 - 3.

    Are you allowed to use switch / case statements or haven't you learned about them? That might make it a bit easier.

    Also, there's another problem with your code. The following line:
    Code:
    else if (option = 3)
    isn't right. Do you see anything wrong with that line? How does it differ from the other if statements that you have?

    As for indentation, there are a few good styles out there, I prefer indenting by four spaces or so for each level. I would have something like this personally.
    Code:
    ...
        if (option == 1) {
            displayName();
        }
        else if (option == 2) {
            displayColor();
        }
        else if (option == 3) {
            displayFood();
        }
        else {
            printf("\n");
        }
    ...
    This is just an example. Generally, you want your ending bracket to line up with the beginning of the statement that it belongs to. Some people prefer another similar style, where the bracket starts on the line after the statement, kind of like the style you're using:
    Code:
    ...
        if (option == 1)
        {
            displayName();
        }
        else if (option == 2)
        {
            displayColor();
        }
        else if (option == 3)
        {
            displayFood();
        }
        else
        {
            printf("\n");
        }
    ...
    I hope this helps you a little bit. Once you fix your do / while loop, if you still have problems, let us let know.
    Last edited by Spork Schivago; 09-26-2015 at 09:43 PM. Reason: Corrected spacing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-21-2012, 11:50 PM
  2. error (do-while)loop
    By prehisto in forum C Programming
    Replies: 8
    Last Post: 10-30-2011, 09:06 AM
  3. error with my for loop?
    By rs07 in forum C Programming
    Replies: 1
    Last Post: 07-25-2008, 07:12 PM
  4. Loop seg error
    By Zishaan in forum Game Programming
    Replies: 2
    Last Post: 03-28-2007, 01:27 PM
  5. error in a for loop
    By saahmed in forum C Programming
    Replies: 11
    Last Post: 03-10-2006, 12:44 AM

Tags for this Thread