-1
/* Programmer: Joshua Zuber
   Program Desc: This is a game that can keep track of players scores!
   Program Name:  Score Tracker
   Clean Compile Date: 
*/

#include<iostream>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include <vector>



using namespace std;

int main()

{

    // Player Variables

    string player1
    string player2;
    string player3;
    string player4;
    string player5;


    // Variables
    char again = 'y';
    char menu1 = 'y';
    int pNumb;

    // Game Arrays
    string player1G[] = "";
    string player2G[] = "";
    string player3G[] = "";
    string player4G[] = "";
    string player5G[] = "";

    // Score Arrays
    string player1S[] = "";
    string player2S[] = "";
    string player3S[] = "";
    string player4S[] = "";
    string player5S[] = "";




    while (toupper(again) == 'Y')
    {
        cout << "Welcome to Score Tracker!" << "\n\n";


        while (toupper(menu1) == 'Y')
        {
            cout <<  "1. "  << player1 << "\n\n";
            cout <<  "2. "  << player2 << "\n\n";
            cout <<  "3. "  << player3 << "\n\n";
            cout <<  "4. "  << player4 << "\n\n";
            cout <<  "5. "  << player5 << "\n\n";
            cout << "Please select a player to add your favorite games and scores!";
            cin >> pNumb;
            system("cls");


            if (pNumb == 1)
            {
                cout << "Please enter a name for player 1!" << "\n\n";
                cin >> player1;
                cin.getline(player1);
                cout << "\n\n" << "Would you like to return to menu? Y/N " << "\n\n";
                     cin >> menu1 ; 
                     system("cls");
            }
        }
    }
} 

How do I take a users input that has spaces and return it to a string? I am trying to make a simple program that takes 5 users names and has them input their 3 favorite games but i am having trouble with just getting them to input their names for the menu.

J.Zuber
  • 19
  • 2

1 Answers1

0

The reason your behavior is happening is because of these 2 lines:

cin >> player1;
cin.getline(player1);

In the first line, cin will ignore whitespace, and in the second one, it will not. So just remove the first line in the snippet above, and it should accept a name with spaces.

  • 1
    The second line won't even compile. There is no member function `std::istream::getline` which accepts a `std::string`. You have to use the non-member function. `getline(cin, player1)` – Benjamin Lindley Mar 01 '17 at 15:55
  • @Benjamin Lindley thank you very much. However, when i try this it only gives me the last name for example. When i enter [Joshua Zuber] it returns only [Zuber]. – J.Zuber Mar 01 '17 at 18:40