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;
}
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.