getline() 函数的问题

发布于 2024-09-27 07:41:30 字数 1154 浏览 0 评论 0原文

我是 C++ 初学者,我第一次尝试使用 getline() 函数。 当我编写这段代码时,出现了 2 个错误。

这段代码应该做什么? 它应该从 read.txt 读取 4 个数字,然后计算它以找到平均值并将输出写入 output.txt。

这 4 个数字(在 read.txt 中)都位于单独的行中,如下所示:

6
12
15
19

这里是代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

    int main () 
    {
     ifstream readFile;
        ofstream sendFile;
     readFile.open ("read.txt");
     sendFile.open ("output.txt");;

     float mean;
     int num, num2, num3, num4;
     getline(readFile, num), getline(readFile, num2), getline(readFile, num3), getline(readFile, num4); 

     readFile >> num >> num2 >> num3 >> num4;
     sendFile << "1. The mean of " << num << ", " << num2 << ", " << num3 << ", and " << num4 << "is " << (num + num2 + num3 + num4) / 4;

     readFile.close();
     sendFile.close();

      system ("PAUSE") ;
      return 0;
    }

这里是错误: IntelliSense:没有重载函数“getline”的实例与参数列表 20 匹配 IntelliSense:函数调用 20 中的参数太少

I am a beginner at C++ and I'm trying to use the getline() function for the first time.
When I wrote this code, 2 errors showed up.

What is this code supposed to do?
It is supposed to read 4 numbers from read.txt then calculate it to find the mean and write the output in output.txt.

The 4 numbers (in read.txt) are all on separate lines like this:

6
12
15
19

Here is the code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

    int main () 
    {
     ifstream readFile;
        ofstream sendFile;
     readFile.open ("read.txt");
     sendFile.open ("output.txt");;

     float mean;
     int num, num2, num3, num4;
     getline(readFile, num), getline(readFile, num2), getline(readFile, num3), getline(readFile, num4); 

     readFile >> num >> num2 >> num3 >> num4;
     sendFile << "1. The mean of " << num << ", " << num2 << ", " << num3 << ", and " << num4 << "is " << (num + num2 + num3 + num4) / 4;

     readFile.close();
     sendFile.close();

      system ("PAUSE") ;
      return 0;
    }

Here are the errors:
IntelliSense: no instance of overloaded function "getline" matches the argument list 20
IntelliSense: too few arguments in function call 20

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

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

发布评论

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

评论(3

轮廓§ 2024-10-04 07:41:30

std::getline() 接受两个参数:一个流和要读取下一行的 std::string 对象(以及可选的第三个参数,分隔符) 。您传递的是 int 而不是 std::string

您可能应该使用普通的格式化提取:

if (readFile >> num >> num2 >> num3 >> num4) {
    // extraction succeeded!
}
else {
    // extraction failed; handle the error here
}

std::getline() takes two arguments: a stream and the std::string object into which to read the next line (and an optional third argument, the delimiter). You are passing an int instead of a std::string.

You should probably use the ordinary formatted extraction:

if (readFile >> num >> num2 >> num3 >> num4) {
    // extraction succeeded!
}
else {
    // extraction failed; handle the error here
}
白首有我共你 2024-10-04 07:41:30

getline 读入 std::string,它无法读入 int。只需使用readFile>>即可数>>数字2>>数字3>> num4; 就像您已有的那样,并删除带有 getline 的行。

另一方面,您不需要在此处显式关闭文件,因为文件流对象的析构函数将为您处理该问题。

getline reads into std::string, it can't read into ints. Just use readFile >> num >> num2 >> num3 >> num4; as you already have, and delete the line with getlines.

On another note, you don't need to close the files explicitly here as the destructors of file stream objects will take care of that for you.

半寸时光 2024-10-04 07:41:30

std::getline 是一个有用的工具,用于读取单行文本或读取文本直至特定字符,并将其写入 std::string ,然后可以在其中进一步读取。默认情况下,它使用换行符,即“\n”作为分隔符,但您可以更改它。

关于使用流读取多个整数然后输出它们的平均值,为什么不直接读取到文件末尾,因此:

int count = 0, total = 0, num;
while( instr >> num )
{
  ++count;
  total += num;
}

float mean = (count > 0 ) ? total / num : std::numeric_limits<float>::quiet_NaN();
outstr << mean;

您可以将其作为一个函数,采用 istream & instr 和 ostream & outstr

假设现在我们想要更改它以读取多行,每行都有由空格或制表符分隔的数字。在我们的输出中,我们将所有均值写在各自的行上。

现在做这样的事情:

std::string line;
while( std::getline( bigInStr, line ) )
{
   std::istringstream iss(line);
   outputMean( iss, outstr );
   outstr << '\n';
}

虽然您可能不想实际输出 NaN,但只是在输出中将该行留空。如果计算平均值的函数必须返回浮点数,则它可能会希望使用 NaN 作为返回值。如果我们愿意的话,我们可以在迭代时同时计算方差、偏度和峰度。

然后,您将在行上将它们输出为多个值,并且必须选择自己的分隔符。我自己的偏好是在这种情况下使用制表符('\t')。

_

std::getline is a useful tool for reading a single line of text or reading text up to a specific character, and writing it to a std::string where it can then be read further. By default it uses newline i.e. '\n' as the delimiter but you can change this.

With regards to using a stream to read in a number of integers then output their mean, why not just read to the end of the file, thus:

int count = 0, total = 0, num;
while( instr >> num )
{
  ++count;
  total += num;
}

float mean = (count > 0 ) ? total / num : std::numeric_limits<float>::quiet_NaN();
outstr << mean;

You could make that a function, taking istream & instr and ostream & outstr

Suppose now we want to change that to read multiple lines, each with numbers delimited by space or tab. In our output we write all the means on their own line.

Now do something like this:

std::string line;
while( std::getline( bigInStr, line ) )
{
   std::istringstream iss(line);
   outputMean( iss, outstr );
   outstr << '\n';
}

although you might want to not actually output the NaN but just leave that line blank in the output. A function calculating the mean probably would want to use a NaN as a return value if it has to return a float. We could calculate the variance, skewness and kurtosis at the same time if we want whilst iterating.

Then you would output these as multiple values on the line and you would have to pick your own delimiter. My own preference is to use tab ('\t') in this situation.

_

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