C++;,程序在允许输入之前结束 P>练习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; } #包括“stdafx.h” #包括 int main() { 载体名称; 向量得分; 字符串n=”“;//名称 int s=0;//分数 字符串student=“”; cout n>>s) { 对于(size_t i=0;i>分数) { if(记录.查找(名称)!=结束(记录)) { 你难道从来没有清除过eof标志吗。 #include <map> #include <iostream> std::map<std::string, int> read_records() { std::map<std::string, int> records; std::string name; int score; std::cout << "Enter the name followed by the score. (Ex. John 89)" << std::endl; while(std::cin >> name >> score) { if (records.find(name) != end(records)) { std::cout << "Error: Duplicate name, Overwriting" << std::endl; } else { records.insert({name, score}); } } std::cin.clear(); return records; } int main() { auto const records = read_records(); std::cout << "Name\tScore:" << std::endl; for(auto& r : records) std::cout << r.first << "\t" << r.second << std::endl; std::cout << "Enter name of student to look up their score: " << std::flush; std::string name; if (std::cin >> name) { std::cout << "\nScore: " << records.at(name) << std::endl; } }
在按下CTRL+Z组合键(这会导致C++;,程序在允许输入之前结束 P>练习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; } #包括“stdafx.h” #包括 int main() { 载体名称; 向量得分; 字符串n=”“;//名称 int s=0;//分数 字符串student=“”; cout n>>s) { 对于(size_t i=0;i>分数) { if(记录.查找(名称)!=结束(记录)) { 你难道从来没有清除过eof标志吗。 #include <map> #include <iostream> std::map<std::string, int> read_records() { std::map<std::string, int> records; std::string name; int score; std::cout << "Enter the name followed by the score. (Ex. John 89)" << std::endl; while(std::cin >> name >> score) { if (records.find(name) != end(records)) { std::cout << "Error: Duplicate name, Overwriting" << std::endl; } else { records.insert({name, score}); } } std::cin.clear(); return records; } int main() { auto const records = read_records(); std::cout << "Name\tScore:" << std::endl; for(auto& r : records) std::cout << r.first << "\t" << r.second << std::endl; std::cout << "Enter name of student to look up their score: " << std::flush; std::string name; if (std::cin >> name) { std::cout << "\nScore: " << records.at(name) << std::endl; } },c++,C++,在按下CTRL+Z组合键(这会导致cin流处于EOF状态)后,您需要将cin输入流恢复到正常的“良好”状态,以便能够再次使用它。 在打印向量内容的for循环之后添加以下代码 cin.clear(); 您还可以使用rdstate()函数检查标准输入流的状态。除0之外的任何内容都表示标准流处于错误状态。如前所述,在读取记录失败后,您需要清除std::cin上的错误状态 std::cin.clear(); 我应该做这件事。这是我对这件事的看法 正确的数据结构,而不是两个孤立的向量 常数正确性 分
cin
流处于EOF状态)后,您需要将cin
输入流恢复到正常的“良好”状态,以便能够再次使用它。
在打印向量内容的for
循环之后添加以下代码
cin.clear();
您还可以使用
rdstate()
函数检查标准输入流的状态。除0
之外的任何内容都表示标准流处于错误状态。如前所述,在读取记录失败后,您需要清除std::cin
上的错误状态
std::cin.clear();
我应该做这件事。这是我对这件事的看法
- 正确的数据结构,而不是两个孤立的向量
- 常数正确性
- 分离功能
- 不再使用魔法索引进行hacky
调用.erase()
#包括
#包括
std::地图读取记录()
{
地图记录;
std::字符串名;
智力得分;
标准::cout name>>分数)
{
if(记录.查找(名称)!=结束(记录))
{
你难道从来没有清除过eof标志吗。
#include <map>
#include <iostream>
std::map<std::string, int> read_records()
{
std::map<std::string, int> records;
std::string name;
int score;
std::cout << "Enter the name followed by the score. (Ex. John 89)" << std::endl;
while(std::cin >> name >> score)
{
if (records.find(name) != end(records))
{
std::cout << "Error: Duplicate name, Overwriting" << std::endl;
} else
{
records.insert({name, score});
}
}
std::cin.clear();
return records;
}
int main()
{
auto const records = read_records();
std::cout << "Name\tScore:" << std::endl;
for(auto& r : records)
std::cout << r.first << "\t" << r.second << std::endl;
std::cout << "Enter name of student to look up their score: " << std::flush;
std::string name;
if (std::cin >> name)
{
std::cout << "\nScore: " << records.at(name) << std::endl;
}
}