如何打印字符串而不是数字

时间:2019-09-26 10:48:13

标签: c++

我有一个叫做Truth Table的程序。如何打印“ T”和“ F”而不是1和0? 这是一些代码

string Choice, UserInput, NotChoice;
bool FirstChoice[2] = { true, false };
bool ValidInput = false;

bool InvertChoice = false;

cout<<"Enter a Hypothesis: ";
cin>>Choice;
do
{
    ValidInput = false;
    cout<<"Do you want to NOT "<<Choice<<"?(Y/N): ";
    cin>>UserInput;
    toUpper(UserInput);
    if (UserInput == "Y") 
    {   
        InvertChoice = true; ValidInput = true;
    }
    else if (UserInput == "N")
    {
        InvertChoice = false; ValidInput = true;
    }
    else
    {
        cout<<"ERROR: Please enter valid values [Y, N]" << endl;
    }
}
while (!ValidInput);

NotChoice = "~" + Choice;

cout<< Choice << (InvertChoice? " | ":"") <<(InvertChoice? NotChoice : "" )<<endl;

for ( int x = 0; x < 2; x++)
{
    bool FirstValue = InvertChoice ? !FirstChoice[x] : FirstChoice[x];
    for ( int z = 0; z < 1; z++ )
    {
        if ( InvertChoice == true )
        {
            cout<< setw(1) << FirstChoice[x] << " | " << FirstValue << endl;
        }
        else
        {
            cout<< setw(1) << FirstChoice[x] << endl;
        }
    }
}

我要这样打印

Q | 〜Q

T | F

F | T

这是实际输出

Q | 〜Q

1 | 0

0 | 1

1 个答案:

答案 0 :(得分:2)

您可以使用std::boolalpha打印“ true”和“ false”。

std::cout << std::boolalpha << true;

或根据布尔值选择一个字符串

bool x = true;
std::cout << (x ? "T" : "F");