?
Solved

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

Posted on 2003-03-13
11
Medium Priority
?
280 Views
Last Modified: 2010-04-01
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
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
  • 5
  • 5
11 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8131763
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8134717
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
 

Author Comment

by:killer455
ID: 8138796
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!

 

Author Comment

by:killer455
ID: 8141414
Mayank,

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

Ben Johnson
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8142856
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
 

Author Comment

by:killer455
ID: 8144530
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8146119
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
 

Author Comment

by:killer455
ID: 8147776
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 300 total points
ID: 8149298
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
 

Author Comment

by:killer455
ID: 8149976
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 8149990
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

770 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