0

So I'm having a lot of trouble validating input with my homework, I've tried using do while loops to test input against the four char's for validity and also switch statements, but no matter what I do I either get stuck in an infinite loop or the code breaks and outputs nothing at all and just freezes in Ubuntu for me no matter what I input. Here are the two different methods I tried, with no luck. for those curious my assignment is to remake minesweeper, and my professor broke it up into 3 parts for us. Thank you for any help in advance! (Full code https://pastebin.com/QCT74x4g )

int acceptInput()
{
    int row;
    int column;
    char menu;
    printf("____________________________MENU____________________________\n");

    printf("(F) flag a spot as a mine, (R) remove a flag, (A) assert that spot is mine free, (Q) quit the game\n");
    scanf("%c", &menu);
    getchar();
    menu = toupper(menu);
    printf("input: %c", menu);

    switch(menu)
    {
    case 'F':
    {
        printf("Input successfully entered as F!");
        break;
    }
    case 'R':
    {
        printf("Input successfully entered as R!");
        break;
    }
    case 'A':
    {
        printf("Input successfully entered as A!");

        break;
    }
    case 'Q':
    {
        printf("Input successfully entered as Q!");

        tearDown();
        break;
    }
    default:
    {
        printf("Bad input, Try again.\n");
        acceptInput();
    }
    }

    printf("input successful4");
}


//method 2
int acceptInput()
{
    int row;
    int column;
    char menu;
    printf("____________________________MENU____________________________\n");

    do
    {
        printf("(F) flag a spot as a mine, (R) remove a flag, (A) assert that spot is mine free, (Q) quit the game\n");
        scanf("%c", &menu);
        getchar();
        menu = toupper(menu);
        printf("input: %c", menu);
    }while((menu != 'F') || (menu != 'R') || (menu != 'A') || (menu != 'Q'));

    printf("input successful2");

    if(menu == 'F')
    {
        printf("input successful3");
        return 0;
    }
    else if(menu = 'R')
    {
        return 0;
    }
    else if(menu = 'A')
    {
        return 0;
    }
    else if(menu = 'Q')
    {
        tearDown();
    }
    else
    {
        return 0;
    }
    return 0;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612

0 Answers0