You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++程序输入后无输出问题排查及修复请求

问题描述

我正在开发一款C++程序,程序在main()函数中要求用户输入朋友的姓名和爱好,输入示例如下:

Erik,French,soccer,END
Jenna,Obrien,soccer,games,END
Ricardo,Waters,games,baseball,END

程序需要输出用户列表,以及按拥有该爱好的用户数量排名的前5个热门爱好。但运行程序并输入内容后,无任何输出。我尝试用以下if语句配合break处理,但问题依旧:

if (hobby != "done"){
    users.push_back(user);
}else break;

以下是完整代码,请问该如何修复?

main.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "User.h"

using namespace std;

int main()
{
    vector<User> users;
    vector<string> allHobbies;
    string firstname, lastname, hobby, done;
    
    while (cin >> firstname >> lastname) {
        string friendsname = firstname + " " + lastname;
        User user(friendsname);
        
        while (cin >> hobby && hobby != "END") {
            user.AddHobby(hobby);
            allHobbies.push_back(hobby);
        }
        if (hobby != "done"){
            users.push_back(user);
        }else break;
    }
    
    // Print users
    cout << "The Users" << endl;
    cout << "=========================" << endl;
    for (const auto& user : users) {
        cout << user.GetName() << endl;
    }
    
    // Count hobby occurrences
    sort(allHobbies.begin(), allHobbies.end());
    auto last = unique(allHobbies.begin(), allHobbies.end());
    vector<pair<string, int>> hobbyCounts;
    for (auto it = allHobbies.begin(); it != last; ++it) {
        hobbyCounts.push_back(make_pair(*it, count(allHobbies.begin(), allHobbies.end(), *it)));
    }
    
    // Sort by count and print top 5
    sort(hobbyCounts.begin(), hobbyCounts.end(), [](const auto& p1, const auto& p2) {
        return p1.second > p2.second;
    });
    
    cout << "The Top 5 Hobby Metrics" << endl;
    cout << "=========================" << endl;
    cout << "Rank\tHobby\tCount" << endl;
    cout << "=========================" << endl;
    int i = 1;
    for (const auto& p : hobbyCounts) {
        if (i > 5) break;
        cout << i++ << "\t" << p.first << "\t" << p.second << endl;
    }
    
    return 0;
}

User.h

#ifndef USER_H
#define USER_H

#include <string>
#include <vector>

class User {
public:
    User(const std::string& name);
    void AddHobby(const std::string& hobby);
    const std::vector<std::string>& GetHobbies() const;
    std::string GetName() const;
    
private:
    std::string friendsname;
    std::vector<std::string> hobbies_;
};

#endif

User.cpp

#include "User.h"

User::User(const std::string& name) {
    this->friendsname = name;
}

void User::AddHobby(const std::string& hobby) {
    this->hobbies_.push_back(hobby);
}

const std::vector<std::string>& User::GetHobbies() const {
    return hobbies_;
}

std::string User::GetName() const {
    return friendsname;
}

问题原因与修复方案

核心问题

程序无输出的根本原因是输入处理逻辑错误:

  • 原代码使用cin >> firstname >> lastname读取输入,cin默认以空格作为分隔符,但输入是用逗号分隔字段的。这会导致第一个cin >> firstname直接读取整行内容(比如Erik,French,soccer,END),而lastname无法读取有效内容,外层while循环直接终止,users向量始终为空,后续自然没有输出。
  • 代码中判断hobby != "done"完全无效,因为输入终止标记是END,从未出现done字符串。

修复步骤

  1. 按行读取输入:改用getline(cin, line)读取每一行用户信息,再将每行按逗号分割成多个字段。
  2. 正确解析字段:每行的前两个字段是名和姓,后续字段是爱好,直到遇到END标记。
  3. 移除无效的"done"判断逻辑:处理完每个用户后直接将其加入users向量,无需额外判断。

修改后的完整代码

main.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include "User.h"

using namespace std;

// 辅助函数:将字符串按指定分隔符分割成向量
vector<string> split(const string& s, char delimiter) {
    vector<string> tokens;
    string token;
    istringstream tokenStream(s);
    while (getline(tokenStream, token, delimiter)) {
        if (!token.empty()) { // 跳过空字段
            tokens.push_back(token);
        }
    }
    return tokens;
}

int main()
{
    vector<User> users;
    vector<string> allHobbies;
    string line;
    
    // 按行读取输入
    while (getline(cin, line)) {
        vector<string> fields = split(line, ',');
        if (fields.size() < 3) { // 至少需要名、姓、END/爱好
            continue;
        }
        
        // 构造用户姓名:前两个字段是名和姓
        string friendsname = fields[0] + " " + fields[1];
        User user(friendsname);
        
        // 遍历后续字段,收集爱好直到END
        for (size_t i = 2; i < fields.size(); ++i) {
            if (fields[i] == "END") {
                break;
            }
            user.AddHobby(fields[i]);
            allHobbies.push_back(fields[i]);
        }
        
        users.push_back(user);
    }
    
    // Print users
    cout << "The Users" << endl;
    cout << "=========================" << endl;
    for (const auto& user : users) {
        cout << user.GetName() << endl;
    }
    
    // Count hobby occurrences
    sort(allHobbies.begin(), allHobbies.end());
    auto last = unique(allHobbies.begin(), allHobbies.end());
    vector<pair<string, int>> hobbyCounts;
    for (auto it = allHobbies.begin(); it != last; ++it) {
        hobbyCounts.push_back(make_pair(*it, count(allHobbies.begin(), allHobbies.end(), *it)));
    }
    
    // Sort by count and print top 5
    sort(hobbyCounts.begin(), hobbyCounts.end(), [](const auto& p1, const auto& p2) {
        return p1.second > p2.second;
    });
    
    cout << "The Top 5 Hobby Metrics" << endl;
    cout << "=========================" << endl;
    cout << "Rank\tHobby\tCount" << endl;
    cout << "=========================" << endl;
    int i = 1;
    for (const auto& p : hobbyCounts) {
        if (i > 5) break;
        cout << i++ << "\t" << p.first << "\t" << p.second << endl;
    }
    
    return 0;
}

User.h(无需修改)

#ifndef USER_H
#define USER_H

#include <string>
#include <vector>

class User {
public:
    User(const std::string& name);
    void AddHobby(const std::string& hobby);
    const std::vector<std::string>& GetHobbies() const;
    std::string GetName() const;
    
private:
    std::string friendsname;
    std::vector<std::string> hobbies_;
};

#endif

User.cpp(无需修改)

#include "User.h"

User::User(const std::string& name) {
    this->friendsname = name;
}

void User::AddHobby(const std::string& hobby) {
    this->hobbies_.push_back(hobby);
}

const std::vector<std::string>& User::GetHobbies() const {
    return hobbies_;
}

std::string User::GetName() const {
    return friendsname;
}

说明

  • 新增split辅助函数,将每行输入按逗号分割成字段向量,适配输入格式。
  • 改用getline按行读取输入,解决了空格分隔导致的解析错误。
  • 移除无效的done判断逻辑,确保每个用户都能被正确加入列表。
  • 现在输入示例中的内容会被正确解析,程序将输出用户列表和前5热门爱好。

内容的提问来源于stack exchange,提问作者guest 3456

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 05:29:58