Thread: Vector compared to a string error message std::vector<std::basic_string<char> >) (uns

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    14

    Vector compared to a string error message std::vector<std::basic_string<char> >) (uns

    Hello everyone,
    I am getting this error
    std::vector<std::basic_string<char> >) (unsigned int&)'|
    and cannot figure out what I am doing wrong. There might be more mistakes but cannot pass this error message so I couldn't test anything yet.

    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    
    
    using namespace std;
    
    
    int main()
    {
        // Create an input file
        ifstream inputFile;
        inputFile.open("WorldSeriesWinners.txt");
    
    
        // Check if the files exists or not,
        if(!inputFile){
            cout << "File couldn't be found!" << endl;
            return 0;
        }
    
    
        // Read the contents of the file into a vector
        vector<string> worldSeries;
        string input;
        while(inputFile >> input)
        {
    
    
            worldSeries.push_back(input);
        }
    
    
        // Close the file
        inputFile.close();
    
    
        // Get a team name from the user
        string userTeam;
        cout << "Enter a team name that you would like to check for: " << endl;
        getline(cin, userTeam);
        int wins = 0;   // Accumulator
        string quantity;
        // Find how many times user's team won the series for
        for(unsigned int index = 0; index < worldSeries.size(); index++)
        {
            if(worldSeries(index) == userTeam) // Error is here!!!
            {
                wins++;
            }
        }
    
    
        // Display the results
        cout << userTeam << " won the series for " << wins << (wins > 0 ? " times." : " time.");
    
    
    }
    Thanks for reading.

  2. #2
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    Cannot believe I asked this questions. Sorry for wasting your time.
    Used parentheses instead of brackets. It's getting late.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Wrong operator, i.e., worldSeries(index) should have been worldSeries[index]

    Alternatively, #include <algorithm> and write:
    Code:
    int wins = std::count(worldSeries.begin(), worldSeries.end(), userTeam);
    If you are compiling with respect to C++11 or later, then another alternative:
    Code:
    // Find how many times user's team won the series for
    for (const auto& team : worldSeries)
    {
        if (team == userTeam)
        {
            ++wins;
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    14
    Thank you for sharing.
    How did you tag your code so that it shows the line numbers?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adaptron
    How did you tag your code so that it shows the line numbers?
    I posted my code without any special markup as text in [code][/code] bbcode tags. If you copy your code such that it has some special bbcode markup, this may interfere with the forum plugin's automatic syntax highlighting and line numbering.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  2. vector<vector<const char*>> behaving strangely
    By unixmania in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2013, 02:50 PM
  3. Copying string vector to char vector
    By Ducky in forum C++ Programming
    Replies: 21
    Last Post: 07-10-2013, 11:22 AM
  4. Replies: 9
    Last Post: 04-04-2008, 12:41 PM
  5. std::vector<char> vs. std::string
    By drrngrvy in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2006, 05:06 PM

Tags for this Thread