Brendan Sleanbeck Brendan Sleanbeck - 3 months ago 7
C++ Question

I am having trouble with a boolean Acceptor

#include "stdafx.h"
#include <iostream>
#include <string>

void WorldBuilder();
bool Acceptor();

int main()
{
bool IsAcceptable = (false);

while (IsAcceptable == (false))
{
WorldBuilder(); // build world
IsAcceptable = Acceptor();
}
return 0;
}

// Builds the world
void WorldBuilder()
{
std::cout << "Building World...\n";
return;
}

bool Acceptor()
{
std::cout << "Is world build acceptable? (y/n) ";
std::string qIsAcceptable = "";
std::cin >> qIsAcceptable;

if (qIsAcceptable[1] == 'y')
{
return (true);
}
else if (qIsAcceptable[1] == 'n')
{
return (false);
}

}


When I debug it goes from the elses without changing the IsAcceptable boolean and goes back to WorldBuilder() and boolean trips to true whatever the input.

I feel like there must be something I don't understand about booleans.

Answer

Change:

if (qIsAcceptable[1] == 'y')
    {
        return (true);
    }
else if (qIsAcceptable[1] == 'n')
    {
        return (false);
    }

to

if (qIsAcceptable[0] == 'y')
    {
        return (true);
    }
else if (qIsAcceptable[0] == 'n')
    {
        return (false);
    }

C/C++ array arithmetics starts from 0, not 1.