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);
//.....
}
getline doesn't return the string, so you shouldn't be trying to assign the return value to a string.
do:
instead of
To read the next integer in the file use the stream input operators:
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 overloadoperator>>
for that class to read in all the data for a student: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 anistream_iterator
instantiated over that type, such as: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 thedouble-ended
part of its definition.getline()
returnsistream&
, 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:Also,
getline
reads into strings, so you need to convert that string to an integer before you store it intemp_salad
. There are several ways to do this, the easiest of which isJust make sure to
#include <cstdlib>
in that file.