Reputation: 23
I have a source code file with getline function in it and I receive and error when I compile it (code and error below). My problem is that I copied and pasted the whole function from an already compiled and working program (included below as well). I also have the getline function in 2 other source code files in the program, both of which compile fine. I am fairly new to programming and just started c++ programing (much better at Java) so try to keep answers simple. I looked through some of the already posted questions and answers here (and google) but all the ones I found the answers say that the parameters for the function are not correct. But I know they are correct here because it works in the other program just fine. Also as you can see in the working file the only #included is iostream and it works, that's with g++ complier in Code::Blocks. I made sure to include all the variables/constants needed as well.
Here is the code for the function and file I am getting the error from. The parts I am refering to are marked by 3 * (sorry best I could do). The error is reprinted at the bottom of post as well.
#include <iostream>
#include <istream>
#include "candidates.h"
using namespace std;
int nCandidatesInPrimary;
int delegatesForThisState;
const int maxCandidates = 10;
int delegatesWon[maxCandidates];
int totalVotes;
int totalDelegates = 0;
int votesForCandidate[maxCandidates];
string candidate[maxCandidates];
int nCandidates;
string candidateNames;
void readCandidates ()
{
cin >> nCandidates;
string line;
getline (cin, line);
for (int i = 0; i < nCandidates; ++i)
{
***getline (cin, candidateNames[i]);***
delegatesWon[i] = 0;
}
}
int findCandidate (std::string name)
{
int result = nCandidates;
for (int i = 0; i < nCandidates && result == nCandidates; ++i)
if (candidateNames[i] == name)
result = i;
return result;
}
void printCandidateReport (int candidateNum)
{
int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
if (delegatesWon[candidateNum] >= requiredToWin)
cout << "* ";
else
cout << " ";
cout << delegatesWon[candidateNum] << " " << candidateNames[candidateNum] << endl;
}
void assignDelegatesToCandidates ()
{
int remainingDelegates = delegatesForThisState;
for (int i = 0; i < nCandidatesInPrimary; ++i)
{
int candidateNum = findCandidate(candidate[i]);
int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes-1)) / totalVotes;
if (nDel > remainingDelegates)
nDel = remainingDelegates;
delegatesWon[candidateNum] += nDel;
remainingDelegates -= nDel;
}
}
here is the code from the already working program.
#include <iostream>
using namespace std;
// Max # of candidates permitted by this program
const int maxCandidates = 10;
// Names of the candidates participating in this state's primary
string candidate[maxCandidates];
// Names of all candidates participating in the national election
std::string candidateNames[maxCandidates];
// How many delegates are assigned to the state being processed
int delegatesForThisState;
// How many delgates have been won by each candidate
int delegatesWon[maxCandidates];
// How many candidates in the national election?
int nCandidates;
// How many candidates in the primary for the state being processed
int nCandidatesInPrimary;
// How many states participate in the election
int nStates;
// How many delegates in the election (over all states)
int totalDelegates = 0;
// How many votes were cast in the primary for this state
int totalVotes;
// How many votes wone by each candiate in this state's primary
int votesForCandidate[maxCandidates];
int findCandidate (std::string name);
/**
* For the most recently read primary, determine how many delegates have
* been won by each candidate.
*/
void assignDelegatesToCandidates ()
{
int remainingDelegates = delegatesForThisState;
for (int i = 0; i < nCandidatesInPrimary; ++i)
{
int candidateNum = findCandidate(candidate[i]);
int nDel = (delegatesForThisState * votesForCandidate[i] + (totalVotes-1)) / totalVotes;
if (nDel > remainingDelegates)
nDel = remainingDelegates;
delegatesWon[candidateNum] += nDel;
remainingDelegates -= nDel;
}
}
/**
* Find the candidate with the indicated name. Returns the array index
* for the candidate if found, nCandidates if it cannot be found.
*/
int findCandidate (std::string name)
{
int result = nCandidates;
for (int i = 0; i < nCandidates && result == nCandidates; ++i)
if (candidateNames[i] == name)
result = i;
return result;
}
/**
* Print the report line for the indicated candidate
*/
void printCandidateReport (int candidateNum)
{
int requiredToWin = (2 * totalDelegates + 2) / 3; // Note: the +2 rounds up
if (delegatesWon[candidateNum] >= requiredToWin)
cout << "* ";
else
cout << " ";
cout << delegatesWon[candidateNum] << " " << candidateNames[candidateNum] << endl;
}
/**
* read the list of candidate names, initializing their delegate counts to 0.
*/
void readCandidates ()
{
cin >> nCandidates;
string line;
getline (cin, line);
for (int i = 0; i < nCandidates; ++i)
{
***getline (cin, candidateNames[i]);***//already working
delegatesWon[i] = 0;
}
}
/**
* read the info on one state's primaries
*/
void readState ()
{
totalVotes = 0;
cin >> nCandidatesInPrimary >> delegatesForThisState;
totalDelegates += delegatesForThisState; // "x += y" is a shorthand for "x = x + y"
string word, line;
getline (cin, line);
for (int i = 0; i < nCandidatesInPrimary; ++i)
{
cin >> votesForCandidate[i];
totalVotes = totalVotes + votesForCandidate[i];
cin >> word;
getline (cin, line);
candidate[i] = word + line;
}
}
/**
* Generate the report on the national primary election.
*/
int main(int argc, char** argv)
{
readCandidates();
int nStates;
cin >> nStates;
for (int i = 0; i < nStates; ++i)
{
readState();
assignDelegatesToCandidates();
}
for (int i = 0; i < nCandidates; ++i)
{
printCandidateReport(i);
}
return 0;
}
error: no matching function for call to 'getline(std::istream&, char&)'|
Upvotes: 0
Views: 1525
Reputation: 23
Sorry I might have been unfair when posting this question. I was able to find the problem, it was in my header file, I messed up the variables there. for candidateNames[] I forgot the [] at the end in the header file. And since I didn't include that file in my post you couldn't see that. Thanks for the help though.
Upvotes: 0
Reputation: 283624
Looks like you forgot to
#include <string>
It's likely that the other headers have pulled in the string class itself, but not all the supporting functions such as getline
overloads that accept std::string
.
Upvotes: 0
Reputation: 96233
candidateNames
is a single string, not a collection of strings. What about setting up a vector of them?
std::vector<string> candidateNames;
And then before your for
loop:
candidateNames.resize(nCandidates);
Upvotes: 0
Reputation: 14705
Looks like you intended candidateNames to be declared as a vector<string> candidateNames;
vector<string> candidateNames;
void readCandidates ()
{
cin >> nCandidates;
string line;
getline (cin, line);
candidateNames.resize(nCandidates);
for (int i = 0; i < nCandidates; ++i)
{
getline (cin, candidateNames[i]);
}
}
Upvotes: 1