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

How to print string instead of numbers

本文关键字:数字 字符串 何打印 打印      更新时间:2023-10-16

我有一个名为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

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

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

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

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