gpt4 book ai didi

c++ - 使用从外部文件导入的数据进行循环

转载 作者:行者123 更新时间:2023-11-28 05:53:04 24 4
gpt4 key购买 nike

我有一个包含 5 个数字的 data.txt 文件:32 99 135 0 -999 我的程序应该按顺序从文件中一个一个地获取所有第一个数字,计算这些数字的华氏度并将其存储到一个名为 result.txt 的文件。单击调试后,所有这些过程都应该是自动的。但是我不知道我的程序如何从一个数字跳到另一个数字。

#include <iostream>
#include <fstream>
#define in file "data.txt"

using namespace std;

int main ()
{
int a;
int b;
int c;
int d;
int e;
int carry;
int fah;
ofstream fout;
ifstream fin;
fin.open ("data.txt");
fout.open ("result.txt");
fin >> a >> b >> c >> d >> e;
fout << a << " " << b << " " << c << " " << d << " " << e;
for( a; a > -999; a--)
{
fah = a *9/5+32;
cout << "The Fahrenheit of "<< a << " is " << fah << ".";
}
}

最佳答案

while ( a, a!= -999, a--) 没有按照您的预期执行。 while 只能有一个终止条件。您需要一个像这样的 for 循环:

for( a=0; a > -999; a--)

这是一个带有控制变量a 的循环。在开始时,a 被设置为 0。每次运行 a 都会递减 1。当 a 变为 -999 时,循环终止。

如果您想使用 while 循环来代替,您必须这样做:

while ( a > -999 )
{
... your code ...
a--;
}

这个 if ( a,b,c,d != -999) 无论如何都不起作用。如果您想测试 a 是否不是 -999b 是否不是 -999c 不是-999d 不是-999,你必须这样写:

if ( a != -999 && b != -999 && c != -999 && d != -999)

关于c++ - 使用从外部文件导入的数据进行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34805183/

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