-1

I'm working on a program to aid me in world-building that randomly generates a settlement (hamlet, village, town, city) based on a nation (German, Latin, Eastern) that the user chooses. Unfortunately, my code halts right at the "main()" function as it won't call the "settlementCreation()" void function I created.

I've tried moving the function I want to call above the "main()" function, or my usual method of creating the function above, defining it's contents below, but neither of these are working. I can't figure out any other solutions with my limited experience coding C++.

Main() Function:

int main() {
    char tempChoice{};
    bool isMakingSettlement = true;
    while (isMakingSettlement = true) {
        cout << "Create a settlement? (y/n): ";
        cin >> tempChoice;
        cout << "\n\n";
        if (tempChoice == 'y') {
            settlementCreation();
        } else {
            isMakingSettlement = false;
        }
    }
    return 0;
}

settlementCreation() Function:

void settlementCreation() {
    int tempType{};
    int tempNation{};
    bool isTypeValid = false;
    bool isNationValid = false;
    while (isTypeValid = false) {
        cout << "What type of settlement would you like to create?:";
        cout << "\n     1. Hamlet";
        cout << "\n     2. Village";
        cout << "\n     3. Town";
        cout << "\n     4. City\n";
        cin >> tempType;
        if (tempType >= 1 && tempType <= 4) {
            isTypeValid = true;
        } else {
            cout << " is an invalid choice, please select a valid choice.";
        }
        cout << "\n\n";
    }
    while (isNationValid = false) {
        cout << "What nation would you like your settlement to be in?: ";
        cout << "\n     1. Latin";
        cout << "\n     2. German";
        cout << "\n     3. Eastern\n";
        cin >> tempNation;
        if (tempNation >= 1 && tempNation <= 3) {
            isNationValid = true;
        } else {
            cout << " is an invalid choice, please select a valid choice.";
        }
        cout << "\n\n";
    }
    Settlement objSettlement(tempType,tempNation);
}

So the program is supposed to allow the user to choose a nation and a settlement type before redirecting to the Settlement object constructor to create the objSettlement instance of the object. The usual outcome however, is just an infinite loop of: "Create a settlement? (y/n): " With no responses I've tried closing the program or going to the "settlementCreation()" function.

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
  • 2
    How do you know that it isn't called? Did you try stepping through your code with a debugger? Note, that `while (isTypeValid = false)` loop will never be executed, since `isTypeValid = false` is an assignment - not comparison. – Algirdas Preidžius Oct 02 '19 at 14:36
  • 1
    Possible duplicate of [C++ Won't exit do while loop](https://stackoverflow.com/questions/19019381/c-wont-exit-do-while-loop) – dbc Oct 02 '19 at 15:32

1 Answers1

6
while (isMakingSettlement = true) {

This does not check if isMakingSettlement is true. It sets isMakingSettlement to true! This means the check in the while loop always sees true, so never stops going round.

Use while (isMakingSettlement == true).

(Or while (isMakingSettlement), or while (true == isMakingSettlement); all are fine, it's a stylistic choice, though the last would have helped you catch this bug!).

Similarly for all your other while loops.


Assuming you fix the above, your next problem will be here:

bool isTypeValid = false;
bool isNationValid = false;
while (isTypeValid == false) { // once corrected
    // ... never get here!
while (isNationValid == false) { // once corrected
    // ... never get here!

You always set those bools to false, so these loops are never executed.

BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • 4
    Better yet, just use `while (isMakingSettlement)`. – Fred Larson Oct 02 '19 at 14:37
  • Well, perhaps. That is a matter of style, I wanted to emphasise the correctness. Personally, I'm a big fan of Yoda conditions. – BoBTFish Oct 02 '19 at 14:37
  • I've never understood the compulsion some people have to use comparison operators to make Boolean expressions into Boolean expressions. And I don't like Yoda conditions, either. Use `-Wall -Werror` and let the compiler take care of you without mangling your expressions. – Fred Larson Oct 02 '19 at 14:39
  • I did notice this just a short time after posting this, confusing the assignment operator with the equality one. I've since corrected the mistake but I'm getting to same issue as before, the void function remains uncalled. – SamsyTheUnicorn Oct 02 '19 at 14:41
  • 2
    @SamsyTheUnicorn How do you know, that it is not called? Do you realize, that you have similar mistake in multiple places in your code? – Algirdas Preidžius Oct 02 '19 at 14:42
  • @FredLarson that's not an argument I've encountered yet, I'll add it to my list of best practices to learn, thanks! – SamsyTheUnicorn Oct 02 '19 at 14:43
  • I like Yoda conditions, and preventing this bug is just a small part of the reason. I find them very explicit and readable. I think I might be in a minority in that though. – BoBTFish Oct 02 '19 at 14:45
  • @BoBTFish "_I find them very explicit and readable. I think I might be in a minority in that though._" I too like them, to an extent. For equality/inequality, they just as readable, as non-Yoda condition, to me. I struggle reading less-than/more-than (`5 >= someNumber`) type of conditions, though. – Algirdas Preidžius Oct 02 '19 at 14:48
  • 1
    @AlgirdasPreidžius I thought I had a "cout" before the "while" so I would have some record of the function being called, but it appears i removed that in one of my edits and I've still not learned how to properly use the debug tools on my IDE. BoBTFish, just noticed those other issues with assignments, after fixing them all (making sure to use equality operators) the program runs smoothly, thanks for the help! – SamsyTheUnicorn Oct 02 '19 at 14:48