C++ problem (redone)

Björn Lindberg d95-bli at nada.kth.se
Fri Aug 27 03:40:01 PDT 2004


michael <michael8110 at terra.com.br> writes:

> I hope this is better;

No it isn't. You have invented your own formatting, which is not very
wise when learning a new language, especially considering that you
seek the help of others. No C++ programmer formats his code like
yours. If you don't know how to format it, there are a wealth of
resources on the Internet, and you can have a look at the code in my
posts.

> 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

So do what it says! You are missing a newline at the last line of the
file, so add it!

> The linked lists i prefer to write myself at this point,for learning
> purposes.

Ok. You could try to write the list implementation more in C++ style
though, eg more object oriented. As it is now, it looks exactly like
C. But never mind that for now.

> 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.

I have inserted some comments below.

> Input_from_user.cpp
> #include<string>
> #include<iostream>
> #include<fstream>
> using namespace std;
> 
>   class car
>   {public:
>    int itsYear;

Don't do this! Format like this instead:

class car
{
public:
    int itsYear;
^^^^-- Note four spaces

<snip>

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

This is horrible formatting! Separate the braces, and put spaces on
both sides of most binary operators. Oh, and indent scope blocks, eg
by four spaces:

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;

<snip>

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

Replace above line with

        node * big = head;

>       while(temp->itsNext!=0)
>       {if(temp->itsCar.itsYear > big->itsCar.itsYear)
>       {big=temp;}
>       temp=temp->itsNext;}

Insert the following line before making the change above, run and see
what it writes out

        cout << big->itsCar.itsYear << '\n';

>       return big;
>       }

Here is your problem. You make a comparison with big->itsCar.itsYear
to see if the current element has a larger value, but big points to a
new, uninitialized node object. Since it is uninitialized, its fields
can have any value. Initialize big to the first element of the list
instead.

You can see this if you insert print statements at appropriate places
to print the value of big->itsCar.itsYear.

It is actually pure coincidence that you first program happens to
work. Simply because the undefined value in that case happened to
become low enough.


Björn



More information about the lfs-chat mailing list