#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
int Yvote = 0;
int Nvote = 0;
int Pvote = 0;
int Avote = 0;
string getline(istream& ins, string& vote) {
string temp;
char v;
ins.get(v);
while (v != '\n' && !ins.eof()) {
temp += v;
ins.get(v);
if (v = '#') break;
}
vote = temp;
for (auto i = vote.begin(); i != vote.end(); ++i) {
if (*i = 'Y') Yvote++;
elseif (*i = 'N') Nvote++;
elseif (*i = 'P') Pvote++;
elseif (*i = 'A') Avote++;
elseif (*i = '#') return 0;
}
return vote;
};
string getVote(string vote) {
int half = vote.length() / 2;
if (Avote >= half) return"need quorum";
elseif (Yvote > Nvote) return"yes";
elseif (Nvote > Yvote) return"no";
elseif (Yvote = Nvote) return"tie";
};
int main() {
string vote;
string result;
vote = getline(cin, vote);
result = getVote(vote);
cout << result;
return 0;
}
I want my program to read several lines of votes, which could be A,P,Y or N, until # is reached. Each line corresponds to one final result.
What I have so far is I enter one line of votes and I always get "need quorum" as an output regardless of the input, and program ends.
Can anyone help me with the code, I don't know what I'm doing wrong. Thank you
Hi Mazaret. You're doing pretty good. Not bad that you wrote this on your own.
However, there are a couple of pretty big errors I can see in your code. I'll start with why your code is always displaying need quorum.
On line 19 you write if (v = '#') break;
But what you mean to type is if (v == '#') break;
"==" and "=" are two fundamentally different commands to the computer. "==" is used to compare values. "=" is used to set values.
Second, on lines 15 and 18, you rely on ins.get(v);
I wouldn't do that, I would either do this,
1 2 3
ins.ignore(256, '\n');
ins.clear();
ins.get(v);
or this ins >> v;
Both are logically equivalent.
See if you can figure out the rest. Keep in mind that '=' and '==' are totally different.
Fixed the issue thank you.
However that's still not what I want. I'm trying to make it so that for each line it reads, it prints an output until "#" is reached to indicate end of program. Right now it is reading several lines until "#" is entered, and outputs just one output. ( for all the lines that is )
Let me know if you have any idea on how to fix it and thank you :) I'm kind of stuck