c++ - 如何打印字符串而不是数字
问题描述
我有一个叫做真值表的程序。如何打印“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;
}
}
}
我想这样打印
问 | ~问
T | F
F | 吨
这是实际的输出
问 | ~问
1 | 0
0 | 1
解决方案
您可以使用std::boolalpha
打印“真”和“假”。
std::cout << std::boolalpha << true;
或者根据 bool 值选择一个字符串
bool x = true;
std::cout << (x ? "T" : "F");
推荐阅读
- python - 文字转语音无法正常工作
- sql-server - SQL Server:视图中值的特殊计算
- java - Spring Boot 中的 UnsatisfiedDependencyException
- php - Opencart 管理员中的 408 请求超时错误
- html - IE11 中的布局中断,但在 Chrome 中工作正常
- eclipse - 如何从 Xtext 中定义的 DSL 到 Sirius?
- python - 使用切片就地反转列表
- python - 通过数据集中的层次结构递归
- vba - 如何在word文档的每个部分的上方和下方添加文本?
- java - 我的 RecyclerView 因 NullPointerException 而崩溃