1

This program works EXCEPT that it does not allow a space between the first and last name. Below is an example as to what I am talking about:

Link to Picture

Can someone please help me to fix this? I believe it is in string playerName as it will not accept a space between the first and last name.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

// Structure to hold the Player Data
struct Player {
    string playerName;
    int playerNumber;
    int pointsScored;

};

// Function Prototypes
void getPlayerInfo(Player &);
void showInfo(Player[], int);
int getTotalPoints(Player[], int);
void showHighest(Player[], int);


int main(int argc, char *argv[])
{
    const int N = 12;
    Player players[N];
    for (int i = 0; i<N; i++) {
        cout << "\nPLAYER #" << i + 1 << "\n";
        cout << "---------\n";
        getPlayerInfo(players[i]);
}

    showInfo(players, N);
    int totalPoints = getTotalPoints(players, N);
    cout << "TOTAL POINTS: " << totalPoints << "\n";
    cout << "The player who scored the most points is :";
    showHighest(players, N);
    cout << "\n";
    system("pause");

    return 0;
}


void getPlayerInfo(Player &P) {
    cout << "Player Name:";
    //cin >> P.playerName;                            **CHANGED THIS**
      cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
    std::getline(std::cin, P.playerName);                **TO THIS**
    do {
        cout << "Player Number:";
        cin >> P.playerNumber;
        if (P.playerNumber<0)
            cout << "invalid Input\n";
    } while (P.playerNumber<0);
    do {
        cout << "Points Scored:";
        cin >> P.pointsScored;
        if (P.pointsScored<0)
            cout << "invalid Input\n";
    } while (P.pointsScored<0);

}


void showInfo(Player P[], int N) {
    cout << "\nNAME" << "\t\tNUMBER" << "\t\tPOINTS SCORED" << "\n";
    for (int i = 0; i<N; i++)
        cout << P[i].playerName << "\t\t" << P[i].playerNumber << "\t\t" << P[i].pointsScored << "\n";
}

int getTotalPoints(Player P[], int N) {
    int Points = 0;
    for (int i = 0; i<N; i++)
        Points += (P[i].pointsScored);
    return Points;
}


void showHighest(Player P[], int N) {
    int HighestPoint = P[0].pointsScored;
    string Name = P[0].playerName;
    for (int i = 1; i<N; i++) {
        if (HighestPoint<P[i].pointsScored) {
            HighestPoint = P[i].pointsScored;
            Name = P[i].playerName;
        }
    }
    cout << Name;
}

2 Answers2

3

When std::cin uses operator>> to insert into a std::string, it stops reading at space (' ') characters. Use std::getline instead.

std::getline(std::cin, P.playerName); //read everything up to '\n'
Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36
0

The problem is in this code:

void getPlayerInfo(Player &P) {
cout << "Player Name:";
cin >> P.playerName;//<<----

cin treats ' ' (space) as a delimiter. If you want to have an input with ' ' (space) you need to use: (thanks to @James Root)

//before doing get line make sure input buffer is empty

see also: https://stackoverflow.com/a/10553849/3013996

cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n'); 
std::getline(std::cin,P.playerName);
Community
  • 1
  • 1
789
  • 718
  • 1
  • 10
  • 30
  • `cin::getline` is for arrays. `std::getline` is what you wanted to be using. – Weak to Enuma Elish Nov 22 '15 at 22:41
  • I edited the original but and it does work, EXCEPT now after you enter the first Player's Name, Player's Number, Player's Score. Then it goes to the second Player it skips the Player Name and goes directly to the Player Number. Any suggestions? – More Outdoors Nov 22 '15 at 22:54
  • Ok so I got it to work for the first part EXCEPT now in function: void showInfo it shows only the last name. I am sure it is the part: cout << P[i].playerName – More Outdoors Nov 23 '15 at 00:19