C++ problem -

michael michael8110 at terra.com.br
Wed Aug 25 20:44:03 PDT 2004


Hello,I'm trying to write a program that reads input from user,does a few  
things such as reorder the data,fetch and add  items,etc and then sends  
the data to a file.Initially the data would be taken from the user,but  
after that they would be read from the file ,whatever use of them be  
carried out as required,and then returned back to the  
file.Unfortunately,this second step simply won't work. Simplified code for  
the two programs,identical except for data input:

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

THE FIRST GIVES
   michael ~ # g++ input_from_user.cpp
input_from_user.cpp:61:2: warning: no newline at end of file
   michael ~ # ./a.out
input the number of items in the list
2
input a car year
54
input a car name
ford
input a car year
49
input a car name
buick
   michael ~ # cat ./progr_output
54
ford
49
buick

BUT THE SECOND
   michael ~ # g++ input_from_file.cpp
input_from_file.cpp:61:2: warning: no newline at end of file
   michael ~ # ./a.out
input the number of items in the list
2
   michael ~ # cat ./progr_output  (THE FILE IS EMPTY !)
   michael ~ #

I've tested this extensively,to the effect that input_from_file  
successfully reads the data and lists it back,
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;  THIS WORKS!

but upon introducing the function getmax() ,nothing works anymore.This  
function is part of a more complex reorder function .What really is odd is  
that the programs are identical except as to how they fetch their  
input.(Incidentally, if i run the complete programs,the read from file  
produces a huge list of the same big numberover and over and way down,the  
correct output !).I'm aware that few things can be as boring as reading  
someone else's code (listening to their dreams,perhaps ) but i'll be very  
thankful for any ideas.--
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the lfs-chat mailing list