Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:19:34+00:00 2026-06-14T17:19:34+00:00

I have a custom class ‘team’ and one of its attributes is its ‘name.’

  • 0

I have a custom class ‘team’ and one of its attributes is its ‘name.’ After each ‘team’ is created, I add it to a vector teamList.

I would like to implement a function that continuously prompts the user for a team name which is not already taken by a team within the teamList. I have the following code:

while (true) {
    string newString;
    bool flag = true;
    getline(cin, newString);
    for (int i = 0; i < teamList.size(); i++) {
        if (teamList[i].name.compare(newString) == 0) flag = false; 
    }
    if (flag == true) {
        return newString;
    } else {
        cout << "name already taken." << endl;
    }
}

However, this code is really ugly; is there a better way to check? Also, a more general question- faced with an issue of ugly code (like this one), what kinds of steps can I take to find a new, cleaner implementation? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:19:35+00:00Added an answer on June 14, 2026 at 5:19 pm

    I would use std::set, which deals with duplicates for you. As an example, you can see that the class is sorted by the string member, and when three are inserted in main, only two stay because two of the insertions have the same string, so they are treated equal.

    #include <iostream>
    #include <set>
    #include <string>
    
    struct SetThing {
        SetThing(int value, const std::string &value2) : i(value), s(value2){}
    
        int i;
        std::string s;
    
        bool operator<(const SetThing &other) const {
            return s < other.s;
        }
    };
    
    int main() {
        std::set<SetThing> s;
        s.insert(SetThing(5, "abc"));
        s.insert(SetThing(4, "def"));
        s.insert(SetThing(6, "abc"));
        std::cout << s.size();
    }
    

    Now for inserting, you can just reprompt while the second member of the returned pair is false:

    do {
        //get input
    } while (!teamList.insert(somethingBasedOnInput).second);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class I created, say MyClass. Now how to add a
I have a custom class and std::vector filled with objects of this class. And
In my iPad App, I have one custom class inherited from UIButton. I am
I have a custom class, with a property ID. We can name this class
I have my custom class User : class User { public String name; public
Background: I have a custom class, which represents a Data Base Table, each property
I have Custom UIView class in one textview. Custom View class frame set only
Setup: I have a custom class I've created that handles data retrieved from a
I have a custom class named Profile and a NSMutableArray which I add the
I have custom class Person which contains only data about person ( name, last

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.