string mixup problem with stdin.byLine

torea via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 7 19:44:20 PDT 2016


Hi all,

I'm still at beginner level in D and trying to make a simple note 
program in the terminal.
I've been struggling with a simple problem for the last 2 hours 
so I could use some help!

What I want to do is:
if I write #m, I record the following lines in a specific string 
member of a class, if I write #r, I record the following lines in 
another string member.
Here is the problematic part of my code :

bool stop = false;
auto enr = new Enreg();	// class Enreg{ public: string motsCles; 
string resume; }
enr.motsCles = "";
bool mode_motsCles = false;
bool mode_resume = false;
foreach( line; stdin.byLine ) {
	writeln( enr.motsCles );  // modified when I pressed #r <enter>
	string cleanLine = strip( cast(string)line );
	string[] words = split( cleanLine );	// la phrase est separee en 
mots

	switch( words[0] ){
		case "##":
			stop = true;
			break;
		case "#m":
			mode_motsCles = true;
			mode_resume = false;
			enr.motsCles = "";
			break;
		case "#r":
			mode_resume = true;
			mode_motsCles = false;
			enr.resume = "";
			break;
		default: break;
	}
	if( stop ) break;
	
	if( cleanLine[0] == '#' ) continue;
	if( mode_motsCles ){
		enr.motsCles = cleanLine;
	}else if( mode_resume ){
		enr.resume ~= cleanLine ~"\\n";
	}
	writeln( enr.motsCles );
}

when I press :
   #m <enter>
   things;stuffs; <enter>
at the end of my foreach, i have enr.motsCles initialized 
correctly with "things;stuffs;"
If I continue with :
   #r <enter>
enr.motsCles is modified at the very beginning of the loop and 
contain "#r\nings;stuffs;"

Anyone has an idea about what's going bad with my code?


More information about the Digitalmars-d-learn mailing list