Function call requiring endline

Mar 15, 2015 at 3:03pm
My function calls below will only return a result if I include an endline after each function call. The functions are all basic get functions (code below). I am second semester new to any type of programming, but I don't recall running into this particular issue before, and I am rather stumped.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    cout << "fname for index 10: "  << members[10].GetFirstName() << endl; //will return
    cout << "lname for index 32: " << members[32].GetLastName()<< endl; // will return
    cout << "NumPurc for index 56: " << members[56].GetNumBoxPurch();  // will not return
    cout << "pref for index 13: " << members[13].GetPreference();  // will not return

string MemberType::GetLastName() const
{
        return lastName;
}

string MemberType::GetPreference() const
{
        return preference;

}
Mar 15, 2015 at 3:07pm
What is the error telling you exactly?
Mar 15, 2015 at 3:47pm
I don't receive an error, it simply does not output the result of the function.

full output with endl as above:

fname for index 10: Dorothea
lname for index 32: Parbol

full output if I make the simple change of adding an endl to the other two lines of code:

fname for index 10: Dorothea
lname for index 32: Parbol
NumPurc for index 56: 7
pref for index 13: H

1
2
3
4
5
    cout << "fname for index 10: " << members[10].GetFirstName() << endl;
    cout << "lname for index 32: " << members[32].GetLastName() << endl;
    cout << "NumPurc for index 56: " << members[56].GetNumBoxPurch() << endl;
    cout << "pref for index 13: " << members[13].GetPreference() << endl;


thank you
Last edited on Mar 15, 2015 at 3:47pm
Mar 15, 2015 at 3:53pm
Have you tried getting first and last name without endl?
Mar 15, 2015 at 4:26pm
endl is a newline + a flush.

The flush is what guarantees that whatever you output has been printed. Without it, the data you sent to cout might be sitting in an output buffer waiting to actually be displayed.

If you want to make sure this data is visible, but you don't want the newlines added with endl, you can do a flush normally:

1
2
3
4
5
    cout << "fname for index 10: "  << members[10].GetFirstName() << '\n';
    cout << "lname for index 32: " << members[32].GetLastName()<< '\n';
    cout << "NumPurc for index 56: " << members[56].GetNumBoxPurch();
    cout << "pref for index 13: " << members[13].GetPreference();
    cout << flush;  // <- flush it.... make sure it's all output 




EDIT:

Note that I replaced your endl's with '\n's, since there is no need to flush until you've printed everything you want. Flushing has a bit of overhead, so it's a personal habit of mine not to do it unless I have to.

Leaving the endl's in won't hurt anything, though. So if you're more comfortable with them, then that's fine.
Last edited on Mar 15, 2015 at 4:28pm
Mar 15, 2015 at 4:35pm
Thank you very much!!!
Topic archived. No new replies allowed.