Thread: Help with final project

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

    Help with final project

    I am trying to finish my final project. I am very close but stuck on the last three things I need to complete.

    1. I am trying to get a sub menu into the first menu of my switch case statement. The statements are in the subMenu function;
    however they show on the same page as the instructions menu, how can I have it show a submenu for this function in the instructions menu?


    2. I am trying to get my computer_guess function to stop looping infinitely. It loops infinitely if I enter a char I keep getting these errors. 35 [Error] 'EOF' undeclared (first use in this function)


    35 each undeclared identifier is reported only once for each function it appears in. Is there way to delcare EOF?


    3. I am trying to validate the input for the computer_guess function to only ints.


    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <ctype.h>
    
    
    void instructions() {
        printf("\nHere are the instructions:\n");
        printf("1. Pick a number between 1 and 100 and the computer will tell you if it's too high or too low.\n");
        printf("2. Pick a number between 1 and 100 and the computer will tell you \n if it's too high or too low and validate your input.\n");
    }
    
    
    void subMenu() {
        printf("Additional instructions:In game one you must guess my secret number between 1 and 100. I will tell you when you're correct");
        printf("In game two the computer will select a random number from 1 to 100 and you have to guess it");
    }
    
    
    void user_guess() {
           int secretnum=5, guess, tries = 0;
    
    
        printf("Guess My Secret Number Game\n\n");
    
    
    // Takes user input and compares it to the secret number stored in the secretnum variable
        
        do
        {
            printf("Guess My Secret Number. It's between 1 and 100 : ");
            scanf("%d", &guess);
            tries++;
    
    
            if (guess > secretnum)
            {
                printf("Too high!\n\n");
            }
            else if (guess < secretnum)
            {
                printf("Too low!\n\n");
            }
          
           else if (guess == secretnum)
            {
                printf("That is correct!");
            }
          
        }while (guess != secretnum);
    
    
        return 0;
        
    }
    
    
    void computer_guess() {
          int num, guess, tries = 0;
        srand(time(0)); /* seed random number generator */
        num = rand() % 100 + 1; /* random number between 1 and 100 */
    
    
        printf("Guess The Computer's Number Game\n\n");
    
    
    // Takes user input and compares it to the random number chosen
        
        do
        {
            printf("Enter a guess between 1 and 100 : ");
            scanf("%d", &guess);
        while(flush=getchar() != EOF && flush != '\n');//I get 2 errors
            tries++;
    
    
            if (guess > num)
            {
                printf("Too high!\n\n");
            }
            else if (guess < num)
            {
                printf("Too low!\n\n");
            }
    
    
        else if (scanf("%d" = isalpha);
    
    
        {
    
    
        printf("Please enter a valid number between 1-100.");
    
    
        }
          
        }while (guess != num);
    
    
        return 0;
        
    }
    
    
    main(void)
    {   
    int menu_choice;
    
    
    printf("\tGUESSING GAME \n");
    //Menu list of choices
    
    
    printf("1. Show Me The Instructions.\n");
    printf("2. Guess My Secret Number.\n");
    printf("3. Guess The Computer's Secret Number.\n");
    printf("0. Quit\n");
    do
    {
    printf("Enter your choice: ");
    scanf(" %d", &menu_choice);
    switch (menu_choice)
    {
    case (1): instructions();
        
    case(2): subMenu();
        break;
    
    
    case (3): user_guess();
              
        break;
    
    
    case (4): computer_guess();
        break;
    
    
    case (5): 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
    38,796
    > while(flush=getchar() != EOF && flush != '\n');
    You need to declare a variable called flush.

    And parentheses matter.
    while( (flush=getchar()) != EOF && flush != '\n');

    > else if (scanf("%d" = isalpha);
    Yeah, you should check the previous one.

    Code:
    if ( scanf("%d", &guess) == 1 ) {
      // hey, it's at least a valid integer, 
      // range check it
      // then high/low check it.
    } else {
      // it was junk.
      // flush the input, and print your message
    }
    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
    When I run the program by itself, it works but it will give my the error message only when I print two or more characters. If I enter a,b,c it sees them as too low.

    And when I add this to my full program as a function it gives me this error [Error] 'EOF' undeclared (first use in this function)

    [Note] each undeclared identifier is reported only once for each function it appears in.

    Code:
    #include <stdlib.h>#include <string.h>
    #include <time.h>
    #include <ctype.h>
    
    
    void instructions() {
        printf("\nHere are the instructions:\n");
        printf("1. Pick a number between 1 and 100 and the computer will tell you if it's too high or too low.\n");
        printf("2. Pick a number between 1 and 100 and the computer will tell you \n if it's too high or too low and validate your input.\n");
    }
    
    
    void subMenu() {
        printf("Additional instructions:In game one you must guess my secret number between 1 and 100. I will tell you when you're correct");
        printf("In game two the computer will select a random number from 1 to 100 and you have to guess it");
    }
    
    
    void user_guess() {
           int secretnum=5, guess, tries = 0;
    
    
        printf("Guess My Secret Number Game\n\n");
    
    
    // Takes user input and compares it to the secret number stored in the secretnum variable
        
        do
        {
            printf("Guess My Secret Number. It's between 1 and 100 : ");
            scanf("%d", &guess);
            tries++;
    
    
            if (guess > secretnum)
            {
                printf("Too high!\n\n");
            }
            else if (guess < secretnum)
            {
                printf("Too low!\n\n");
            }
          
           else if (guess == secretnum)
            {
                printf("That is correct!");
            }
          
        }while (guess != secretnum);
    
    
        return 0;
        
    }
    
    
    void computer_guess() {
                int num,flush, guess, tries = 0;
           
        srand(time(0)); /* seed random number generator */
        num = rand() % 100 + 1; /* random number between 1 and 100 */
    
    
        printf("Guess The Computer's Serect Number\n\n");
    
    
    // Takes user input and compares it to the random number chosen
        
        do
        {
            printf("Enter a guess between 1 and 100 : ");
            scanf("%d", &guess);
            while( (flush=getchar()) != EOF && flush != '\n');
    
    
            tries++;
            
            if (guess > num)
            {
                printf("Too high!\n\n");
            }
             if (guess < num)
            {
                printf("Too low!\n\n");
            }
        
        if ( scanf("%d", &guess) == 1 ) {
    
    
        } else {
        printf("That is not a valid number\n");
        }
        
        }while (guess != num);
    
    
        return 0;
    }
    
    
    main(void)
    {   
    int menu_choice;
    
    
    printf("\tGUESSING GAME \n");
    //Menu list of choices
    
    
    printf("1. Show Me The Instructions.\n");
    printf("2. Guess My Secret Number.\n");
    printf("3. Guess The Computer's Secret Number.\n");
    printf("0. Quit\n");
    do
    {
    printf("Enter your choice: ");
    scanf(" %d", &menu_choice);
    switch (menu_choice)
    {
    case (1): instructions();
        
    case(2): subMenu();
        break;
    
    
    case (3): user_guess();
              
        break;
    
    
    case (4): computer_guess();
        break;
    
    
    case (5): 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;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,796
    EOF is in stdio.h
    Which you should be including for printf/scanf.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ideas for a Final Project ?
    By zhoomz in forum C Programming
    Replies: 3
    Last Post: 04-22-2016, 12:00 AM
  2. Final project
    By albireo in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2014, 07:07 PM
  3. Replies: 4
    Last Post: 10-19-2013, 11:01 PM
  4. Final year project
    By afisher in forum C# Programming
    Replies: 3
    Last Post: 07-04-2005, 08:15 AM
  5. Final year project
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 10-10-2003, 07:04 AM

Tags for this Thread