Good Evening,

I'm working on my final project and I'm having an issue with reading input from a file and converting the string into an integer - specifically with the first character being read from the file.

Here is the data in the .txt file created in notepad:

6 -1 -1 -1 2 -1 -1 -1 9
-1 1 -1 3 -1 7 -1 5 -1
-1 -1 3 -1 -1 -1 1 -1 -1
-1 9 -1 -1 -1 -1 -1 2 -1
2 -1 -1 8 7 5 -1 -1 3
-1 -1 5 -1 1 -1 4 -1 -1
-1 7 -1 -1 8 -1 -1 9 -1
-1 -1 1 -1 4 -1 8 -1 -1
-1 -1 -1 2 5 9 -1 -1 -1

When the first character is read from the file, this is what is saved into the variable and then displayed on the screen:

6

when I use atoi to convert to an interger, it ends up being a 0. The code is pretty straight forward.

 ifstream inputFile;
 string one = "";
 inputFile.open("puzzle.txt");

 for (int x = 0; x < 9; x++) {
   for (int y = 0; y < 9; y++) {
   inputFile >> one;
   cout << one << " ";
   }
  cout << endl;
 }
 inputFile.close();

What am I missing to eliminate the garbage characters before the 6? I've tried using getline (inputFile, one); and used a while loop using while (inputFile >> one), setting one equal to \0 and a few other things I found through Google but it always reads the three garbage characters.

I can "fix" it by adding a value I don't need before the 6 (junk 6 -1 -1 -1 2 -1 -1 -1 9) and then putting an inputFile >> one; before the loop and it works as intended, however, that seems like poor programming practice. Is there any other way to remove the garbage from the first value read from the file?

Figured it out. Did a google search on the junk characters and apparently it's common for some editors to add UTF-8 characters. I recreated the text file and that fixed the issue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.