微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何获得一个函数,该函数在每次调用它时都会读取文档的下一行?

如何解决我如何获得一个函数,该函数在每次调用它时都会读取文档的下一行?

真正的新程序员,这里的教授真的很糟糕。

我有这段代码,应该使用功能(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 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 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,c;

     display_instructions();

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

     //if discrimant less than 0 stop the program    
     if (calc_discriminant(a,c) < 0) {
         cout << "No real solution" << endl;
     }
     else {   //else get the roots
         double disc = calc_discriminant(a,c);
         double r1 = calc_root_1(a,c,disc);
         double r2 = calc_root_2(a,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 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 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,c;

         display_instructions();

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。