#include <string>
#include <iostream>
#include <fstream>
#include <list>
using namespace std;

#include "mono.h"

int main()
{
int e;
string m;
string z;
string fname;

mono * mptr;
mono * mptr2;

list<mono>P1;
list<mono>P2;
list<mono>::iterator P1itr;
list<mono>::iterator P2itr;


cout<<"Menu"<<endl<<endl;
cout<<"A Enter two polynomials from a file"<<endl;
cout<<"B Enter two polynomials via keyboard"<<endl;
cout<<"G Quit"<<endl;
cout<<endl;
cout<<"Please enter A, B, or G"<<endl;
cin>>z;


if (z=="A")
    {
cin.ignore();
cout<<"Which file contains your first polonomial?"<<endl<<endl;
system("dir *.txt");
getline(cin,fname);
fstream fin;
fin.open(fname.c_str(),ios::in);

     while (!fin.eof())
     {
           getline(fin, m);
           fin>>e;
           fin.ignore();
           mptr=new mono(m,e);
           P1.push_front(*mptr);
     }
     
     P1.reverse();

//     fin.close();
     P1itr=P1.begin();
     while (P1itr!=P1.end())
     {
           P1itr->display();
           P1itr++;
     }
fin.close();
cout<<endl;     
cout<<"Which file contains your second polonomial?"<<endl<<endl;
getline(cin,fname);
fin.open(fname.c_str(),ios::in);

    while (!fin.eof())
     {
           getline(fin, m);
           fin>>e;
           fin.ignore();
           mptr2=new mono(m,e);
           P2.push_front(*mptr2);
     }

     fin.close();
     P2itr=P2.begin();
     while (P2itr!=P2.end())
     {
           P2itr->display();
           P2itr++;
     }

     }

my mono.h file

class mono
{
  public:
         mono(){}
         mono (string m, int e){monomial=m; exponent=e;}
         
         
         void display(){cout<<" + "<<monomial<<"x^"<<exponent;}
         
         
         bool mono::operator< (const mono &A)const
              {
              if(this->exponent==A.exponent){
              return (this->monomial < A.monomial);}
              return(this->exponent < A.exponent);
              }
         
  private:    
         string monomial;
         int exponent;   
};

The problem I am having is reading from a text file and placing the units into the correct order. The text files have the format of:

4 7
5 4
8 3


And should be read all together as : 4x^7 + 5x^4 + 8x^3 to create the polynomial. When I execute the file it reads:

4 7x^5 + 4x^8 3x^8

I realize it probobly has something to do with my pointers, but i just cant figure out where I am going wrong. I havent programmed in 2 years and now I am trying to dust the ol brain off and am stuck.

Also, when it prompts for second polynomial file, it will let me type the text file in, but will not display like poly1 would do. Essentially I need each polynomial in its own linked list. After I have them in separate linked lists, I have to run code to add the two together or multiply them. In order to do this though, I have to get them in their linked lists first :(

I figured out how to get it to read properly (i think). I still can't get poly2.txt to print out the list though.

fstream fin;fin.open(fname.c_str(),ios::in);      
while (!fin.eof())     
{           
fin>>m;  //changed from getline(fin, m);
fin>>e;           
fin.ignore();           
mptr=new mono(m,e);           
P1.push_front(*mptr);     
}     
 P1.reverse();

hmm, all these looks and no one helps me out. Looks like I won't receive a great grade on this homework.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.