Search code examples
c++functionwhile-loopio

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


Really new programmer with a really bad professor here.

I have this code that is supposed to get inputs from a text document (inputsFile) using a function (get_coefficients) and do stuff with it. Currently, everything works perfectly except it reads the same line from the file every time it's executed in the while loop in main. I've google around but I can't find anything that is implementable in my case. I've tried implementing while loops and trying to pass some sort of counting variable but nothing seems to work.

Since this is an assignment for school, I can't do anything fancy. So the more elementary the explanation, the better please, haha. Thank you in advance for any help I get.

#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);
     }
 }
}

Solution

  • The problem here is that when you do inputsFile.open() you're opening the file from the start. You should only open the file once so each time you read from it you'll read the next line from where you were the last time. However, the ifstream will be deleted at the end of the scope if you don't save it anywhere. What you can do is initialize the ifstream in main() and then pass it to the function by reference so the ifstream will still be there until the program reaches the end of main.

    I think this should work for what you want:

    #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();
    }