?
Solved

Easy Code - Need Help fixing - c++ class

Posted on 2003-03-09
4
Medium Priority
?
281 Views
Last Modified: 2010-04-01
I asked a previous questions concerning this program.  I have most of the code (i hope).  The problem is I am a beginner and to tell you the truth the following code is elementary and I just dont know that much about classes.  I am having trouble compiling my code and there are several errors in the coding.  If someone could give me a few tips or show me whats wrong in my code that would be great, as I am just beginning to learn classes.  Thanks.

This program takes input (5 numbers) from the user and they are inserted into an array.  Then the array is sorted.  Then input is taken from an input file containing sets of numbers:

numbers1.txt:
1 2 4 6 3
21 43 56 34 45
23 34 3 45 5

A set (of 5 numbers, or one line) is taken in, inserted into a second array, sorted, and then compared to the numbers inserted by the user.  If they match, increment a counter.  Then take in another line of numbers, sort, compare, etc and so on till end of file.  

Well here is the code, (think of it as kind of a lottery program).

-------------------
LOTTERY.H
-------------------

#ifndef LOTTERY_H
#define LOTTERY_H

class Lottery
{
public:
 Lottery();
 void addNumber(int arr[], int number);
 void SortList(int arr[]);
 bool CompareLists(winList[], numList[]);

private:
 int count;
 int winner;
 int winnersList[5];
 int numbersList[5];
};

#endif

-----------------
LOTTERY.CPP
-----------------

#include "lottery.h"

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

void Lottery::AddNumber(int arr[], int number)
{
 arr[count++]=number;
 return 0;
}

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

bool Lottery::CompareLists(winList[], numList[])
{
 for(int position=0; position<5; i++)
   {
     if(winList[position]!=numList[position])
    {
      winner=0;
    }
   }
 return winner;
}

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

int main()
{
 int winPosition=0;
 int numPosition=0;
 Lottery list1, list2;
 int winnersList[5], winNum, number;
 int numbersList[5];

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

 list1.SortList(winnersList[]);

 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(numbersList[], number);
    }

     list2.SortList(numbersList[])

    // right here i want to compare the two lists using the
    // compare method but am not sure how
    // if two lists match increment a counter

   }


 return 0;
}
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
  • 3
4 Comments
 
LVL 5

Accepted Solution

by:
Kocil earned 100 total points
ID: 8100425
-------------------
LOTTERY.H
-------------------

#ifndef LOTTERY_H
#define LOTTERY_H

class Lottery
{
public:
Lottery();
bool addNumber(int number);
void SortList(int arr);
int CompareList(Lottery& lott2);

private:
int count;
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;
}

int compareInt(int a, int b)
{
   return (a == b) ? 0 : (a<b ? -1 : 1);
}

void Lottery::SortList()
{
  // I confused about your sort algorithm.
  // this is the easy one
  qsort(arr, count, sizeof(int), compareInt);
}

int Lottery::CompareList(Lottery& lot2)
{
  int winner = 0;
  for(int i=0; (i<count) && (i<lot2.count; i++)
  {
    if(arr[i] == lot2.arr[i]) winner++;
  }
  return winner;
}

-----------------
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: ";
  for (winPosition=0; winPosition<5; winPosition++)
  {
    cin >> winNum;
    list1.AddNumber(winNum);
  }
  list1.SortList();

  ifstream fileInput;
  string inData;

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

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

  // Well, this is how to compare 2 list.
  // Please try your self to read it from file
  while (...) {

     // read 1 list from file to the list2

     // compare it
     if (list1.CompareList(list2)) winner++;

  }
  cout << "Win " << winner << "times";  
  return 0;
}


0
 

Author Comment

by:killer455
ID: 8103681
I am confused about a few parts in your answer, although it has helped me a lot.

1. For qsort(arr, count, sizeof(int), compareInt);
   Is there anything #include's i need to use for qsort?
   I guess what im asking is for list1.SortList(????);
   what are the parameters that need to be passed?

2. For int Lottery::CompareList(Lottery& lot2).  What is   lot2? and i think this is a typo, you have int CompareList(Lottery& lott2);.  I am confused about what lot2 is?  I'm guessing it is suppose to be list2 instead.

3. When counting the winners the two lists are compared.  However the counter increments everytime a single element matches a single element from the second list.  This means it will count 5 times for 2 lists when it should only count 1 time for the entire list if the entire list matches... this is where I had trouble before... i wasnt sure how to compare the entire list.

Thanks a lot Kocil your code correction above has helped a lot.

Ben
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8142338
1. qsort is in stdlib.h

2. The CompareList(Lottery& list2), compare the 'this' object with the list2.
So the command list1.CompareList(list2) mean compare list1 to list2.

3. yeah, sorry. The compare retuns number of matches. So if you want 5 matches to increase winner, it should be

   // compare it
   if (list1.CompareList(list2) == 5) winner++;

0
 
LVL 5

Expert Comment

by:Kocil
ID: 8142375
Bonus answer, regarding to :
The CompareList(Lottery& list2), compare the 'this' object with the list2.

You may write an override operator like this.
class Lottery
{
public:
 ...
 int CompareList(Lottery& list2));
 bool operator == (Lottery& list2)
   {CompareList(list2)==5;}

};


Then instead of
  if (list1.CompareList(list2) == 5)

you can use
  if (list1 == list2)

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 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