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

时间:2015-07-16 10:47:28

标签: c++ console-application

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

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;

2 个答案:

答案 0 :(得分:0)

尝试编写一个基于整数输出得分表的函数。像这样:

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

请求用户输入后调用该功能。

答案 1 :(得分:0)

如果我理解正确,你需要这样的东西:

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;

请记住,在读取数字和字符串时,使用输入运算符>>读取并丢弃输入中的前导空格,因此即使输入缓冲区中有换行符分数的输入,如果您尝试读取新数字或字符串,则只会忽略换行。