help !!!

I have added code formatting to my program to make it easier to read. Can anyone please help. I can not figure it out!

I am unable to enter the names of the players. I want to be able to enter a first and last name, so I need to use getline. Can someone help...PLEASE?


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
//10-1a Assignment
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{	
	string playerName;	//variable to hold the name
	int playerScore;	//variable to hold the score
	int numPlayers;		//variable to hold the number of players
	int counter;		//loop variable count

	//declare an output file
	ofstream playerFile;
	playerFile.open("playerData.txt");

	//get the number players
	cout << "Enter the number of players:" << endl;
	cin >> numPlayers;

	//Get the names and scores for each player and write it to the file
	for (counter = 1; counter <= numPlayers; counter++)
	{
		//get the player name
		cout << "Enter the name of player #" << counter << ":" << endl;
		//cin >> playerName;
		getline(cin, playerName);

		//get the player score
		cout << "Enter the score of player #" << counter << ":" << endl;
		cin >> playerScore;
		//getline(cin, score);

		//write the name and score to playerData.txt
		playerFile << playerName <<  "\t"  << playerScore << endl;
	}

	//close the file
	playerFile.close();
	cout << "Data written to playerData.txt" << endl;

	system("pause");
	return 0;
}


OUTPUT:
Enter the number of players:
3
Enter the name of player #1:
Enter the score of player #1:
33
Enter the name of player #2:
Enter the score of player #2:
44
Enter the name of player #3:
Enter the score of player #3:
55
Data written to playerData.txt
Press any key to continue . . .
Last edited on
cin leaves a newline in the stream which is read when getline is called. use cin.ignore() after the cin to flush the stream.
Awesome!!!! I added cin.ignore(1000, '\n'); after the getline, and the file does not contain the name written to it.
Please help.


//10-1a Assignment
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
string playerName; //variable to hold the name
int playerScore; //variable to hold the score
int numPlayers; //variable to hold the number of players
int counter; //loop variable count

//declare an output file
ofstream playerFile;
playerFile.open("playerData.txt");

//get the number players
cout << "Enter the number of players:" << endl;
cin >> numPlayers;

//Get the names and scores for each player and write it to the file
for (counter = 1; counter <= numPlayers; counter++)
{
//get the player name
cout << "Enter the name of player #" << counter << ":" << endl;
getline(cin, playerName);
//this stops characters such as newline if the end-of-file is reached. sets the eofbit flag
cin.ignore(1000, '\n');

//get the player score
cout << "Enter the score of player #" << counter << ":" << endl;
cin >> playerScore;

//write the name and score to playerData.txt
playerFile << playerName << "\t" << playerScore << endl;
}

//close the file
playerFile.close();
cout << "Data written to playerData.txt" << endl;

system("pause");
return 0;
}


-----------------------------------------------------------

Enter the number of players:
3
Enter the name of player #1:
bob baker
Enter the score of player #1:
88
Enter the name of player #2:
woody putter
Enter the score of player #2:
67
Enter the name of player #3:
bobby pinns
Enter the score of player #3:
95
Data written to playerData.txt
Press any key to continue . . .

-----------------------------------------------------------

OUTPUT:
88
67
95
Last edited on
1
2
//this stops characters such as newline if the end-of-file is reached. sets the eofbit flag
cin.ignore(1000, '\n'); 

What you're doing with this ignore function as written, is throwing away characters from the input stream until either 1000 characters have been thrown out or the newline character '\n' has been found, or if there are less than 1000 characters in the stream and no '\n', then it will set the end of bit flag.

cin leaves a '\n' in the stream and if not cleared, getline will read the '\n' and not grab your string. So AFTER your cin, or before the getline, you need to use an ignore to clear out that '\n'. Otherwise your getline call will not grab your string from the stream.

Here's what your stream looks like.
you enter 3 and hit enter.
cin grabs the 3 and leaves '\n'.
Then you enter the player name.
getline grabs the '\n' left from the cin and leaves bob baker in the stream.
Then another cin asks for an int and you enter 88\n.
Now your stream looks like -bob baker88\n.
The cin looks for an int so it throws everything out until it finds and int, so it throws away bob baker and reads 88 and then leaves the '\n'.

So, all the input through the first players score would look like this in the buffer.
3\nbob baker88\n
You want to read the 3 with cin, ignore the newline, read bob baker with getline, read 88 with cin and then ignore the newline.
Get it?
Topic archived. No new replies allowed.