Hi,
I have finally found a solution to my problems. I decided to post it here, in case somebody else runs into similar issues.
1. The extra character problem
As I mentioned in my last post, there was an extra character in the string returned by the pigeUnMot() function. I did some testing, and found out that the last character always was a "\r" (carriage return character).
Now, about the getline() functon:
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).
|
On some other post, I found that depending on your OS, the newline sequences in texts files vary.
- In Windows, the newline sequence is "\r\n";
- In GNU/Linux, the newline sequence is "\n";
- In MacOS, the newline sequence is "\r".
The text file I use in this code (dico.txt), comes from a Windows environment. However, I am compiling this code on a GNU/Linux environment, which means that the getline() function will take everything up to the "\n" character, including the "\r" character. In other words, in my example above, with RAMOLIREZ, the getline() function actually took RAMOLIREZ\r, and stopped at the "\n", which explains the 11 character count, although there are truly only 10 letters in the word.
2. The overlapping console output problem
So in every string my pigeUnMot() function returns, there is a "\r" character (carriage return). When I cout these strings, the computer is told: "print the letters in the string, and then make a carriage return". In other words: "print the letters in the string, and then put the cursor back at the beginning of the line."
No wonder why everything is overlapping...
3. Solutions to this issue
I have found two solutions to this issue:
1) Systematically remove the last character of each string before printing it to the console. For example, in my case, I added the line:
motChoisit.pop_back();
at the end of my pigeUnMot function. This removes the last character in the string. In this case, it removes the unwanted "\r" character.
2) Use the
dos2unix program proposed by Duoas. Haven't tried this option yet, but I will very soon!
Hope this will help somebody not loose days on this, as I have.
@fabtasticwill: thanks for your support!
Sources
---
1) getline() function:
http://www.cplusplus.com/reference/string/string/getline/
2) Different OS newline sequences:
http://www.cplusplus.com/forum/general/51349/#msg279286 (Duoas's intervention)
3)
dos2unix link:
http://linux.die.net/man/1/dos2unix
EDIT: Just been reading the
dos2unix man pages, and found this interesting paragraph:
In DOS/Windows text files a line break, also known as newline, is a
combination of two characters: a Carriage Return (CR) followed by a
Line Feed (LF). In Unix text files a line break is a single character:
the Line Feed (LF). In Mac text files, prior to Mac OS X, a line break
was single Carriage Return (CR) character. Nowadays Mac OS uses Unix
style (LF) line breaks.
|
EDIT 2: Just tried
dos2unix program. Works like a charm! I read in that manual that it can work both ways, too. I prefer this solution, as it makes your program more portable, and simplifies the code.
Cheers!