字符以数字形式打印到std::cout
Character printing out as numbers to std::cout
我不明白这里的问题。我的名单上采取的价值观的派别和数字似乎是正确的。但是,我的输出出现了问题。只给我数字,没有字母显示他们在哪个派别。
打印数字的语句位于底部,如下图所示:
cout << "Your first Card is "
<< numberBoxListHead_ptr->getNumber()
<< factionHead_ptr->getLetter()
<< endl;
这是完整的代码:
#include <iostream>
#include <conio.h>
using namespace std;
//The Class where the List depends on it.
class NumberBox
{
private:
int number;
char letter;
public:
NumberBox *next_ptr;
void setNumber(int number)
{
this->number = number;
}
void setLetter(char letter)
{
this->letter = letter;
}
int getNumber()
{
return number;
}
char getLetter()
{
return letter;
}
};
int main()
{
int number[4];
char faction[4];
NumberBox *factionHead_ptr = new NumberBox();
NumberBox *factionBody1_ptr = new NumberBox();
NumberBox *factionBody2_ptr = new NumberBox();
NumberBox *factionBody3_ptr = new NumberBox();
NumberBox *factionTail_ptr = new NumberBox();
NumberBox *numberBoxListHead_ptr = new NumberBox();
NumberBox *numberBoxListBody1_ptr = new NumberBox();
NumberBox *numberBoxListBody2_ptr = new NumberBox();
NumberBox *numberBoxListBody3_ptr = new NumberBox();
NumberBox *numberBoxListTail_ptr = new NumberBox();
cout << "Please give the number for the first Card" << endl;
cin >> number[0];
if(number[0] > 0 && number[0] < 13)
{
numberBoxListHead_ptr->setNumber(number[0]); /* <--- Setting for the first input, which is the number */
}
else
{
cout << " Invalid " << endl;
exit(1);
}
cout << "Please Give the faction for the first Card" << endl;
cin >> faction[0];
if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')
{
if(faction[0] == 's')
{
factionHead_ptr->setLetter('s');
}
else if(faction[0] == 'c')
{
factionHead_ptr->setLetter('c');
}
else if(faction[0] == 'h')
{
factionHead_ptr->setLetter('h');
}
else if(faction[0] == 'd')
{
factionHead_ptr->setLetter('d');
}
}
else
{
cout << "Invalid" << endl;
exit(1);
}
cout << "Please give the number for the Second Card" << endl;
cin >> number[1];
if(number[1] > 0 && number[1] < 13)
{
numberBoxListBody1_ptr->setNumber(number[1]); /* <--- Setting for the second input, which is the number */
}
else
{
cout << " Invalid " << endl;
exit(1);
}
cout << "Please Give the faction for the second Card" << endl;
cin >> faction[1];
if(faction[1] == 's' || faction[1] == 'c' || faction [1] == 'h' || faction [1] == 'd')
{
if(faction[1] == 's')
{
factionHead_ptr->setLetter('s');
}
else if(faction[1] == 'c')
{
factionHead_ptr->setLetter('c');
}
else if(faction[1] == 'h')
{
factionHead_ptr->setLetter('h');
}
else if(faction[1] == 'd')
{
factionHead_ptr->setLetter('d');
}
}
else
{
cout << "Invalid" << endl;
exit(1);
}
cout << "Please give the number for the Third Card" << endl;
cin >> number[2];
if(number[2] > 0 && number[2] < 13)
{
numberBoxListBody2_ptr->setNumber(number[2]); /* <--- Setting for the third input, which is the number */
}
else
{
cout << " Invalid " << endl;
exit(1);
}
cout << "Please Give the faction for the Third Card" << endl;
cin >> faction[2];
if(faction[2] == 's' || faction[2] == 'c' || faction [2] == 'h' || faction [2] == 'd')
{
if(faction[2] == 's')
{
factionHead_ptr->setLetter('s');
}
else if(faction[2] == 'c')
{
factionHead_ptr->setLetter('c');
}
else if(faction[2] == 'h')
{
factionHead_ptr->setLetter('h');
}
else if(faction[2] == 'd')
{
factionHead_ptr->setLetter('d');
}
}
else
{
cout << "Invalid" << endl;
exit(1);
}
cout << "Please give the number for the fourth Card" << endl;
cin >> number[3];
if(number[3] > 0 && number[3] < 13)
{
numberBoxListBody3_ptr->setNumber(number[3]); /* <--- Setting for the fourth input, which is the number */
}
else
{
cout << " Invalid " << endl;
exit(1);
}
cout << "Please Give the faction for the fourth Card" << endl;
cin >> faction[3];
if(faction[3] == 's' || faction[3] == 'c' || faction [3] == 'h' || faction [3] == 'd')
{
if(faction[3] == 's')
{
factionHead_ptr->setLetter('s');
}
else if(faction[3] == 'c')
{
factionHead_ptr->setLetter('c');
}
else if(faction[3] == 'h')
{
factionHead_ptr->setLetter('h');
}
else if(faction[3] == 'd')
{
factionHead_ptr->setLetter('d');
}
}
else
{
cout << "Invalid" << endl;
exit(1);
}
cout << "Please give the number for the Fifth Card" << endl;
cin >> number[4];
if(number[4] > 0 && number[4] < 13)
{
numberBoxListTail_ptr->setNumber(number[4]); /* <--- Setting for the fifth input, which is the number */
}
else
{
cout << " Invalid " << endl;
exit(1);
}
cout << "Please Give the faction for the fifth Card" << endl;
cin >> faction[4];
if(faction[4] == 's' || faction[4] == 'c' || faction [4] == 'h' || faction [4] == 'd')
{
if(faction[4] == 's')
{
factionHead_ptr->setLetter('s');
}
else if(faction[4] == 'c')
{
factionHead_ptr->setLetter('c');
}
else if(faction[4] == 'h')
{
factionHead_ptr->setLetter('h');
}
else if(faction[4] == 'd')
{
factionHead_ptr->setLetter('d');
}
}
else
{
cout << "Invalid" << endl;
exit(1);
}
//This would be the list for the value of the card.
numberBoxListHead_ptr->next_ptr = numberBoxListBody1_ptr; /* <--- From Head to Body1 */
numberBoxListBody1_ptr->next_ptr = numberBoxListBody2_ptr; /* <--- From Body1 to Body2 */
numberBoxListBody2_ptr->next_ptr = numberBoxListBody3_ptr; /* <--- From Body2 to Body3 */
numberBoxListBody3_ptr->next_ptr = numberBoxListTail_ptr; /* <--- From Body3 to BodyTail */
//This Would be the list for the faction of the card.
factionHead_ptr->next_ptr = factionBody1_ptr; /* <--- From Head to Body1 */
factionBody1_ptr->next_ptr = factionBody2_ptr; /* <--- From Body1 to Body2 */
factionBody2_ptr->next_ptr = factionBody3_ptr; /* <--- From Body2 to Body3 */
factionBody3_ptr->next_ptr = factionTail_ptr; /* <--- From Body3 to BodyTail */
int counter;
for(counter = 0; counter < 4; counter++)
{
if(counter == 0)
{
cout << "Your first Card is " << numberBoxListHead_ptr->getNumber() << factionHead_ptr->getLetter() << endl;
}
if(counter == 1)
{
cout << "Your Second Card is " << numberBoxListBody1_ptr->getNumber() << factionBody1_ptr->getLetter() << endl;
}
if(counter == 2)
{
cout << "Your third Card is " << numberBoxListBody2_ptr->getNumber() << factionBody2_ptr->getLetter() << endl;
}
if(counter == 3)
{
cout << "Your fourth Card is " << numberBoxListBody3_ptr->getNumber() << factionBody3_ptr->getLetter() << endl;
}
if(counter == 4)
{
cout << "Your fifth Card is " << numberBoxListTail_ptr->getNumber() << factionTail_ptr->getLetter() << endl;
}
}
getch();
}
int getLetter()
在这种情况下,这是错误的。letter
是一个字符,如果您想将其打印为字母,请确保返回一个字符。
char getLetter() { ... }
如果您返回一个int
,std::cout
会像打印卡片等级一样打印它的数值。如果返回char
,它将打印相应的字符,而不是其数值。
(你的代码需要认真的重构工作。制作一些函数来处理suit和rank的输入,并重用这些函数。然后找出如何循环读取四张卡片。)
相关文章:
- std::cout.imbue()多重调用
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- std::cout输出int时出现编译错误
- 为什么 std::cout 打印浮点数、双精度和长双精度到相同的小数精度?
- main() 中的 std::cout 在调试期间不会在调试控制台中打印任何内容
- std::cout 来自多个线程
- 在 c++ 中以十六进制格式打印无符号字符(BYTE).使用 std::cout
- Cuda 基本程序 (将值写入矩阵和 std:cout 不起作用) ;主功能不启动
- 是否可以为 std::cout 创建别名?
- std::cout 在打印变量与函数表达式时的行为不同
- 带有 std::cout 的多线程控制台文本动画
- 捕获/禁止发送到 std::cout 的 OpenCV 警告
- printf() 和 std::cout 在指针方面的区别
- 使用字符串流加速 std::cout 日志记录
- 是否可以在每次使用std::cout时执行一个函数
- 为什么'std::cout << !+2 '输出 0?
- 如何将自定义类传递给std::cout
- 对于输出,std::copy是否比std::cout快