C++ problem still

michael michael8110 at terra.com.br
Sun Aug 29 00:35:13 PDT 2004


Going straight to the matter,this is noproblem.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;
	cout<<"input the number of items in the list \n";
	cin>>a;
	list(fillin(head,a));
	cout<<getmax(head)->itsCar.itsYear;
	return 0;
}

OUTPUT:
   michael ~ # ./a.out
input the number of items in the list
3
input a car year
45
input a car name
t
input a car year
67
input a car name
u
input a car year
89
input a car name
h
89
AND this is myproblem.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;
	cout<<"input the number of items in the list \n";
	cin>>a;
	list(fillin(head,a));
	cout<<getmax(head)->itsCar.itsYear;
	return 0;
}
OUTPUT:
   michael ~ # ./a.out
input the number of items in the list
4
1076081740

So again,noproblem reads from user input and myproblem, from the file the  
first one output to.They're to cout the max year value,the first does that  
but the second prints that huge number.The problem lies within getmax  
,because list works fine in both cases.But why does it work here and not  
there?Thanks again for any ideas.
-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/m2/



More information about the lfs-chat mailing list