I apologize, the topic of this thread has nothing to do because this post was started about a parse/syntax error but I solved it on my own and now this is something else, hopefully simple!
I have this function that goes like this

void er(void)
{
    string erbase[]    ={"parl",     "chant",     "dans",     "ferm"};            //french verb base
    string ertrans[]   ={"to talk",  "to sing",   "to dance", "to close"};      //english translation 1
    string eralttrans[]={"to speak", " ",          " ",         " "};                  //englsh translation 2
    int r = rand()%4;
    cout << "The verb "<<erbase[r]<<"er means ";
    char ans[16];
    cin.getline(ans,16);    
    if((ans==ertrans[r])||(ans==eralttrans[r]))
    {
    cout << "Good!";
    }
}
 
int main(int nNumberofArgs, char* pszArgs[])
{
    cout << 
"This program is to be used as tool for learning french verbs. A french verb in\n"
"the infinitive form will be randomly chosen. You will be asked to translate the\n"
"infinitive form into english, and to conjugate the french verb into the present,"
"imperative, past (passe compose), imperfect, future, and conditional tenses.\n"
"It is important to read the 'READ ME' file before continuing.\n\n"
"Have you read the 'READ ME' file? (Enter Y for yes or N for no.) ";
    char yesno;
    cin >> yesno;
    int exit = YesNo(yesno);              //I have another function referenced here that works fine.
    if(exit==0){return(0);};              //This kicks them out if they haven't read the 'README' :)
    cout << endl;             
    for(;;){er();};                            //I know this is infinite loop, I will add constraints later.
    system("PAUSE");
    return(0);
}

The output looks like

This program is to be used as tool for learning french verbs. A french verb in
"the infinitive form will be randomly chosen. You will be asked to translate the
"infinitive form into english, and to conjugate the french verb into the present,
"imperative, past (passe compose), imperfect, future, and conditional tenses.
"It is important to read the 'READ ME' file before continuing.
 
"Have you read the 'READ ME' file? (Enter Y for yes or N for no.) Y    //This output is from YesNo()
 
The verb parler means The verb parler means to speak
Good!The verb danser means to dance
Good!The verb chanter means

The blue is my input.
So my questions are:
Why does it print double the first time?
Also, I don't want the "Good!" on the next line, I think it's got something to do with cin.getline(), but I'm not sure. Any help?

Recommended Answers

All 5 Replies

Try This

void er(void)
{
    string erbase[]    ={"parl",     "chant",     "dans",     "ferm"};            //french verb base
    string ertrans[]   ={"to talk",  "to sing",   "to dance", "to close"};      //english translation 1
    string eralttrans[]={"to speak", " ",          " ",         " "};                  //englsh translation 2
    int r = rand()%4;
    cout << "The verb "<<erbase[r]<<"er means ";
    char ans[16];
    cin.sync();
    cin.getline(ans,16);    
    if((ans==ertrans[r])||(ans==eralttrans[r]))
    {
    cout << "Good!";
    }
}

Thanks! That takes care of the redundancy redundancy. But the "Good!" is still in the wrong place. Do you think that it could be because when I input I have to press ENTER and that is also putting the next output on the next line?

Member Avatar for iamthwee

If you use cin.getline for all the input you shouldn't have this problem?

But I still do. I'll just have to make do with what it's giving me.

>Why does it print double the first time?
Because you type Y and then [Enter]. The Y is consumed by the cin, and the newline is left in the input buffer. This satisfies the first call to getline -- needing no input from you.

In general, take all input as strings.

>Also, I don't want the "Good!" on the next line
Learn to like it. Getting it to behave otherwise is a pile of crap you don't need to get into.

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.