gpt4 book ai didi

c++ - 在 C++ 中对文件中的值单独执行计算

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:09 25 4
gpt4 key购买 nike

<分区>

所以我有一个输入文件,其中包含几个不同的整数值(每个在单独的行中),现在我需要读取每个值,找到平方根并获得输出。我遇到的问题是我的代码只从输入文件中读取第一个值。我觉得我应该使用循环来分别读取每个值,所以如果有人能帮助我,我将不胜感激。

float file_inp() //reads values from file and calculates the square root
{
float y = 0;
ifstream fin;
fin.open("input.txt",ios::in);
if (fin)
{
int x = 0;
fin >> x;
y=sqrt(x);
}

return y;
}

int main()
{
float y = 0;
cout << file_inp();

system("Pause");
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com