-3

I am working on a C++ program. In the main() function, it will ask the user to input the first and last names of their friends, and the hobbies they have. For example, the user input will be like this:

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

Which will then output the list of users and the top 5 interests ranked by the number of users who have those interests.

When I run my program and I put in my inputs, the program prints out no output.

no output logical error

Instead of this output:

output

I tried to use the if statement break, but it still shows the same result.

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

Is there any way to fix this issue?

main

#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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • PSA: Time to learn about [constructor lists](https://en.cppreference.com/w/cpp/language/constructor). – tadman Mar 21 '23 at 22:32
  • 4
    Also worth stepping through in a debugger to see what's going on. – tadman Mar 21 '23 at 22:33
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Mar 21 '23 at 22:41
  • 2
    [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Mar 21 '23 at 22:43
  • Please inline your actual and expected outputs. They are just text and will be more accessible that way. – Chris Mar 21 '23 at 22:44

2 Answers2

3

press CTRL-D after the last user is entered.

This closes cin and makes your loop exit. YOu then get your output

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
pm100
  • 48,078
  • 23
  • 82
  • 145
1

After reviewing the code, I noticed that there is an error in the code that causes the program to exit prematurely. Specifically, the line of code if (hobby != "done") should be if (firstname != "done"), since firstname is the variable that is being read from cin to get the name of the friend.

Here is the corrected version of the code:

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;
}
nelsonsule
  • 334
  • 2
  • 8