每次调用时如何获得一个函数来读取文档的下一行?

How can I get a function to read the next line of a document everytime it's called?

真正的新程序员在这里有一个非常糟糕的教授。

我有这段代码应该使用函数 (get_coefficients) 从文本文档 (inputsFile) 获取输入并用它做一些事情。目前,除了每次在 main 的 while 循环中执行时它从文件中读取同一行外,一切都运行良好。我周围有 google,但我找不到任何可在我的案例中实现的东西。我试过实现 while 循环并尝试传递某种计数变量,但似乎没有任何效果。

因为这是学校的作业,我不能做任何花哨的事情。所以解释越简单越好,哈哈。预先感谢您对我的帮助。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//=======================================FUNCTIONS======================================

//a function that displays instructions
void display_instructions() {

 cout << "Enter a, b, c: ";
}

//a function that gathers inputs
void get_coefficients(double& a, double& b, double& c) {
 ifstream inputsFile;
 string inputs;
 string inputString;
 inputsFile.open("textinputs.txt");
     inputsFile >> a >> b >> c;
     cout << a << b << c;
 inputsFile.close();
}
//a function that calculates a discriminant
double calc_discriminant(double a, double b, double c) {
 double discriminant = (b * b) - (4 * a * c);
 return discriminant;
}


//a function that calculates root 1
double calc_root_1(double a, double b, double c, double disc) {
 double r1 = ((-b) + (sqrt(disc))) / (2 * a);
 return r1;
}
//a function that calculates root 2
double calc_root_2(double a, double b, double c, double disc) {
 double r2 = ((-b) - (sqrt(disc))) / (2.0 * a);
 return r2;
}

void display(double r1, double r2) {
 cout << "the roots are " << r1 << " and " << r2 << "." << endl;

}

//=======================================EXCECUTE=======================================
int main()
{
 //while again is true
 for (int i = 0; i < 3; i++) {
     double a, b, c;

     display_instructions();

     //run get numbers   
     get_coefficients(a, b, c);

     //if discrimant less than 0 stop the program    
     if (calc_discriminant(a, b, c) < 0) {
         cout << "No real solution" << endl;
     }
     else {   //else get the roots
         double disc = calc_discriminant(a, b, c);
         double r1 = calc_root_1(a, b, c, disc);
         double r2 = calc_root_2(a, b, c, disc);
         //print the roots
         display(r1, r2);
     }
 }
}

这里的问题是,当您执行 inputsFile.open() 时,您是从头开始打开文件。你应该只打开文件一次,这样每次你从它读取时,你都会从上次所在的位置读取下一行。但是,如果您不将其保存在任何地方,ifstream 将在范围末尾被删除。您可以做的是在 main() 中初始化 ifstream,然后通过引用将其传递给函数,这样 ifstream 将一直存在,直到程序到达 main.

的末尾

我认为这应该可以满足您的需求:

#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
//=======================================FUNCTIONS======================================

//a function that displays instructions
void display_instructions() {

 cout << "Enter a, b, c: ";
}

//a function that gathers inputs
void get_coefficients(double& a, double& b, double& c, ifstream& inputsFile) {
 string inputs;
 string inputString;
     inputsFile >> a >> b >> c;
     cout << a << b << c;
}
//a function that calculates a discriminant
double calc_discriminant(double a, double b, double c) {
 double discriminant = (b * b) - (4 * a * c);
 return discriminant;
}


//a function that calculates root 1
double calc_root_1(double a, double b, double c, double disc) {
 double r1 = ((-b) + (sqrt(disc))) / (2 * a);
 return r1;
}
//a function that calculates root 2
double calc_root_2(double a, double b, double c, double disc) {
 double r2 = ((-b) - (sqrt(disc))) / (2.0 * a);
 return r2;
}

void display(double r1, double r2) {
 cout << "the roots are " << r1 << " and " << r2 << "." << endl;

}

//=======================================EXCECUTE=======================================
int main()
{
    ifstream inputsFile;
    inputsFile.open("textinputs.txt");
    //while again is true
    for (int i = 0; i < 3; i++) {
         double a, b, c;

         display_instructions();

         //run get numbers
         get_coefficients(a, b, c, inputsFile);

         //if discrimant less than 0 stop the program
         if (calc_discriminant(a, b, c) < 0) {
             cout << "No real solution" << endl;
         }
         else {   //else get the roots
             double disc = calc_discriminant(a, b, c);
             double r1 = calc_root_1(a, b, c, disc);
             double r2 = calc_root_2(a, b, c, disc);
             //print the roots
             display(r1, r2);
        }
    }
    inputsFile.close();
}