简体   繁体   中英

C++ program crashes when reading from text file?

I am making a program that reads in a string of numbers from a text file and uses those numbers to create a polynomial. I am using a linked list to create the polynomial. Each node in the list represents a term in the polynomial. The nodes have data members such as coefficient and exponent and obviously a pointer that will point to the next node (term). I don't know what is wrong with my code, but when I try to read from the text file my program crashes. I know that my program can find the file (it's in the same project directory) but as soon as I type in the name and press enter it stops running. Here is my code.

#include "polynomial.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>       // for exit() call

using namespace std;


void makePolynomials( shared_ptr<Polynomial> [], int *x );


int main()
   {
   shared_ptr<Polynomial> poly[ 100 ];
   int nPolynomials;

   makePolynomials( poly, &nPolynomials );

   // print out the polynomials
   // -------------------------
   for (int j=0; j < nPolynomials; j++)
      cout << *(poly[j]);

   return 0;
   }



void makePolynomials( shared_ptr<Polynomial> poly[], int *nPolynomials )
   {
   // get the filename from the user and open the file
   // ------------------------------------------------
   char filename[50];
   cout << "Enter the filename: ";
   cin >> filename;

   ifstream infile;
   infile.open( filename );
   if (! infile.is_open()) {
      cerr << "ERROR: could not open file " << filename << endl;
      exit(1);
      }

   // read in the data and construct a polynomial for each input line
   // ---------------------------------------------------------------
   string polynom;
   while (getline( infile, polynom ))
      {
      poly[ *nPolynomials ] = shared_ptr<Polynomial>(new Polynomial( polynom ));
      nPolynomials++;
      }
   }

class Polynomial constructor

    Polynomial::Polynomial( string & str )
   {
   stringstream ss( str );  // stringstream lets us extract items separated by
                            // whitespace simply by using the >> operator
   double coefficient;      // to hold the coefficient
   int exp;                 // to hold the exponent
   head = nullptr;          // initialize head to null

   // read in coefficient-exponent pairs and add each term to the list
   // ----------------------------------------------------------------
   while (ss >> coefficient >> exp)
      if (coefficient != 0)   // don't make a 0 term
         head = shared_ptr<Term>(new Term( coefficient, exp, head ));
   }

Probably the following line is causing the problem:

   int nPolynomials; //contains a garbage value

Make it :

  int nPolynomials=0; // or initialise it to any other value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM