getline function

this is my code so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <vector>
using namespace 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++;
		else if (*i = 'N') Nvote++;
		else if (*i = 'P') Pvote++;
		else if (*i = 'A') Avote++;
		else if (*i = '#') return 0;
	}
	return vote;
};

string getVote(string vote) {
	int half = vote.length() / 2;
	if (Avote >= half) return "need quorum";
	else if (Yvote > Nvote) return "yes";
	else if (Nvote > Yvote) return "no";
	else if (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
Last edited on
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.

Last edited on
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
Last edited on
Here! Try this!

1
2
3
4
5
6
7
8
9
10
11
12
13
string holdLetters; // Initially = to ""

	char v;
	cin >> v;
	holdLetters += v;
	while (v != '#') {
		cin >> v;
		holdLetters += v;
	}
	cin.ignore(256, '\n'); // Throws out any remaining chars
	cin.clear();

	cout << holdLetters;
Topic archived. No new replies allowed.