Output leaderboard through txt file

Hello I am trying to output a leaderboard from my text file and it seems to not be working but spewing out random letters any ideas?

The text file "Leaderboard.txt"

1 John Doe 23567
2 Larry Bird 21889
3 Michael Jordan 21889
4 James Bond 13890
5 Gary Smith 10987
6 Gj 9989
7 Vivien Vien 8890
8 Samantha Carl 6778
9 Gary Lewes 5667
10 Sybil Saban 4677
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  #include <fstream>  
#include <iostream>  
#include <string>
using namespace std;

int main() 
{

	ifstream infile("Leaderboard.txt");
	char textFileLetters[20]; //array to store the names
	
	for (int x = 0; x < 18; x++)
	{
		infile.seekg(0);
		infile.get(textFileLetters, sizeof(textFileLetters)-1);

		cout << textFileLetters[x];
	}
	
	cout << endl;
	system("pause");
	return 0;
}
Hello Doe,

Line 10: You have included the header file "string". This would work better as a string than a C-style character array. As a character array of 20 it is short for one of the entries in the text file.

Line 12: the for loop runs to < 18, but you only have 10 entries in the file.

Line 14: will reset the file pointer to the beginning of the file each time through the loop and will only read the first line of the file each time through the loop. Line 14 is not needed anywhere as opening the file sets the file pointer to the beginning.

Line 15: works for one read and then fails thus skipping the rest of the file. i am not sure why if fails after just one read, Still working on that.

Line 17: is where you are print the random characters. cout << textFileLetters[x];. The subscript "x" is print only one character form "textFileLetters" based on the value of "x" and that is why it seams random. Remove "[x]" and it will print the whole line.

Working with our code I tried this to see what would happen and it worked.

1
2
3
4
while (std::getline(infile,textFileLetters))
{
	std::cout << textFileLetters << std::endl;
}


I did have to change char textFileLetters[20]; to std::string textFileLetters;

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>  
#include <iostream>  
#include <string>
using namespace std;

string extractNameFrom( string line )
{
   int istart, iend;
   for ( istart = 0            ; istart <  line.size(); istart++ ) if ( isalpha( line[istart] ) ) break;
   for ( iend = line.size() - 1; iend   >= 0          ; iend--   ) if ( isalpha( line[iend  ] ) ) break;
   return line.substr( istart, iend - istart + 1 );
}

int main() 
{
   string line;
   ifstream infile( "Leaderboard.txt" );
   while ( getline( infile, line ) ) cout << extractNameFrom( line ) << endl;
   infile.close();
}


John Doe
Larry Bird
Michael Jordan
James Bond
Gary Smith
Gj
Vivien Vien
Samantha Carl
Gary Lewes
Sybil Saban
Hello Doe,

I think I figured out the problem.

1
2
3
4
5
6
7
8
9
10
char textFileLetters[25]; //array to store the names <--- Changed to 25.

for (int x = 0; x < 10; x++)
{
		// <--- Removed the seekg(0).
		infile.get(textFileLetters, sizeof(textFileLetters) - 1);
		infile.ignore();  // <---Added to remove the \n from the buffer berfore the next read.
	
		std::cout << textFileLetters << std::endl;  // <--- Removed the subscript to print whole line not a character.
}

The "infile.ignore();" solved the problem of the file stream failing after the first read. I believe it was the "\n" left in the input buffer causing the problem. See http://www.cplusplus.com/reference/istream/istream/get/ for more information on how it works.

Or I came up with this with input form lastchance:

1
2
3
4
5
6
7
8
9
10
std::string textFileLetters{ "" };
size_t pos{ 0 };

while (std::getline(infile, textFileLetters))
{
	for (size_t lp = 0; lp < textFileLetters.size(); lp++)
		if (isalpha(textFileLetters[lp])) break; else pos = lp;  // <--- or lp + 1 if you do not want a leading space.

	std::cout << textFileLetters.substr(pos) << std::endl;
}


This prints everything without the leading numbers and a leading space. See note in code for removing leading space.

 John Doe 23567
 Larry Bird 21889
 Michael Jordan 21889
 James Bond 13890
 Gary Smith 10987
 Gj 9989
 Vivien Vien 8890
 Samantha Carl 6778
 Gary Lewes 5667
 Sybil Saban 4677


Hope that helps,

Andy
Last edited on
Topic archived. No new replies allowed.