Thread: Beginner help

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    4

    Beginner help

    I am trying to have my switch case statements print with defined functions but they are not working. I have defined the statements, but it seems that the error is in the function itself.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    nstructions() {printf("\nHere are the instructions:"), printf("You may have myself or the computer give you a number.\n")};
    
    
    user_guess(){printf("The number the computer is thinking of is...10.\n")};
    
    
    computer_guess(){printf("\nThe number I'm thinking if is...5")};
    
    
    main(void)
    {   
    int menu_choice;
    
    
    printf("\tGUESSING GAME /n");
    //Menu list of choices
    
    
    printf("1. Show me the instructions\n");
    printf("2. Let the computer think of a number\n");
    printf("3. I will think of a number\n");
    printf("0. Quit\n");
    do
    {
    printf("Enter your choice: ");
    scanf(" %d", &menu_choice);
    switch (menu_choice)
    {
    case (1): instructions();
        break;
    
    
    case (2): user_guess();
        break;
    
    
    case (3): computer_guess();
        break;
    
    
    case (4): exit(1); //Exits the program.
        break;
                   //Validates input
        default: printf("\n%d is not a valid choice.\n", menu_choice);
    printf("Try again.\n");
    break;
    }
    } while ((menu_choice < 1) || (menu_choice > 4));
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    1. Make all your functions return a proper type.
    2. There's no benefit to writing code all on one line.
    3. There's no benefit in abusing the comma operator.
    4. There's no need for a trailing ; at the end of the function.
    4. Your statements inside the function DO however need ;
    Code:
    void instructions() {
        printf("\nHere are the instructions:");
        printf("You may have myself or the computer give you a number.\n");
    }
    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.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    4
    Thanks. That was clearest response from the three forums I have tried. My book did not make it clear that your function needs to be inside of curly braces.

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    424
    The 'all on one line with a ';' on the end' thing might be stylistically influenced by C++, where it is often used for small functions in class definitions.

    But it is not the done thing for plain C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Question -- From Absolute Beginner's Guide to C
    By Dghelerter in forum C Programming
    Replies: 5
    Last Post: 12-26-2013, 01:30 PM
  2. Beginner to C need help
    By spiderslayerx in forum C Programming
    Replies: 14
    Last Post: 08-31-2013, 04:30 AM
  3. Beginner help
    By flyguy10269 in forum C++ Programming
    Replies: 7
    Last Post: 08-28-2010, 03:17 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  5. C++ Beginner Needs Help!!!
    By DeanDemon in forum C++ Programming
    Replies: 2
    Last Post: 11-29-2002, 09:23 AM

Tags for this Thread