0
#include <iostream>
#include <stdlib.h>
#include "boardsetting.h"
using namespace std;

main()
{
    int x_dim = 19;
    int y_dim = 5;
    int num_zombie = 1;
    std::string answer;
    // default
    cout << "Default Game Settings" << endl;
    cout << "-----------------------" << endl;
    cout << "Board Rows     : " << y_dim << endl;
    cout << "Board Columns  : " << x_dim << endl;
    cout << "Zombie Count   : " << num_zombie << endl;

    int i = 0;
    while (i == 0)
    {
        cout << "\nDo you wish to change the game settings (y/n)? => ";
        cin >> answer;
        system("CLS");
        if (answer == "y")
        {
            Board_Setting();
            i = i + 1;
        }
        else if (answer == "n")
        {
            // Game DashBoard
            i = i + 1;
        }
        else
        {
            cout << "Invalid input. Please try again." << endl;
            i = i + 0;
        }
    }
}

// Header file board_setting.h
#include <iostream>
#include <stdlib.h>
#include <limits>

// system to accept only numeric number only
const auto Accept_Num = std::numeric_limits<std::streamsize>::max();
void Board_Setting()
{
    int x_dim = 0;
    int y_dim = 0;
    int num_zombie = 0;

    bool test_row = false;
    std::cout << "Board Settings" << std::endl;
    std::cout << "----------------" << std::endl;
    while (!test_row)
    {
        std::cout << "Enter rows => ";
        std::cin >> y_dim;
        if (!y_dim)
        {
            std::cout << "Please enter numeric_number. Please try again!" << std::endl;
            std::cin.clear();
            std::cin.ignore(Accept_Num, '\n');
            test_row = false;
            std::cout << "\n";
        }
        else if (y_dim % 2 == 0)
        {
            std::cout << "You have entered even number! Please try again!" << std::endl;
            std::cout << "\n";
        }
        else
        {
            while (y_dim % 2 != 0)
            {
                bool test_col = false;
                while (!test_col)
                {
                    std::cout << "Enter columns => ";
                    std::cin >> x_dim;
                    if (!x_dim)
                    {
                        std::cout << "Please enter numeric value. Please try again!" << std::endl;
                        std::cin.clear();
                        std::cin.ignore(Accept_Num, '\n');
                        test_col = false;
                        std::cout << "\n";
                    }
                    else
                    {
                        test_col = true;
                        bool test_zombie = false;
                        std::cout << "\nZombie Settings" << std::endl;
                        std::cout << "------------------" << std::endl;
                        // to allow user to reenter when input is not numeric number.
                        while (!test_zombie && test_col)
                        {
                            std::cout << "Enter number of zombies => ";
                            std::cin >> num_zombie;
                            if (num_zombie)
                            {
                                std::cout << "\nSetting Updated." << std::endl;
                                test_zombie = true;
                                test_col = true;
                                system("pause");
                            }
                            else
                            {
                                std::cout << "Invalid input. Please try again!" << std::endl;
                                std::cin.clear();
                                std::cin.ignore(Accept_Num, '\n');
                                test_zombie = false;
                            }
                        }
                    }
                }
            }
        }
    }
}

Expected : Board Settings

Enter rows => 3 Enter columns => 5

Zombie Settings

Enter number of zombies => 2

Setting Updated. Press any key to continue . . .

Actual : Board Settings

Enter rows => 3 Enter columns => 5

Zombie Settings

Enter number of zombies => 2

Setting Updated. Press any key to continue . . . Enter columns =>

It starts to keep looping.

  • Use a debugger. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jason Jan 30 '23 at 03:29
  • You run a loop `while (y_dim % 2 != 0)`. And you are entering 3 for `y_dim`. `3 % 2 != 0` is true, and so your loop repeats. – Igor Tandetnik Jan 30 '23 at 04:55

0 Answers0