Newline debugging

Hello, I am writing a driver program that will eventually need to pass two strings to a function I am writing in a separate file. I am reading data from a file that is formatted like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
ac: and
amo: love
amor: love
animal: animal
annus: year
ante: before, in front of, previously
antiquus: ancient
ardeo: burn, be on fire, desire
arma: arms, weapons
atque: and
aurum: gold
aureus: golden, of gold
aurora: dawn


I'm trying to get the latin word into one string and the english equivalent in another string. Also, each time I get an english equivalent I want to be able to send the two strings to my function. My code currently looks like this:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//#include "tree.h"

int main(int argc, char* argv[])
{
    string latinWord   = "",
           englishWord = "";

    char   buffer;

    bool   isLatinWord = true;

    ifstream vocabFile;
    vocabFile.open(argv[1]);

    if (!vocabFile)
       cout << "File open failed." << endl;

    while(vocabFile.get(buffer))
    {

        if (isLatinWord)
        {
            if (buffer == ':')
                isLatinWord = false;

            else
                latinWord+= buffer;
        }

        else
        {
            if (buffer == ',') // indicates 1 of multiple equivs processed
            {
                cout << englishWord << " = " << latinWord << endl;
                englishWord = "";
            }

            else if (buffer == '\n') // indicates all english equivs processed
            {
                cout << englishWord << " = " << latinWord << endl;
                latinWord = true;
                englishWord = latinWord = ""; // reset both strings
            }

            else
                englishWord+= buffer;
        }
    }
}


The way this SHOULD work is that if there is a colon, that symbolizes that the latin word string is finished populating (the flag is set to false) and then the english word string should start being populated. The english word string should be populated until a comma is hit (send the words to the function at this point), or a newline is hit (resetting the flag because all english equivs have been checked at this point).

However, when I try to output the strings I would send to my functions they are totally messed up. Here is my output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ ./prog5 latin.txt
 = ac
 = amo
 = amor
 = animal
 = annus
 before = ante
 in front of = ante
 = anteusly
 = antiquus
 burn = ardeo
 be on fire = ardeo
 = ardeo
 arms = arma
 = armas
 = atque
 = aurum
 golden = aureus
 = aureus
 = aurora


Only a few of the words are actually being processed in the way I want. It appears that only words followed by a comma are processed correctly. I'm thinking my code is recognizing newlines in a wrong way, and I was wondering if anyone sees any errors or has any suggestions?

Thanks,
Ben
Last edited on
Still nothing.. I noticed that after "in front of," there appears to be a word that mashes up with another word..
Topic archived. No new replies allowed.