简体   繁体   English

C ++,程序在允许输入之前结束

[英]C++, Program ends before allowing input

To practice C++ I am trying to make a simple program that allows a user to input a name followed by a score and then allows the user to enter a name and get the score that name was entered with. 为了练习C ++,我试图创建一个简单的程序,允许用户输入一个名称后跟一个分数,然后允许用户输入一个名称并获得该名称输入的分数。 The program works fine until I enter an escape character (ctrl + z) once I'm done entering names, after entering the escape character the program will output the line "Enter name of student to look up the score" but not allow the user to input the name and instead reads out "Press any key to exit". 程序工作正常,直到我输入转义字符(ctrl + z),输入转义字符后,程序将输出“输入学生姓名以查找分数”行但不允许用户输入名称,然后读出“按任意键退出”。 I'm totally stumped on how to fix this and any help is greatly appreciated. 我完全不知道如何解决这个问题,非常感谢任何帮助。

#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;
}

After you press the CTRL+Z key combination, which induces an EOF state to the cin stream, you need to bring the cin input stream back to its normal 'good' state to be able to use it again. 在按下CTRL + Z组合键后,它会向cin流引入EOF状态,您需要将cin输入流恢复到正常的“良好”状态,以便能够再次使用它。 Add the following code after your for loop where you print the contents of the vectors. for循环之后添加以下代码,您for在其中打印向量的内容。

cin.clear();

You may also check the state of the standard input stream using the rdstate() function. 您还可以使用rdstate()函数检查标准输入流的状态。 Anything other than 0 means that the standard stream is in an error state. 0以外的任何0表示标准流处于错误状态。

As has been said, you need to clear the error state on std::cin after reading the records failed. 如前所述,您需要在读取记录失败后清除std::cin上的错误状态。

std::cin.clear();

should do the trick. 应该做的伎俩。 Here's my take on this using 这是我对此的看法

  • proper data structures instead of two isolated vectors 适当的数据结构而不是两个孤立的向量
  • const correctness const正确性
  • separating functions 分离功能
  • no more hacky .erase() calls with magic indexes 没有更多hacky .erase()调用魔法索引
#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;
    }
}

If you required contiguous storage, use a flat_map like the one from boost. 如果您需要连续存储,请使用类似boost中的flat_map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM