Trouble using an input facilitator function to read a list with tabs.

Working on a small ungraded assignment for my class and have stumbled upon a problem. We are suppose to be using a facilitator input function (see below) to read from a .txt file. The .txt file is a list of the states and includes Abbreviation (space) Name (Tab) Capital (Tab) Population... The issue I am running in to is with the reading of the tab before and after the Capital. I have the error with the first '\t' token, error C2679. Any help appreciated, thank you.


Code I have so far:

void State::input(istream& in) {
in >> m_abbr >> " " >> m_name >> '\t' >> m_capital >> '\t' >> m_pop >> " " >> m_rep >> " " >> m_joined >> " " >> m_area;
}
>> " " Text in quotes is a const char* in C++. So how do you think you can into it ???
Try this:
1
2
3
4
void State::input(istream& in) 
{
  in >> m_abbr >> m_name >> m_capital >> m_pop >> m_rep >>  m_joined >> m_area;
} 

The stream will skip whitespace by default. If you need to read delimiter then you need to declare a char variable and read into it.
Ah okay, I was unsure if a state name like Rhode Island would cause an issue and the program would read Rhode as the m_name and Island as the m_capital.

Edit: Moved along in the project and this problem is coming up now, while I create a state object and fin >> state to display the information, the program is not recognizing the tabs before and after the m_capital. How would I fix this?

Code:

This is the loop were given:

while (fin >> state)
{
state.display(cout);
cout << endl;
}

Here are my input and output facilitator functions:

void State::input(istream& in) {
in >> m_abbr >> m_name >> m_capital >> m_pop >> m_rep >> m_joined >> m_area;
}

void State::output(ostream& out) const {
out << m_abbr << " " << m_name << '\t' << m_capital << '\t' << m_pop << " " << m_rep << " " << m_joined << " " << m_area<< " " << endl;
}
Last edited on
If a string contains space you need to use getline with '\t' as delimiter.
If a string contains space you need to use getline with '\t' as delimiter.


Edit:

Changed the code to this:
void State::input(istream& in) {
string info;
getline(in, info, '\t');
}

Now I just need to figure out how to correctly read the information from the .txt file
Last edited on
Topic archived. No new replies allowed.