为了练习 C++,我正在尝试制作一个简单的程序,该程序允许用户输入名称后跟分数,然后允许用户输入名称并获取输入名称时使用的分数。该程序运行良好,直到我输入转义字符(ctrl + z)一旦我完成输入名称,输入转义字符后程序将输出“输入学生姓名以查找分数”行但不允许用户输入名称,而是读出“按任意键退出”。我完全不知道如何解决这个问题,非常感谢任何帮助。
#include "stdafx.h"
#include <std_lib_facilities.h>
int main()
{
vector <string>names;
vector <int>scores;
string n = " "; // name
int s = 0; // score
string student = " ";
cout << "Enter the name followed by the score. (Ex. John 89)" << endl;
while(cin >> n >> s)
{
for(size_t i = 0; i < names.size(); ++i)
{
if(n == names[i])
{
cout << "Error: Duplicate name, Overwriting" << endl;
names.erase(names.begin() + i);
scores.erase(scores.begin() + i);
}
}
names.push_back(n);
scores.push_back(s);
}
cout << "Name: Score:" << endl;
for(size_t j = 0; j < names.size(); ++j)
{
cout << names[j];
cout <<" " << scores[j] << endl;
}
cout << "Enter name of student to look up their score" << endl;
cin >> student;
for(size_t g = 0; g < names.size(); ++g)
{
if(student == names[g])
{
cout << "Score: " << scores[g] << endl;
}
}
keep_window_open();
return 0;
}