Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Easy Code - Need Help fixing - c++ class

Posted on 2003-03-09
4
Medium Priority
?
291 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
  • 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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

  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 …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

564 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