?
Solved

Easy Class problem - help on compiling

Posted on 2003-03-17
29
Medium Priority
?
238 Views
Last Modified: 2013-12-14
This is a lottery program.  Five numbers are inputed from the user into an array.  The array is then sorted.  This list, the first list, is considered the "winning numbers" list.  

A file containing sets of 5 numbers on a line... ex:

numbers1.txt
1 2 3 4 5
23 32 34 45 34
4 9 10 18 7
... etc.

A set of five numbers (one line) is taken in from the file a set at a time, put into an array, and then sorted.  The array is then compared to the "winning list" mentioned above.  If the arrays are identical a counter is incremented by 1, indicating a match or a "win".  This is done for every line in the file until the end of the file.  

I get the following compiler error when compiling.....

/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o: In function `_start':
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

If there are any other problems in the code let me know... if i am naming things with the same name and there is a name collision or something.

--------------
lottery.h
--------------
#ifndef LOTTERY_H
#define LOTTERY_H

class Lottery
{
public:
 Lottery();
 bool AddNumber(int number);
 void SortList();
 bool CompareLists(Lottery& list2);

private:
 int count;
 int winner;
 int arr[5];
};

#endif

----------------
lottery.cpp
----------------
#include "lottery.h"

Lottery::Lottery()
{
 count=0;
 winner=0;
}

bool Lottery::AddNumber(int number)
{
 if(count<5){
   arr[count++]=number; return true;
 }
 return false;
}

void Lottery::SortList()
{
 for(int i=1, j; i<5; i++)
   {
     int tmp=arr[i];
     for(j=i-1; j>=0 && arr[j]>tmp; j--)
       {
         arr[j+1]=arr[j];
       }
     arr[j+1]=tmp;
   }
}

bool Lottery::CompareLists(Lottery& list2)
{
 for(int i=0; (i<count) && (i<list2.count); i++)
   {
     if(arr[i]!=list2.arr[i])
       {
         return 0;
       }
   }
 return 1;
}

----------------
lotterycheck.cpp
----------------

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "lottery.h"
#include "lottery.cpp"

int main()
{
 int winner=0;
 int winNum=0;
 int winPosition=0;
 Lottery list1, list2;

 cout << "Enter the winning numbers: ";
 while(winPosition<5)
   {
     cin >> winNum;
     list1.AddNumber(winNum);
   }

 list1.SortList();

 ifstream fileInput;
 string inData;

 cout << "input file: ";
 cin >> inData;

 fileInput.open(inData.c_str());
 string lineOfNumbers;

 while(getline(fileInput, lineOfNumbers))
   {
     istringstream input(lineOfNumbers);
     while(input >> number)
       {
         list2.AddNumber(number);
       }

     list2.SortList();

     if(list1.CompareLists(list2))
     {
       winner++;
     }

   }

     cout << winner << endl;

 return 0;
}

Thanks
Ben
0
Comment
Question by:killer455
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 15
  • 14
29 Comments
 

Author Comment

by:killer455
ID: 8157250
Mayank,

I fixed the last array[], changed it too arr[]... but am still getting the error listed above.

Thanks
Ben
0
 

Author Comment

by:killer455
ID: 8157268
Mayank,

I fixed the last array[], changed it too arr[]... but am still getting the error listed above.

Thanks
Ben
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157300
>> In function `_start':

Can't see this function anywhere.

Mayank.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:killer455
ID: 8157314
Yes thats why im confused also.... see anything else wierd in the overall code?  I mean it looks fine to me..?  I have no idea whats wrong, any suggestions or reconfiguration of code?

Ben
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157337
#ifndef LOTTERY_H
#define LOTTERY_H
#endif

Try removing these three lines altogether. Though I wonder how it could still be of help..

And yeah! Is your "lottery.h" file in the same directory as the other header files like iostream, etc?

Mayank.

0
 

Author Comment

by:killer455
ID: 8157351
I dont have the iostream files....  its a university server and have never really had to deal with those files just use the includes.... ?? ill try removing those lines u mentioned.

0
 

Author Comment

by:killer455
ID: 8157354
I get the same error when removing those 3 lines.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157396
Then you should change your include-file settings such that it should also read files fom the folder where you have stored your "lottery.h" header-file, otherwise it'll not recognize that header-file in your program. If you don't have the permission to change the settings, then I suggest that you should remove your lottery.h and lottery.cpp header files, and copy-paste their text into your lotterycheck.cpp file.

Mayank.
0
 

Author Comment

by:killer455
ID: 8157409
Ok I found my problem.  I was using a wrong compiling command.  However...   When I am adding numbers to the second array from the stream... after adding the first 5 and then comparing... when I am getting ready to add the second set of 5 numbers into the array to replace the set that came before it.  How do I reset the count so it adds the numbers in the correct slot (beginning at 0, to 1... etc.?

COMMENTED BELOW

Code:

---------------
lottery.h
---------------
#ifndef LOTTERY_H
#define LOTTERY_H

class Lottery
{
 public:
  Lottery();
  bool AddNumber(int number);
  void SortList();
  bool CompareLists(Lottery& list2);

 private:
  int count;
  int winner;
  int arr[5];
};

#endif

---------------
lottery.cpp
---------------
#include "lottery.h"

Lottery::Lottery()
{
  count=0;
  winner=0;
}

bool Lottery::AddNumber(int number)
{
  if(count<5){
    arr[count++]=number; return true;
  }
  return false;
}

void Lottery::SortList()
{
  for(int i=1, j; i<5; i++)
    {
      int tmp=arr[i];
      for(j=i-1; j>=0 && arr[j]>tmp; j--)
        {
          arr[j+1]=arr[j];
        }
      arr[j+1]=tmp;
    }
}

bool Lottery::CompareLists(Lottery& lot2)
{
  for(int i=0; (i<count) && (i<lot2.count); i++)
    {
      if(arr[i]!=lot2.arr[i])
        {
          return 0;
        }
    }
  return 1;
}

-----------------
lotterycheck.cpp
-----------------
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "lottery.h"

int main()
{
  int winner=0;
  int winNum=0;
  int winPosition=0;
  Lottery list1, list2;

  cout << "Enter the winning numbers: ";
  while(winPosition<5)
    {
      cin >> winNum;
      list1.AddNumber(winNum);
    }
  list1.SortList();

  ifstream fileInput;
  string inData;

  cout << "input file: ";
  cin >> inData;

  fileInput.open(inData.c_str());
  string lineOfNumbers;
  int number;

  while(getline(fileInput, lineOfNumbers))
    {
      istringstream input(lineOfNumbers);
      while(input >> number)
        {
          list2.AddNumber(number);
        }

      list2.SortList();

      if(list1.CompareLists(list2))
        {
          winner++;
        }

       // THIS IS STILL IN THE WHILE LOOP AND THE
       // COUNTER SHOULD BE AT 4, but a new  
       // list is coming in... doesnt it need to be
       // reset somehow? either here or in the addnumber
       // method

    }
  cout << winner << endl;

  return 0;
}




0
 

Author Comment

by:killer455
ID: 8157437
Well after looking at the code im not sure if that is the problem.

If the input file is:
1 2 3 4 5
2 3 4 5 1
22 34 45 32 23
2 3 4 1 5
23 34 44 23 22

And the winning numbers are:
1 2 3 4 5

It comes up with 5 wins instead of the correct answer 3.

This is using the above code in previous comment.

I'll up the points for this problem as I am tending to add on questions for you, sorry :(

Thanks
Ben
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157440
What format have you stored the numbers in the file? There's probably an unexpected error in reading integers from the file. If you've simply typed in, say, 1243 in the file, then its better to read it in a string and convert it to a number as:

int number;
char str[10] ;

while ( getline (fileInput, lineOfNumbers ) )
{
  istringstream input ( lineOfNumbers ) ;

  while ( input >> str )
  {
    number = atoi ( str ) ;

...
...

See if that helps!

Mayank.
0
 

Author Comment

by:killer455
ID: 8157460
They are in the format
1 2 3 4 5
with spaces...

??

Ben
0
 

Author Comment

by:killer455
ID: 8157464
So you dont think it has to do with the count incrementation at all?

0
 

Author Comment

by:killer455
ID: 8157470
I see what your hinting at though... is there a way to just read them in straight as integers without having to convert the string to an integer...? or will this effect the getline code for taking only a line at a time?

Thanks
Ben
0
 

Author Comment

by:killer455
ID: 8157482
I tried the code you provided below with same input.  It comes up with winner being 0.

Ben
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157483
You'll have to first write them into the file (by a program) as integers:

Like:

ofstream fout ( "filename" ) ;
int num = 5 ;
fout << num ; // just an example to show how to write an integer: you'll have to write the whole array

See your file after that - it'll have some ASCII characters, not the usual digits 5, 4, etc

Now, when reading from the file, use:

while ( ! fileInput.eof () )
{
  for ( i = 0 ; i < 5 ; i ++ )
  {
    fileInput >> num ;
    list2.AddNumber ( num ) ;

  } // end for

  list2.SortList () ;

  if ( list1.CompareLists ( list2 ) )
    winner ++ ; // end if

} // end while


Mayank.
0
 

Author Comment

by:killer455
ID: 8157496
Ok I have to get off for the night.  But I ran the program several times with different input.  It is not getting past the first line in the input stream file... basically its not loading the second line of numbers in the file into list2... it is keeping the first set and not chaning.

Let me know if you can help.  If not I will clear this post and make a new one, to clean up a little and will let you know.

Thanks
Ben
0
 

Author Comment

by:killer455
ID: 8157506
I need to be able to access the file and see the actual number setsin the file, not ascii characters :( Ok Mayank I appreciate the help tonight, if you get something let me know but I will get a hold of you tomorrow.

Thanks a lot...
Ben
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157509
No.. no need to clear this question. Let all the comments above be there. However, just post your updated code on this page.

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157536
>>  need to be able to access the file and see the actual number setsin the file, not ascii characters

Oh, that was just for your information.

I will try to work on your code, modify it, and send you a compiled one.

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157841
Ok - caught a big error:

>> cout << "Enter the winning numbers: ";
>> while(winPosition<5)
>> {
>>   cin >> winNum;
>>   list1.AddNumber(winNum);
>> }

>> list1.SortList();

In this while loop, the value of winPosition is not being updated at all. Put:

winPosition ++ ;

inside its block.

Mayank.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157849
Also, since CompareLists () has a return-type of bool now, so we can change the return 1 and return 0 statements in that function to return true and return false respectively.

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8157940
Also:

>> void Lottery::SortList()
>> {
>> for(int i=1, j; i<5; i++)
   
Make it:

void Lottery :: SortList ()
{
  for ( int i = 1, j ; i< count ; i ++ )

// use 'count' for the range of the array.

Mayank.

   
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8158056
I executed a program trying your method. Its always reading an extra line from the file and putting some garbage values into it, before reaching out of the loop.

I will work on it and post you a solution when I have some more time....

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8158253
Ok, and another big error is that you are adding elements directly into your list2 object without clearing the previous elements in your nested while loop. So, hte previous numbers remain there and that is causing the problem which you stated.

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8158414
Ok, and another big error is that you are adding elements directly into your list2 object without clearing the previous elements in your nested while loop. So, hte previous numbers remain there and that is causing the problem which you stated.

Mayank.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8158419
Ok, and another big error is that you are adding elements directly into your list2 object without clearing the previous elements in your nested while loop. So, the previous numbers remain there and that is causing the problem which you stated.

Mayank.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 220 total points
ID: 8158464
/*
Ok, I have modified your code and this one runs fine if you have typed the numbers in the file and separated individual numbers by blanks, and kept new lists in new lines, like:

1 2 3 4 5
2 3 4 5 1
22 34 45 32 23
2 3 4 1 5
23 34 44 23 22

*/

#include <fstream>
#include <string>
#include <stdlib>

#define SIZE 5

class Lottery
{
     int count ;          // private by default
     int winner ;
     int arr[SIZE] ;
public:
     Lottery () ;
     bool AddNumber ( int ) ;
     void clear () ;
     void display () ;
     void SortList () ;
     void CompareLists ( Lottery & ) ;

} ; // class definition over

Lottery :: Lottery () // constructor ()
{
     count = 0 ;
     winner = 0 ;

} // end of constructor ()

bool Lottery :: AddNumber ( int number )
{
     if ( count < SIZE )
     {
          arr[count++] = number ;
          return true ;

     } // end if

     return false ;

} // end of AddNumber ()

void Lottery :: clear ()
{
     count = 0 ;

} // end of clear ()

void Lottery :: display ()
{
     cout << "\n The list is: " ;
     
     for ( int i = 0 ; i < count ; i ++ )
          cout << arr[i] << "\t" ; // end for

     cout << endl ;

} // end of display ()

void Lottery :: SortList ()
{
     for ( int i = 1 ; i < count ; i ++ )
     {
          int tmp = arr[i] ;
         
          for ( j = i - 1 ; j >= 0 && arr[j] > tmp ; j -- )
               arr[j+1] = arr[j] ; // end nested for

          arr[j+1] = tmp ;

     } // end outer for

} // end of SortList ()

bool Lottery :: CompareLists ( Lottery &list2 )
{
     for ( int i = 0 ; i < count && i < lot2.count ; i ++ )
          if ( arr[i] != lot2.arr[i] )
               return false ; // end if, for

     return true ;

} // end of CompareLists ()

int main()
{
     int winner = 0, winPosition = 0, winNum, num ;
     char str[80], * ptr, filename[80] ;
     Lottery list1, list2 ;

     cout << "Enter the winning numbers: " ;

     while ( winPosition < SIZE )
     {
          cin >> winNum ;
          list1.AddNumber ( winNum ) ;
          winPosition ++ ;

     } // end while

     list1.SortList();
     cout << "\n Winning list: " ;
     list1.display () ;

     cout << "\n Enter filename: " ;
     cin >> filename ;

     ifstream fileInput ( filename ) ;

     if ( fileInput.fail () )
     {
          cout << "Bad filename. " ;
          return 0 ;

     } // end if

     while ( ! fileInput.eof () )
     {
          fileInput.getline ( str, 80 ) ;
          list2.clear () ;

          if ( strcmp ( str, "" ) == 0 || strcmp ( str, "\n" ) == 0 )
               break ; // end if

          ptr = strtok ( str, " " ) ;

          while ( ptr != NULL )
          {
               number = atoi ( ptr ) ;
               list2.AddNumber ( number ) ;
               cout << number << "\t" ;
               ptr = strtok ( NULL, " " ) ;

          } // end while

          list2.SortList () ;
          list2.display () ;

          if ( list1.CompareLists ( list2 ) )
               winner ++ ; // end if

     } // end while

     cout << "Winner: " << winner << endl ;
     fileInput.close () ;

     return 0 ;

} // end of main ()


/*
Hope that helps!

Mayank. :-)
*/
0
 

Author Comment

by:killer455
ID: 8161983
Mayank,

Thanks a lot.  Got it working without having to convert it but would have never finished the program without your help.

thanks again
Ben
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question