Program only reads the first line! C++

时间:2021-07-22 06:53:32

my program is supposed to use a .txt file as input (time in seconds 1 through 10) to calculate the distance of a falling object. The text file reads as follows:

我的程序应该使用.txt文件作为输入(时间以秒为单位1到10)来计算下落物体的距离。文本文件如下:

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

And here is my code so far.

到目前为止,这是我的代码。

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;

//function prototype
double fallingDistance (int);

void main()
{
    ifstream inputFile;
    int time;
    double distance;

    //open the file
    inputFile.open("05.txt");
    inputFile >> time;

    {
        distance = fallingDistance (time);
        cout << time << "\t\t" << distance << endl;
    }
}
double fallingDistance (int time)
{
    double distance, gravity=9.8;
    distance = static_cast<double>(0.5 * gravity * pow(time,2));
    return distance;
}

And this is what my program compiles:

这就是我的程序编译的内容:

1 4.9 press any key to continue...

1 4.9按任意键继续...

Thanks in advance!

提前致谢!

1 个解决方案

#1


3  

cout << time << "\t\t" << distance << endl;

You first read an int from the input file. Next you initialize distance using time. Then you're printing the value of time, two tabs, and lastly, the value of distance.

您首先从输入文件中读取一个int。接下来,您使用时间初始化距离。然后你打印时间值,两个标签,最后是距离值。

After this line executes main returns and your program exits. Why would you expect it to print anything else?

此行执行主返回后,程序退出。为什么你会期望它打印其他东西?

If you need to grab more values from the file then you need to use a loop, wrapping that entire process in a loop which reads from the file until it gets through the whole thing. Something like:

如果你需要从文件中获取更多的值,那么你需要使用一个循环,将整个过程包装在一个从文件中读取的循环中,直到它完成整个过程。就像是:

inputFile.open("05.txt");
int time;
while(inputFile >> time) {     
    distance = fallingDistance (time);
    cout << time << "\t\t" << distance << endl;
}

On a side note, main is defined by the standard to have a return type of int, not void. Omitting the arguments (int argc and char *argv[]) as you have done is fine.

在旁注中,main由标准定义为返回类型为int,而不是void。如你所做的那样省略参数(int argc和char * argv [])很好。

#1


3  

cout << time << "\t\t" << distance << endl;

You first read an int from the input file. Next you initialize distance using time. Then you're printing the value of time, two tabs, and lastly, the value of distance.

您首先从输入文件中读取一个int。接下来,您使用时间初始化距离。然后你打印时间值,两个标签,最后是距离值。

After this line executes main returns and your program exits. Why would you expect it to print anything else?

此行执行主返回后,程序退出。为什么你会期望它打印其他东西?

If you need to grab more values from the file then you need to use a loop, wrapping that entire process in a loop which reads from the file until it gets through the whole thing. Something like:

如果你需要从文件中获取更多的值,那么你需要使用一个循环,将整个过程包装在一个从文件中读取的循环中,直到它完成整个过程。就像是:

inputFile.open("05.txt");
int time;
while(inputFile >> time) {     
    distance = fallingDistance (time);
    cout << time << "\t\t" << distance << endl;
}

On a side note, main is defined by the standard to have a return type of int, not void. Omitting the arguments (int argc and char *argv[]) as you have done is fine.

在旁注中,main由标准定义为返回类型为int,而不是void。如你所做的那样省略参数(int argc和char * argv [])很好。