我正在开发一个 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;
有什么办法可以解决这个问题吗?
主要
#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;
}
用户.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
用户.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;
}
输入最后一个用户后按 CTRL-D。
这会关闭 cin 并退出循环。你然后得到你的输出
pal moo
a b END
p d
d f END
>>>>>>>>> ==== CTRL-D here
The Users
=========================
pal moo
p d
The Top 5 Hobby Metrics
=========================
Rank Hobby Count
=========================
1 a 1
2 b 1
3 d 1
4 f 1
查看代码后,发现代码中有错误导致程序过早退出。具体来说,代码行
if (hobby != "done")
应该是 if (firstname != "done")
,因为 firstname 是从 cin 中读取的变量,以获取朋友的名字。
这里是代码的更正版本:
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 (firstname != "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;
}