字符串和fstream的问题 [英] Problems with strings and fstream

查看:86
本文介绍了字符串和fstream的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在一个项目中,我需要实现几个类来模拟自助餐厅.每个正在排队等待获得食物的学生"都有5个描述他们的变量,即:名称,组,主菜类型,小吃/甜点类型,以及代表他们计划购买的沙拉量的数字的想法是,所有这些信息都将使用fstream从文本文件中读取(轮廓按特定顺序排列,并针对每个学生重复).读完每位学生后,我将其推入队列,模拟他们排队等候.

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.

我的问题有两件事,首先,当使用 getline()函数读取每一行时,我试图将该行存储在一个临时变量中,以便将其插入到构造函数中,以用于学生班,然后将其复制到队列中.这似乎是不允许的,因为当我尝试存储信息时,它说没有运算符'='与这些操作数匹配."

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() 返回istream&,这只是从中提取行之后的第一个参数.它将行读入第二个参数(在您的示例中为line2).因此,它看起来应该像这样:

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);
// ...

此外,getline读入字符串,因此您需要将该字符串转换为整数,然后再将其存储在temp_salad中.有几种方法可以做到这一点,最简单的方法是

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());

只需确保在该文件中#include <cstdlib>.

这篇关于字符串和fstream的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆