首页 > 解决方案 > 每次调用文档时,如何获取读取文档下一行的函数?

问题描述

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

我有这段代码应该使用函数(get_coefficients)从文本文档(inputsFile)获取输入并用它做一些事情。目前,除了每次在 main 的 while 循环中执行时,它都会从文件中读取同一行之外,一切都运行良好。我用谷歌搜索,但我找不到任何可以在我的情况下实现的东西。我尝试实现 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);
     }
 }
}

标签: c++functionwhile-loopio

解决方案


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

推荐阅读