C++ problem (redone)

michael michael8110 at terra.com.br
Fri Aug 27 00:28:52 PDT 2004


I hope this is better;they compile as written (i tested );i tried to set  
node *big=0; in getmax() but then i get segmentation fault.As for those  
flags,the warning didn't go away
   michael ~ # g++ -Wall -ansi -pedantic input_from_user.cpp
input_from_user.cpp:63:8: warning: no newline at end of file
The linked lists i prefer to write myself at this point,for learning  
purposes.Again,this is probably still full of faults,and i'm afraid my  
level of knowledge doesn't allow me to quite fully  understand all your  
comments (especially as to references and ponters),but i must carry on as  
i can,and if you will bear with me,the fact remains that the second  
program will work only as it stands,with the last lines commented out,and  
i cannot see why,since the only difference between them is data  
input.Maybe at a later stage (if your patience and kindness allows for  
that ) i can send you the whole program (with reorder and insert ),which  
in the read from file version spews out the same meaningless number over  
and over,and at the very end,the expected results!Can't thank you enough  
for your time.One last thing:if you should do me the honour of running  
these :-) make sure you do not already have a progr_output file.

Input_from_user.cpp
#include<string>
#include<iostream>
#include<fstream>
using namespace std;

  class car
  {public:
   int itsYear;
   string itsName;
   };

   class node
   {public:
    car itsCar;
    node *itsNext;
   };

   node * fillin(node *head,int n)
   {node *temp=head;
    int a;
    string b;
    for (int i=0;i<n;i++)
    {cout<<"input a car year \n";
     cin>>a;
     temp->itsCar.itsYear=a;
     cout<<"input a car name \n";
     cin>>b;
     temp->itsCar.itsName=b;
     temp->itsNext=new node;
     temp=temp->itsNext;}
     return head;
     }

    void list(node *head)
    {node *temp=head;
     ofstream file("progr_output");
     while(temp->itsNext!=0)
       {file<<temp->itsCar.itsYear<<"\n";
       file<<temp->itsCar.itsName<<"\n";
       temp=temp->itsNext;}}

     node * getmax(node *head)
     {node *temp=head;
      node *big=new node;
      while(temp->itsNext!=0)
      {if(temp->itsCar.itsYear > big->itsCar.itsYear)
      {big=temp;}
      temp=temp->itsNext;}
      return big;
      }

      int main()
      {int a;
       node *head=new node;
       node *hea=0;
       cout<<"input the number of items in the list \n";
       cin>>a;
       list(fillin(head,a));

       hea= getmax(head);
       list(hea);
       return 0;
       }

--  Input_from_file.cpp
#include<string>
#include<iostream>
#include<fstream>
using namespace std;

  class car
  {public:
   int itsYear;
   string itsName;
   };

   class node
   {public:
    car itsCar;
    node *itsNext;
   };

    node * fillin(node *head,int n)
    {node *temp=head;
     ifstream file("progr_output");
     for (int i=0;i<n;i++)
     {char a[45];
      file.getline(a,45);
      int x=atoi(a);
      temp->itsCar.itsYear=x;
      char b[45];
      file.getline(b,45);
      temp->itsCar.itsName=b;
      temp->itsNext=new node;
      temp=temp->itsNext;}
      return head;
     }

    void list(node *head)
    {node *temp=head;
     ofstream file("progr_output");
     while(temp->itsNext!=0)
       {file<<temp->itsCar.itsYear<<"\n";
       file<<temp->itsCar.itsName<<"\n";
       temp=temp->itsNext;}}

     node * getmax(node *head)
     {node *temp=head;
      node *big=new node;
      while(temp->itsNext!=0)
      {if(temp->itsCar.itsYear > big->itsCar.itsYear)
      {big=temp;}
      temp=temp->itsNext;}
      return big;
      }

      int main()
      {int a;
       node *head=new node;
       node *hea=0;
       cout<<"input the number of items in the list \n";
       cin>>a;
       list(fillin(head,a));
       /*
       hea= getmax(head);
       list(hea);*/
       return 0;
       }

Using Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the lfs-chat mailing list