2

Can anyone tell me why my function won't print the first letter of names after the first one? This is my code.

void plusScores(ifstream& in, ostream& out)
{
    string name;
    char score;
    int plus = 0;
    int minus = 0;;
    double sum;
    double percent;

    while (getline(in, name))
    {

        while (in >> score && (score == '+' || score == '-'))
        {
            if (score == '+')
                plus++;
            else if (score == '-')
            minus++;
        }

        sum = plus + minus;
        percent = (plus / sum) * 100;

        out << fixed << showpoint << setprecision(1);

        out << name << ": " << percent << "% plus" << endl;

        plus = 0;
        minus = 0;  
    }
}

My output should look like this:

Kane, Erica: 40.0% plus
Chandler, Adam: 75.0% plus
Martin, Jake: 100.0% plus
Dillon, Amanda: 62.5% plus

Instead I get this:

Kane, Erica: 40.0% plus
handler, Adam: 75.0% plus
artin, Jake: 100.0% plus
illon, Amanda: 62.5% plus

The text file it's reading looks like this:

Kane, Erica
--+-+
Chandler, Adam
++-+
Martin, Jake
+++++++
Dillon, Amanda
++-++-+-
SimpleBeat
  • 747
  • 1
  • 10
  • 20
Kelly Leon
  • 23
  • 2

3 Answers3

4

When you get to the end of the line of "--+-+", your code has read in the first character of the name. It is no longer there for getline to read. This is because the in >> score skips whitespace characters.

You could use in >> std::noskipws >> score. However, this will probably lead to some interesting other issues in a real world application if there are spaces between the line of +- and the newline, you'll read an empty line of "name" and then use the name to calculate a zero score (unless there are + or - in the beginning of the name).

The other choice is to use getline consistently, and instead of reading a character at a time from the input file, read a line, and count + and - in the string you got.

A third choice would be to "unget" the last character from the input. After your loop, insert in.unget(score); to use this method.

All of these methods have slightly different pros and cons. In general, mixing getline and >> is not a great idea.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
3

Because while (in >> score && (score == '+' || score == '-')) reads these letters. As they are not + or - they are not used then.

Instead, I recommend using getline for the +/- line as well, and then count the number of + and - in the complete string. (See this question for how to count them.)

Community
  • 1
  • 1
wkl
  • 1,896
  • 2
  • 15
  • 26
  • [`std::count`](http://en.cppreference.com/w/cpp/algorithm/count) in conjunction with the recommended use of `std::getline` for the line of `+-` values would probably come in pretty handy. – WhozCraig Apr 12 '17 at 06:14
1

Fix your code:

void plusScores(ifstream& in, ostream& out)
{
string name;
string scores;

int plus = 0;
int minus = 0;;
double sum;
double percent;

while (getline(in, name))
{

    getline(in, scores);
    int loc = 0;
    while (loc < scores.length())
    {
        if (scores.at(loc) == '+')
            plus++;
        else if (scores.at(loc) == '-')
            minus++;
        loc++;
    }

    sum = plus + minus;
    percent = (plus / sum) * 100;

    out << fixed << showpoint << setprecision(1);

    out << name << ": " << percent << "% plus" << endl;

    plus = 0;
    minus = 0;  
}
}
user3811082
  • 218
  • 1
  • 7