在 C++ 中使用 getline 的多个实例

发布于 2024-10-01 14:26:51 字数 777 浏览 0 评论 0原文

我一直在为 C++ 进行类分配,我们需要从文本文件获取输入并将这些值分配给数组......一个是字符串,第二个是 int,第三个是 double。

我们只了解了数组,我对指针或链表或任何更高端的东西一无所知,所以我觉得我的选择有些有限。我一整天都在努力找出一种从文本文件获取输入并将其分配给适当的数组的方法。我尝试使用 getline 读取输入文件并设置分隔符来分隔每条数据,但当我尝试多次使用它时出现错误。根据我所读到的内容,这与我如何重载该函数有关,但我无法解决它。我读到的关于它的每一个解释都超出了我目前的熟悉程度。现在,我专注于这段代码:

for (int i = 0; i < EMP_NUM; i++) // Get input from text file for names.
    getline(inFile, nameAr[i], '*');

for (int i = 0; i < EMP_NUM; i++) // Input for hours.
    getline(inFile, hoursAr[i], '*');

for (int i=0; i < EMP_NUM; i++) // Input for hourly rate.
    getline(inFile, hrateAr[i], '*');

我尝试使用 getline 三次并将数据写入三个单独的数组,然后用它们进行一系列计算并将它们输出到另一个文本文件。 getline 的第一个实例不会产生任何编译器错误,但后两个实例会产生任何编译器错误。我不太确定是否有其他解决方案可以将数据放入我的数组中,所以我不知所措。任何帮助都会很棒!

I've been working on a class assignment for C++ and we're required to acquire input from a text file and assign those values to an array....one is a string, the second an int, and the third a double.

We've only been introduced to arrays and I don't know anything yet about pointers or linked lists, or any of the higher end stuff, so I feel like I'm somewhat limited in my options. I've worked all day trying to figure out a way to acquire input from the text file and assign it to the appropriate array. I've tried to use getline to read the input file and set a delimiter to separate each piece of data but I get an error when I try to use it more than once. From what I've read, this has to do with how I'm overloading the function but I'm at a loss at resolving it. Every explanation I've read about it goes beyond my current level of familiarity. Right now, I'm focused on this fragment of code:

for (int i = 0; i < EMP_NUM; i++) // Get input from text file for names.
    getline(inFile, nameAr[i], '*');

for (int i = 0; i < EMP_NUM; i++) // Input for hours.
    getline(inFile, hoursAr[i], '*');

for (int i=0; i < EMP_NUM; i++) // Input for hourly rate.
    getline(inFile, hrateAr[i], '*');

I'm trying to use getline three times and write the data to three separate arrays, then make a series of calculations with them later and output them to another text file. The first instance of getline doesn't produce any compiler errors but the latter two do. I'm not quite sure of another solution to get the data into my arrays, so I'm at a loss. Any help would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

予囚 2024-10-08 14:26:51

如果我理解正确的话,文件中只有三个值:字符串、int 和 double。我假设它们是由空格分隔的。

如果是这样,那么您不需要 std::getline()。相反,使用提取运算符:

std::ifstream file("input.txt");
std::string s;
if( ! (file >> s) ) {  // a single word extracted from the file
    // failure
}
int n;
// ...

If I understand correctly you merely have three values in a file: a string, an int and a double. I assume they are delimited by whitespace.

If that is so then you don't need std::getline(). Rather, use the extraction operator:

std::ifstream file("input.txt");
std::string s;
if( ! (file >> s) ) {  // a single word extracted from the file
    // failure
}
int n;
// ...
笑叹一世浮沉 2024-10-08 14:26:51

1) 不使用三种不同的迭代,只使用一种

2) 在 getline 中传递字符串对象而不是指针

string buf;
for (int i = 0; i < EMP_NUM; i++) // Get input from text file for names.
{
    getline(inFile, buf, '*');
    nameAr[i] = buf;
    getline(inFile, buf, '*');  //assuming delimiter is again *
    hoursAr[i] = atoi(buf.c_str() );  //C way to doing it...however in c++ u have to use stringstreams....
    getline(inFile, buf);
    hrateAr[i] = atof(buf.c_str() );;
}

1) Instead of three different iteration, use only one

2) Pass string object in getline instead of pointers

string buf;
for (int i = 0; i < EMP_NUM; i++) // Get input from text file for names.
{
    getline(inFile, buf, '*');
    nameAr[i] = buf;
    getline(inFile, buf, '*');  //assuming delimiter is again *
    hoursAr[i] = atoi(buf.c_str() );  //C way to doing it...however in c++ u have to use stringstreams....
    getline(inFile, buf);
    hrateAr[i] = atof(buf.c_str() );;
}
千柳 2024-10-08 14:26:51

编译器错误说明了什么?您确定错误是由 getline 引起的吗?也许不是因为 getline 调用,而是因为 i 的多个声明。

What do the compiler errors say? Are you sure that the error is caused by getline? Maybe it's not because the getline calls but because of multiple declarations of i.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文