Could use a little help dealing with \n

The programs used to translate piglatin. It reads in the first half the word using a getline until the '-' is encountered and then uses another getline until a ' ' is encountered. The program works perfectly until a new line feed is encountered in which it adds an extra \n to the output. Theres a space at the end of each line and then a \n so I'm guessing thats were the issue is coming because the delimiter of the second input is ' ' and then when it reads for the next word it only finds an \n. I tried implementing an if statement so that if FirstSide.length() == 0 then it would read in FirstSide again, but that had no effect. Any ideas as to whats going on would be appreciated.
Last edited on
Technically, the newline character is a character, so the length of the string would at least be 1.
You could have the program go through each string after it is translated to discard any newline characters.

Example:
1
2
3
//...Whatever translation code...
if(dastring[dastring.length()-1] == '\n')   dastring.erase(dastring.length()-1,1);
//...Moving on... 

If you already stored the length of the string in some variable beforehand, then the above example can be truncated to be more readable.

Edit:
Forgot that most of your newline characters occur after our delimiter. Be careful of creating empty strings.
Last edited on
So I tried changing the 0 to a 1 and that created a lot more problems so I reverted it back.
I tried throwing your code in and it had no effect:
1
2
if (FirstSide[FirstSide.length()-1] == '\n'){
  FirstSide.erase(FirstSide.length()-1,1);

But to be honest I'm not entirely sure what that code is supposed to do in the first place.
I use this to get the words:
Last edited on
I just realized that I didnt know if it was the FirstSide or the SecondSide that \n was being attached to and added more code and found that its being added to the beginning of the Firstside somehow.
Last edited on
Bump, could really use help
What exactly is your issue? Is it the formatting of the output? Post your whole code please, maybe things can be clearer that way.
Right now I'm having troubles with \n's being read into the beginning of FirstSide when I use getline

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
void ToLower(string&);
void selectionsort(string[], int);
string Translate(string, string);
double ConstCount(string);
void GetInfile(string&);
void GetOutfile(string&);

int main(){

  ifstream input;
  ofstream output;
  int Count=0;
  double  Consonants=0;
  string Outfile, Infile, FirstSide, SecondSide, LatinWord, EnglishWord;
  char Delimeter('-'), Delimit(' '), EndLine = '\n';
  string WordArray[50];
  string LatinArray[50];

  GetInfile(Infile);
  GetOutfile(Outfile);

  input.open(Infile.c_str());
  output.open(Outfile.c_str());

  output << fixed << showpoint << setprecision(2);
  output << "Original Word Alphabetized" << endl;

  getline(input, FirstSide, Delimeter);
  while (!input.eof()){
    getline(input, SecondSide, Delimit);
    ToLower(FirstSide);
    ToLower(SecondSide);
    LatinWord = FirstSide + "-" + SecondSide;
    LatinArray[Count] = LatinWord;
    EnglishWord = Translate(FirstSide, SecondSide);
    WordArray[Count] = EnglishWord;
    output << EnglishWord << "------" << LatinWord << "========" << FirstSide << "_____" << SecondSide << endl;
    Count++;
    Consonants = Consonants + ConstCount(EnglishWord);
    getline(input, FirstSide, Delimeter);
  }
  output << endl;
  for (int i = 0; i<Count; i++){
    output << LatinArray[i] << endl;
  }
  output << endl;
  for (int j = 0; j<Count; j++){
    output << WordArray[j] << endl;
  }
  output << endl << "Average # of consonants per word: " << Consonants/Count << endl;
  if (Count % 2 == 1)
    output << "The Middle Word is \"" << WordArray[Count/2] << "\"" << endl;
  if (Count % 2 == 0)
    output << "The Middle Words are \"" << WordArray[Count/2 - 1] << "\" and \"" << WordArray[Count/2] << "\"" << endl;
  return 0;
}

string Translate(string word, string piglatin)
{
  string Translated;
  piglatin.erase(piglatin.end() - 2, piglatin.end());
  Translated = piglatin + word;
  return Translated;
}


output looks like
this------is-thay========is_____thay
is------is-ay========is_____ay
a------a-ay========a_____ay
little------ittle-lay========ittle_____lay
test------est-tay========est_____tay
to------o-tay========o_____tay
see------ee-say========ee_____say
h
ow------
ow-hay========
ow_____hay
the------e-thay========e_____thay
pig------ig-pay========ig_____pay
latin------atin-lay========atin_____lay
translation------anslation-tray========anslation_____tray
pr
ogram------
ogram-pray========
ogram_____pray
works------orks-way========orks_____way

is-thay
is-ay
a-ay
ittle-lay
est-tay
o-tay
ee-say

ow-hay
e-thay
ig-pay
atin-lay
anslation-tray

ogram-pray
orks-way

this
is
a
little
test
to
see
h
ow
the
pig
latin
translation
pr
ogram
works

Average # of consonants per word: 2.86
The Middle Words are "see" and "h
ow"
The code snippet I posted above checks if the last character in a string is a newline character and, if so, discards it. I use the length of the string minus 1 because index always ranges from 0 to array_length-1. I also use operator[] because I am used to treating std::string objects as character arrays, not containers. My snippet above is similar to:
1
2
//if you are more familiar with containers for some reason
if(*dastring.rbegin() == '\n')   dastring.pop_back();


From your other thread, I looked at the sample input you provided. I realize now that your code is actually appending the newline character to the beginning of the string, not the end. I apologize for recommending the wrong solution.
So instead of checking the end of the string, just check the beginning:
1
2
3
4
5
6
7
8
9
10
11
12
//...Transfer string...
if(FirstSide[0]) == '\n'){
      //Split up parameters in replace() function so it's easier to read
   FirstSide.replace(
      0,            //Start at beginning of string
      Firstside.length()-1,    //replace entire string excluding last character
      FirstSide.substr(1,Firstside.length()-1)  //Replace with string itself excluding the first character
   );
      //Finally, discard last character
   Firstside.pop_back();
}
//...Moving on... 

Warning: I did not test the code above so there might be bugs and mistakes.

Hope this helps.
Topic archived. No new replies allowed.