Trouble with inputting to a file

I have a basic system of entering names to be randomly placed against each other. But i am having a problem with the first name. It is entering a blank for the first person and just moving onto the next

In the file it goes
1
2
3
1 
2 Name
3 name

and so on

And when it asks you to enter the name it askes like
1
2
Enter 1st players name : Enter 2nd players name : (Name)
Enter 3rd players name : (Name)

Insted of
1
2
Enter 1st players name : (Name)
Enter 2nd players name : (Name)


Here is the code
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
#include <iostream>
#include <fstream>
#include <ostream>

using namespace std;
int player_comp();
ofstream namesFile("names.txt");

int main(){
	
	int playerAmount = 0;
	cout << "Enter the amount of players : ";
	cin >> playerAmount;
		for(int i=1;i<=playerAmount;i++){
			string playerName = "temp";
			string iSubscript = "th";
				if((i%10)==1){
					iSubscript = "st";
				}if((i%10)==2){
					iSubscript = "nd";
				}if((i%10)==3){
					iSubscript = "rd";
				}if(i>10 && i<15){
					iSubscript = "th";
				}
			cout << "Enter the " << i << iSubscript << " players name : ";
			getline(cin,playerName);
			namesFile << i << " " << playerName << endl;
		}
	namesFile.close();
}

Any help would be appreciated.
Without testing, I'm guessing the error is at line 14

 
for(int i=1;i<=playerAmount;i++){


should be:

 
for(int i=0;i<=playerAmount;i++){


You're skipping the 0
I don't believe it is that. That is just to make the correct output of number easier.
So insted of

cout << "Enter the " << (i+1) << iSubscript << " players name : ";

i can do

cout << "Enter the " << i << iSubscript << " players name : ";

Also i tested it and it did not work
I have a similar problem in my program - I just played around, and when I placed an additional getline right before my loop, as well as where it originally was - The problem was eliminated.

 
getline (cin, input);


For example:
In my new files it started with a empty line then the first line i entered, etc... After this change, my first line was not a empty line, but the first line i entered.

New Code that works without skipping 1:
1
2
3
4
5
6
7
8
9
10
		getline (cin, input);
			while (true) {
				getline (cin, input);
					if (input == "quit++")
						break;
				File << input << "\n";
			}
		File.close();
		cout << " " << endl;
		cout << "Your file has been saved." << endl;


Old code that skipped 1:
1
2
3
4
5
6
7
8
9
			while (true) {
				getline (cin, input);
					if (input == "quit++")
						break;
				File << input << "\n";
			}
		File.close();
		cout << " " << endl;
		cout << "Your file has been saved." << endl;

Last edited on
OK - Even better, i eliminated that last addition from my program, and added cin.ignore(); before my loop. This did the trick!

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
int player_comp();
ofstream namesFile("names.txt");
int main(){
	
	int playerAmount = 0;
	cout << "Enter the amount of players : ";
	cin >> playerAmount;
        cin.ignore();
		for(int i=1;i<=playerAmount;i++){
			string playerName = "temp";
			string iSubscript = "th";
				if((i%10)==1){
					iSubscript = "st";
				}if((i%10)==2){
					iSubscript = "nd";
				}if((i%10)==3){
					iSubscript = "rd";
				}if(i>10 && i<15){
					iSubscript = "th";
				}
			cout << "Enter the " << i << iSubscript << " players name : ";
			getline(cin,playerName);
			namesFile << i << " " << playerName << endl;
		}
	namesFile.close();
}
Last edited on
Thanks it worked great. This took me like 2 day to try and find out on my own before i asked on here. I used the second option because it just looks better :)

Thank you sir
Topic archived. No new replies allowed.