ThundeReX
ThundeReX

Reputation: 35

Error on getline function no instance matches the arguments

Why my code is not executing and showing me error ?? Im getting error on this line while (getline(s, word, ' , ')) my Code is below:

#include <fstream> 
#include <string>
#include <string.h>
#include <algorithm> 
#include <iostream> 
#include <vector>

using namespace std;
// first we define a class that will represent all the candidates 
class Candidate
{
    string name;
    int votes;
public:
    Candidate()
    {
        name = "";
        int votes = 0;
    }
    Candidate(string cand_name, int vote_count)
    {
        name = cand_name; votes = vote_count;
    } string getName() { return name; } int getVotes() { return votes; } void get_details() { cout << name << ", " << votes << endl; }
    //Following member method is used to increment the vote count
    void vote_this_candidate() { votes++; }
};
int main()
{
    cout << "Welcome to Student President Voting System!!!" << endl;
    Candidate allCandidates[100];
    int totalVotes = 0;
    // File pointer fstream fin; // Open an existing file
    fstream fin;
    fin.open("candidantes.txt", ios::in); // Read the Data from the file // as String Vector 
    vector <string> row;
    string line, word, temp; int index = 0; // Following while loop will iterate for each line in the file
    while (fin >> temp) {
        row.clear(); // read an entire row and // store it in a string variable 'line' 
        getline(fin, line); // used for breaking words 
        string s(line); // read every column data of a row and // store it in a string variable, 'word'
        while (getline(s, word, ' , '))
        { // adding the splitted words to row
            row.push_back(word);
        } allCandidates[index] = Candidate(row[0], stoi(row[1])); totalVotes += stoi(row[1]); index++;
    }
    string name = ""; cout << "\nPlease enter the name of the candidante you want to vote : ";
    getline(cin, name); int cand_no = -1; string userChoice; int i = 0; //Now we find the candidante with the same inputted name
    while (i < index) {
        if (allCandidates[i].getName() == " " + name) {
            cand_no = i; cout << "Do you want to vote this candidante [y/n] : ";
            cin >> userChoice; //After finding the candidate just ask the user to vote the candidante
            if (userChoice == "y") { //to vote just call the member method that increments the vote count
                allCandidates[cand_no].vote_this_candidate(); totalVotes++; cout << endl << "You successfully voted to " << name << " Thanks for voting!!!" << endl;
            }
            else { cout << "You didn't vote!!!" << endl; } break;
        }
        i++;
    } if (cand_no == -1) {
        cout << "Candidante not found!!! Do you like to add this candidate [y/n]: ";
        cin >> userChoice; if (userChoice == "y") { allCandidates[index + 1] = Candidate(name, 1); totalVotes++; index++; }
    }
    //To show top five candidates we first sort the array with lambda 
    std::sort(allCandidates, allCandidates + 10, [](Candidate a, Candidate b) -> bool { return a.getVotes() > b.getVotes(); });
    //then we show only first five candidates 
    cout << endl << "These are top 5 candidantes so far : " << endl;
    for (int i = 0; i < 5; i++)
    {
        cout << i + 1 << ","; allCandidates[i].get_details();
    } cout << endl << "Total studnets voted: " << totalVotes;
}

Upvotes: 0

Views: 351

Answers (2)

catnip
catnip

Reputation: 25388

Problem is here:

string s(line);
while (getline(s, word, ' , '))

because getline has no overload that takes a std::string as its first parameter.

However, there is an overload that takes a stringstream, so you can do:

stringstream ss(line);
while (getline(ss, word, ' , '))

Also, ' , ' won't do what you think. Perhaps you meant ','.

Finally, int votes = 0; in your Candidate() constructor should just be votes = 0;. As it is, you are just declaring, initialising and then discarding a local variable.

Upvotes: 2

Ryan
Ryan

Reputation: 169

The problem is that the compiler is telling you that the parameters you've given don't match a definition of the function. In your case I believe the problem is that you've given it 3 characters instead of 1 in the character portion (remember, a space is also a character). Try changing ' , ' to ','

Upvotes: 0

Related Questions