Problems with strings and fstream

2019-08-12 17:11发布

I'm currently working on a project where I need to implement several classes to simulate a cafeteria. Each "student" who is waiting on line to get their food has 5 variables which describe them, that is: name, group, entree type, snack/dessert type, and a number representing the amount of salad they plan on buying in ounces. The idea is that all this information will be read in using fstream from a text file (with the outline following a specific order and repeating for each student). Once each student is read in, I push the student onto a queue to simulate them waiting their turn in line.

My issue is two things, first, when reading in each line using the getline() function, I attempt to store this line in a temporary variable for the purpose of plugging it into the constructor for the student class and then pushing that copy into the queue. This doesn't seem to be allowed because when I try to store the information it says "no operator '=' matches these operands."

The other issue I have is with reading in the ounces of salad value, this is an integer value, I have searched but I haven't found any way to directly read in a numerical value and pass it off to an integer variable. Sorry for the long explanation but I wanted to make sure I was clear, any help is appreciated.

Here is a portion of the code where I attempt to do this:

string temp_name;
string temp_group;
string temp_entree;
string temp_snack;
int temp_salad;


string line2;
queue<student> line;
ifstream myfile ("students.txt");
if(myfile.is_open())
    while(myfile.good())
    {
        temp_name= getline(myfile, line2);
        temp_group= getline(myfile, line2);
        temp_salad= getline(myfile, line2);
        temp_entree= getline(myfile, line2);
        temp_snack= getline(myfile, line2);

student s(temp_name, temp_group, temp_entree, temp_snack, temp_salad);
    //..... 
    }

3条回答
▲ chillily
2楼-- · 2019-08-12 17:30

getline doesn't return the string, so you shouldn't be trying to assign the return value to a string.

do:

getline(myfile, temp_name);

instead of

temp_name = getline(myfile, line2);

To read the next integer in the file use the stream input operators:

int temp_salad;
myfile >> temp_salad;
if (myfile.fail())
    std::cerr << "we failed to read temp_salad ounces\n";
查看更多
Ridiculous、
3楼-- · 2019-08-12 17:47

You can use operator>> to read an integer from input and put it into a variable, as in: myfile >> my_int;.

Since you've apparently defined student as a class, I'd overload operator>> for that class to read in all the data for a student:

std::istream &operator>>(std::istream &is, student &s) { 
    std::getline(is, s.name);
    std::getline(is, s.group);
    std::getline(is, s.entree);
    std::getline(is, s.snack);
    std::string temp;
    std::getline(is, temp);
    s.salad = lexical_cast<int>(temp);    
    return is;
}

Then we can read in student objects using that operator -- either directly in a loop rather like you had above, or rather indirectly, such as via an istream_iterator instantiated over that type, such as:

std::deque<student> line((std::istream_iterator<student>(infile)),
                          std::istream_iterator<student>());

Note that in this case I've used a deque instead of an actual queue -- in C++, queue is defined as an iterator adapter, and is sort of a second-class citizen in general. std::deque is often (including this case) simpler, even if you don't need the double-ended part of its definition.

查看更多
Fickle 薄情
4楼-- · 2019-08-12 17:50

getline() returns istream&, which is just the first argument after having the line extracted from it. It reads the line into the second argument (line2 in your example). So, it should look something like this:

getline(myfile, name);
getline(myfile, group);
// ...

Also, getline reads into strings, so you need to convert that string to an integer before you store it in temp_salad. There are several ways to do this, the easiest of which is

getline(myfile, temp);
salad = atoi(temp.c_str());

Just make sure to #include <cstdlib> in that file.

查看更多
登录 后发表回答