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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Easy: Class, code problem, problems compiling, help :)

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.  

Could anyone look over my code below and help with the corrections that I need to fix in order to get this thing to compile?  I am a beginning programmer with c++ so please try not to use anything fancy that I probably won't understand :)  Places of focus basically include the whole program, such as general as the general code, passing variables, etc. but I am pretty sure my code for comparing the lists is wrong but I tried.  Thanks for the help!


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

#ifndef LOTTERY_H
#define LOTTERY_H

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

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


void Lottery::SortList()
{
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;
  }
}

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

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

    list2.SortList()

    if (list1.CompareList(list2)) winner++;
   }
 cout << "Win " << winner << "times";  
 return 0;
}
0
killer455
Asked:
killer455
  • 5
  • 5
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hey Killer,

A couple of points.

This may look like a "Lottery" program, but trust me -- it's a homework assignment.  :)

You've asked for help getting this to compile.  The folks at EE are glad to help keep you moving in the right direction, but we're not compilers.  How about telling us where you get compilation errors and we'll try and help you out.

Your CompareList() method returns the number of matching selections.  You need to decide how the number of matches relates to winning.


Kdo
0
 
Mayank SAssociate Director - Product EngineeringCommented:
OK - here are some of the errors

1.
>> -----------------
>> LOTTERY.CPP
>> -----------------

>> #include "lottery.h"

>> Lottery::Lottery()
>> {
>> count=0;
>> winner=0; // ???????? - THIS IS NOT A DECLARED CLASS MEMBER
>> }

2.
>> -------------------
>> LOTTERY.H
>> -------------------

..
..

>> void SortList(int arr);

And in LOTTERY.CPP, the function definition is for a function with no arguments. As per your need, I think you should remove this argument from the function prototype in LOTTERY.H.

3 & 4.
>> -----------------
>> LOTTERY.CPP
>> -----------------

..
..

>> void Lottery::SortList()
>> {
>> for (int i = 1, j; i<5; i++)
>>  {
>>    int tmp = array[i];
>>    for (j = i-1; j>=0 && array[j]>tmp; j--) // ?????? - 'j' IS NOT DECLARED

>> int Lottery::CompareList(Lottery& lot2)
>> {
>> int winner = 0;
>> for(int i=0; (i<count) && (i<lot2.count; i++) // THE LEFT PARENTHESIS HAS NOT BEEN CLOSED (i<lot2.count


First, correct those, then post your updated code and I'll help you out with the rest.

Mayank.
0
 
killer455Author Commented:
Mayank,

Thanks for the corrections.  I hope I entrepreted your tips correctly.  Here is a summary of the changes I made listed respectively to your tips.

1) Declaring winner.
   private:
   int count;
   int winner;  // line added
   int arr[5];

2) Removing argument from SortList prototype.
   public:
   void SortList();  // edited line

3) Isn't j already declared here...?? or is there
   something with loops where it needs to be declared
   for each loop?

   for (int i = 1, j; i<5; i++)    //  <<-- int i=1, j;
    {
      int tmp = array[i];
      for (j = i-1; j>=0 && array[j]>tmp; j--)

4) Paraenthesis closed.

Corrected code, unless #3 still needs to be corrected.

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

#ifndef LOTTERY_H
#define LOTTERY_H

class Lottery
{
public:
Lottery();
bool AddNumber(int number);
void SortList();
int CompareList(Lottery& lot2);

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 = array[i];
   for (j = i-1; j>=0 && array[j]>tmp; j--)
     {
       array[j+1] = array[j];
     }
   array[j+1] = tmp;
 }
}

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

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

   list2.SortList()

   if (list1.CompareList(list2)) winner++;
  }
cout << "Win " << winner << "times";  
return 0;
}

Thanks
Ben
0
Industry Leaders: 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!

 
killer455Author Commented:
Mayank,

Would you like me to accept your answer and start a new question?

Ben Johnson
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, # 3 does not need to be changed. I didn't notice that 'j' was declared there. Anyways, as for your question, if you're satisfied with my answer and its working then do whatever you want to.

But yeah! In case you have declared 'winner' as a class-member, why are you defining another 'winner' variable in the CompareList () function?

In case there are any other problems, then please let me know.

Mayank.
0
 
killer455Author Commented:
Mayank,

I noticed that, yes i need to fix that.  Thanks for pointing the 'winner' problem out.  I guess my only problem now is figuring out how to get this counter to work.  See the compare method compares each selection in the two arrays with eachother and increments a counter if there is a match... but that not really what i need to count.  I need to count whether the WHOLE array matches and that there is a complete match... is there an easier way to do this?

Ben
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Let your CompareList () function return 1 if the first array is lesser or else, simply return a zero.

int Lottery :: CompareList ( Lottery & lot2 )
{
  for ( int i = 0 ; ( i < count ) && ( i < lot2.count ) ; i ++ )
    if ( arr[i] >= lot2.arr[i] )
      return 0 ; // end if, for

  return 1 ;

} // end of CompareList ()


It returns 0 if anywhere the element in arr[i] is greater than or equal to the element in lot2.arr[i], otherwise after the for loop finishes, it returns 1.

Mayank.
0
 
killer455Author Commented:
What happens when.. lets say...
arr[i]=2  and lot2.arr[i]=3

it will return 1

If all of the lot2 values are greater then it will still return 1, even though they wont match...??

It also returns 0 if they match? isnt it suppose to be true if they match?

Ben

0
 
Mayank SAssociate Director - Product EngineeringCommented:
Oh I am so sorry!

I got confused with another C++ problem where I was helping winner21.

Ya, if you want to compare the two lists and see if they're equal, then you can put it as:

return 1 if they are equal,
return 0 if they're not.

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

 return 1 ;

} // end of CompareList ()


Hope that helps!

Mayank.
0
 
killer455Author Commented:
Great answer(s).  Thanks a lot.  I am going to try to get it all going either tonight or tomorrow (monday).  I will post again if I have problems... is there anyway to contact you to let you know that I'm posting... your answers were very good.  Thanks again.

Ben
0
 
Mayank SAssociate Director - Product EngineeringCommented:
My e-mail ID is dontspamme"at"please.com. You can send me a mail to this ID. I am in India and its 12:50 PM right now. I'll stay in the office till 7:00 PM this evening, I guess.

Mayank.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now