如何对 int 和 cin 键盘输入 C++ 使用相同的字母

How to use the same letter for an int and cin keyboard input C++

您好,我想对 int 和 cin 键盘输入使用相同的字母,因此当我输入新数字时,当我使用键盘示例代码输入分数时,它会更改单元格中的数字,考虑到我我还是个初学者,还在学习中:

int h = 0; 
cout << " _______________________" << endl; 
cout << "|chelsea fc |"<< h << "|" << endl; 
cout << "|___________|__________|" << endl; 

string h = ""; 

cout << "Type here to add score to table" << endl; 
getline(cin, h); 

cout << "You added the score " << h << " to the table" << endl;

尝试编写一个函数,根据整数输出分数table。像这样:

void write_table(int h) {
    cout << " _______________________" << endl; 
    cout << "|chelsea fc |"<< h << "|" << endl; 
    cout << "|___________|__________|" << endl; 
}

在请求用户输入后调用该函数。

如果我没理解错的话,你想要这样的东西:

int score = 0; 
cout << " _______________________" << endl; 
cout << "|chelsea fc |"<< score << "|" << endl; 
cout << "|___________|__________|" << endl; 

cout << "Type here to add score to table" << endl; 
cin >> score;

cout << "You added the score " << score << " to the table" << endl;

请记住,在读取数字和字符串时,使用输入运算符 >> 会读取 并丢弃 前导白色-space,因此即使输入分数后输入缓冲区中有换行符,如果您尝试读取新数字或字符串,换行符将被忽略。