Hi,
I have a basic understanding of C++ and was creating something myself with a class of 6 integers and a string what I want to do is to be able to find the mode values for each of the six integers and strings I have instantiated a set of objects for the class that contain the values and I want to find the mode values in the objects.
I reached this far it all works so far as I have created the class the interface and the main methodbut i got stuck on finding the mode could some one please help me

Thanks

Kunal

Could you please post your code (so far) So we have something to work on =)

This is a personal project I want to create for myself to learn OO dev for C++I split it up into 3 files it is for the lottery to help me choose 6 of the most occurring values heres my code:
1)the class file/ interface:

#include<iostream>
using namespace std;
class lottery
{
public:
	lottery(int,int,int,int,int,int,int,string);
	//get methods
	int get_Ball_1();
	int get_Ball_2();
	int get_Ball_3();
	int get_Ball_4();
	int get_Ball_5();
	int get_Ball_6();
	int get_Bonus();
	string get_Machine_Name();
	//set methods
	void set_Ball_1(int ball1);
	void set_Ball_2(int ball2);
	void set_Ball_3(int ball3);
	void set_Ball_4(int ball4);
	void set_Ball_5(int ball5);
	void set_Ball_6(int ball6);
	void set_Bonus(int ballBonus);
	void set_Machine_Name(string);
	void display_results();
	int get_ball_1_Mode();
	int get_ball_2_Mode();
	int get_ball_3_Mode();
	int get_ball_4_Mode();
	int get_ball_5_Mode();
	int get_ball_6_Mode();
	int get_bonus_Mode();
	int get_Name_Mode();
private:
	int ball_1;
	int ball_2;
	int ball_3;
	int ball_4;
	int ball_5;
	int ball_6;
	int bonus;
	string machine_name;
};

2) the code behind the class

#include<iostream>
#include<string>
#include"Lottery.h";//include the lottery object header
using namespace std;
//constructor
lottery::lottery(int b1, int b2, int b3, int b4, int b5, int b6, int bon, string name)
{
	set_Ball_1( b1 );//call set functions to set ball numbers
	set_Ball_2( b2 );
	set_Ball_3( b3 );
	set_Ball_4( b4 );
	set_Ball_5( b5 );
	set_Ball_6( b6 );
	//set the bonus ball
	set_Bonus( bon );
	//set Machine name
	set_Machine_Name( name );
}//end Lottery constructor
//set methods
void lottery::set_Ball_1(int b1)
{
	ball_1 = b1;//store the ball1 in the object
}//end set_Ball_1 function
void lottery::set_Ball_2(int b2)
{
	ball_2 = b2;//store the ball 2 in the object
}//end set_Ball_2 function
void lottery::set_Ball_3(int b3)
{
	ball_3= b3; // store ball 3 in the object
}//end set ball_3 function
void lottery::set_Ball_4(int b4)
{
	ball_4 = b4;//store ball4 in the object
}//end set ball_4 function
void lottery::set_Ball_5(int b5)
{
	ball_5 = b5;//store ball4 in the object
}//end set ball_5 function
void lottery::set_Ball_6(int b6)
{
	ball_6= b6;//store ball6 in the object
}//end set ball_6 function
void lottery::set_Bonus(int Bon)
{
	bonus = Bon;
}//end function set_bonus
void lottery::set_Machine_Name(string name)
{
	machine_name = name;
}//end setMachine name
//get methods
int lottery::get_Ball_1()
{
return lottery::ball_1;//return Ball_1
}//end get ball 1 method
int lottery::get_Ball_2()
{
	return lottery::ball_2; // returns ball2 object
} //end get_ball_2 function
int lottery::get_Ball_3()
{
	return ball_3; //return ball_3 object
} //end get_ball_3 function
int lottery::get_Ball_4()
{
	return ball_4; //return ball_2 object
} //end get_ball_4 function
int lottery::get_Ball_5()
{
	return ball_5; //return ball_2 object
} //end get_ball_5 function
int lottery::get_Ball_6()
{
	return ball_6; //return ball_2 object
} //end get_ball_6 function
int lottery::get_Bonus()
{
	return bonus;//returns bonus ball
}//end the get function
string lottery::get_Machine_Name()
{
	return machine_name;//returns the machine_Name
}//end the get macine name function
void lottery::display_results()
{
	cout<< get_Ball_1()<<"-"<< get_Ball_2()<<"-"<< get_Ball_3()<<"-"<< get_Ball_4()<<"-"<< get_Ball_5()<<"-"<< get_Ball_6()<<" \tBonus: "<< get_Bonus()<<endl;
	cout<<"Machine: "<< get_Machine_Name() <<endl;
}
//find modes im stuck here I commented it out on purpose
/*int lottery::get_ball_1_Mode()
{
	
	return Mode_1;
}
int lottery::get_ball_2_Mode()
{
	
	return Mode_2;
}
int lottery::get_ball_3_Mode()
{
	return mode_3;
}
int lottery:: get_ball_4_Mode()
{
	return mode_4;
}
int lottery::get_ball_5_Mode()
{
	return mode_5;
}
int lottery::get_ball_6_Mode()
{
	return mode_6;
}
int lottery::get_Bonus()
{
	return Mode_Bonus;
}*/

3) the file with the main function

#include<iostream>
#include<string>
#include"Lottery.h";
using namespace std;
//main to create lottery objects
void load_Jun_draws()
{
	lottery lottery_22_06(16,22,23,36,45,48,19,"GUINEVERE");
	lottery lottery_25_06(5,9,15,30,32,39,31,"GUINEVERE");
	lottery lottery_29_06(5,6,13,21,31,47,30,"ARTHUR");
	cout<<"Lottery objects created for Jun 2011"<<endl;
}
void load_Jul_draws()
{
	lottery lottery_02_07(10,18,34,35,39,48,6,"GUINEVERE");
	lottery lottery_06_07(26,32,34,35,41,48,1,"GUINEVERE");
	lottery lottery_09_07(1,2,21,24,33,49,39,"ARTHUR");
	lottery lotter_13_07(7,11,15,18,19,49,25,"ARTHUR");
	lottery lottery_16_07(4,9,13,15,18,40,30,"GUINEVERE");
	lottery lottery_20_07(6,10,15,32,42,49,4,"ARTHUR");
	lottery lottery_23_07(1,2,11,30,33,45,39,"ARTHUR");
	lottery lottery_27_07(4,16,22,34,39,42,35,"ARTHUR");
	lottery lottery_30_07(4,10,29,32,34,40,48,"GUINEVERE");
	cout<<"Lottery objects created for Jul 2011"<<endl;
}
void load_Aug_draws()
{
	lottery lottery_03_08(18,19,26,39,40,47,4,"GUINEVERE");
	lottery lottery_06_08(1,14,18,24,26,28,19,"ARTHUR");
	lottery lottery_10_08(2,4,7,27,43,44,45,"ARTHUR");
	lottery lottery_13_08(5,30,31,33,35,45,3,"ARTHUR");
	lottery lottery_17_08(3,19,32,40,47,48,37,"GUINEVERE");
	lottery lottery_20_08(2,3,18,30,34,35,24,"ARTHUR");
	lottery lottery_24_08(11,27,46,47,48,49,42,"ARTHUR");
	lottery lottery_27_08(6,7,29,32,41,46,49,"GUINEVERE");
	lottery lottery_31_08(5,26,43,46,47,49,28,"GUINEVERE");
	cout<<"Lottery objects created for Aug 2011"<<endl;
}
void load_Sep_draws()
{
	lottery lottery_03_09(4,17,20,26,29,36,31,"ARTHUR");
	lottery lottery_07_09(4,29,31,36,38,48,35,"GUINEVERE");
	lottery lottery_10_09(5,7,9,10,22,47,37,"GUINEVERE");
	lottery lottery_14_09(23,28,29,37,38,42,49,"ARTHUR");
	lottery lottery_17_09(2,3,6,41,44,45,10,"GUINEVERE");
	lottery lottery_21_09(11,15,32,33,37,39,28,"GUINEVERE");
	lottery lottery_24_09(10,16,29,34,37,47,31,"GUINEVERE");
	lottery lottery_28_09(15,19,25,36,44,45,43,"GUINEVERE");
	cout<<"Lottery objects created for Sep 2011"<<endl;
}
void load_Oct_draws()
{
	lottery lottery_02_10(4,15,17,26,37,41,29,"GUINEVERE");
	lottery lottery_05_10(9,15,17,29,30,39,11,"GUINEVERE");
	lottery lottery_08_10(6,16,22,26,30,34,45,"ARTHUR");
	lottery lottery_12_10(13,24,25,26,37,40,30,"ARTHUR");
	lottery lottery_15_10(7,12,27,34,41,46,14,"ARTHUR");
	lottery lottery_19_10(13,14,18,32,34,49,20,"GUINEVERE");
	lottery lottery_22_10(20,27,28,34,43,44,1,"ARTHUR");
	lottery lottery_29_10(5,9,14,28,46,49,45,"GUINEVERE");
	cout<<"Lottery objects created for Oct 2011"<<endl;
}
void load_Nov_draws()
{
	lottery lottery_02_11(11,12,33,37,38,49,3,"GUINEVERE");
	lottery lottery_05_11(14,16,23,24,27,39,43,"LANCELOT");
	lottery lottery_09_11(2,8,10,23,27,32,44,"GUINEVERE");
	lottery lottery_12_11(6,12,13,28,33,38,10,"ARTHUR");
	lottery lottery_16_11(10,12,26,27,39,40,20,"GUINEVERE");
	lottery lottery_19_11(1,29,31,32,34,36,45,"ARTHUR");
	lottery lottery_23_11(4,11,19,22,31,48,6,"MERLIN");
	lottery lottery_26_11(13,16,25,26,36,46,15,"MERLIN");
	lottery lottery_30_11(13,23,24,25,26,30,4,"MERLIN");
	cout<<"Lottery objects created for Nov 2011"<<endl;
}
	void load_Dec_draws()
{
	lottery lottery_03_12(1,30,35,47,48,49,8,"MERLIN");
	lottery lottery_07_12(10,14,27,31,32,47,41,"MERLIN");
	lottery lottery_10_12(16,21,24,29,30,39,20,"LANCELOT");
	lottery lottery_14_12(3,10,25,28,30,43,31,"MERLIN");
	cout<<"Lottery objects created till 14/12/2011"<<endl;
}
int main()
{
	//create lottery objects for June 2011
	load_Jun_draws();	
	load_Jul_draws();
	load_Aug_draws();
	load_Sep_draws();
	load_Oct_draws();
	load_Nov_draws();
	load_Dec_draws();
}

this quite possibly was the most labourious way to code but Its the way I was learningany help to create the mode from the classes would be great
Thanks for your help...

commented: Do you understand the concept of sentences in the English Language? You should read up on them so your posts can be understood. -4

Firstly... Your class can be written a lot better:

class

class Lottery 
{
	public:
	         void setBall(int theBall);
		 int getBall();
	private:
		int ball;
};

functions

#include<iostream>
#include<string>
#include"Lottery.h";//include the lottery object header
using namespace std;
//constructor

lottery::lottery(int theBall)
{
     ball = theBall;
}

void lottery::setBall(int theBall)
{
   ball = theBall;
}
int lottery::getBall()
{
    return ball;
}

And then in main you could have:

#include <iostream>
#include "lottery.h"
using namespace std;
int main(int argc, char *argv[]) {

	Lottery lottery[500]; // 500 objects
	
	lottery[0].setBall(40);
	
	return EXIT_SUCCESS;
}

Then you'd have an array to work with. I haven't compiled this, but, this seems to be a much better way to set out classes than what you have (Not saying you're wrong, an excellent attempt) But.. You don't need to declare that many integers when you can use arrays :)

how about the 6 balls i want to set values for or do i only add them one at a time to a long array and the previous results i want to draw the mode from.

They'd just be an array.. 2 seconds, let me try something :)

would this not work for main

#include<iostream>
#include"lottery.h";
using namespace std;
int main()
{
lottery lottery[500]//create 500 array size object

lottery[0].set_Ball_1(40);//set all lottery objects
lottery[0].set_Ball_2(03);
lottery[0].set_Ball_3(05);
lottery[0].set_Ball_4(12);
lottery[0].set_Ball_5(10);
lottery[0].set_Bonus(21);
}

i dunno if this would work but:

Class "Lottery.h"

class Lottery
{
    public:
        Lottery();
    
        Lottery(int &theBalls);
        void setBall(int theBall);
        int getBall();
    private:
        int ball;
        int balls[500];
    
};

Functions "Lottery.cpp"

#include<iostream>
#include<string>
#include "Lottery.h";

Lottery::Lottery(){};

Lottery::Lottery(int &theBalls)
{
    // Could store them into an array
    // Can someone help with this?
    
}

void Lottery::setBall(int theBall)
{
    ball = theBall;
    
}

int Lottery::getBall()
{
    return ball;
    
}

Main "main.cpp"

#include <iostream>
#include "Lottery.h"

using namespace std;
Lottery lottery[500];


void january()
{
    lottery[0].setBall(10);
    lottery[1].setBall(20);
    lottery[2].setBall(30);
    
}

void feb()
{
    lottery[3].setBall(3403234);
    lottery[4].setBall(2140214);
    lottery[5].setBall(2140214);
}

int main()
{    
    january();
    cout << lottery[0].getBall() << endl;
    
    return EXIT_SUCCESS;
}

If that makes sense? I don't want to go into like Maps and stuff. Do you have to do it using a class?

Hope this helps you though... For the Mode, you could get the data range of a specific month!

Thank you for your help im going to try it later bit busy right now
could you explainwht you mean in main by

return Exit_SUCCESS;
}

Thank you for your help im going to try it later bit busy right now
could you explainwht you mean in main by

return Exit_SUCCESS;
}

Basically, it's a return type.. Just returns like "TRUE" or "FALSE" you should add it to every main/function (that isn't void)

int numbers(int number1, int number2)
{
    if(number1 > number2)
    {
      return true;
    }else{
      return false;
    }
}

if(numbers(1, 4))
{
    cout << "False";
}else{
   cout << "True";
}

Make sense? =)

1)right ok I created this i've fallen again im afraid
i have madde the class like this

#include<iostream>
#include<string>
using namespace std;
class lottery
{
public:
	lottery(int, int,int,int,int,int,int,string);
	int get_Ball_1();
	int get_Ball_2();
	int get_Ball_3();
	int get_Ball_4();
	int get_Ball_5();
	int get_Ball_6();
	int get_Bonus();
	string get_machine_Name();

	//set balls
	void set_Ball_1( int );
	void set_Ball_2( int );
	void set_Ball_3( int );
	void set_Ball_4( int );
	void set_Ball_5( int );
	void set_Ball_6( int );
	void set_Bonus( int );
	void set_Machine_Name( string );
	void lottery::display_results();
private:
	int balls[7];
	string machine_name;
};

2)I have made functions like this

#include<iostream>
#include<string>
using namespace std;
#include"lottery.h";
//constructor
lottery::lottery(int b1, int b2, int b3, int b4, int b5, int b6, int bonus, string name)
{
	set_Ball_1( b1 );
	set_Ball_2( b2 );
	set_Ball_3( b3 );
	set_Ball_4( b4 );
	set_Ball_5( b5 );
	set_Ball_6( b6 );
	//set bonus
	set_Bonus( bonus );
	//set machine name
	set_Machine_Name( name);
}//end constructor
//set methods
void lottery::set_Ball_1(int b1)
{
	balls[0]=b1;
}//end set ball 1 function

void lottery::set_Ball_2(int b2)
{
	balls[1]=b2;
}//end set ball 2 function

void lottery::set_Ball_3(int b3)
{
	balls[2]= b3;
}//end set ball 3 function
void lottery::set_Ball_4(int b4)
{
	balls[3] = b4;
}//end set ball 4 function
void lottery::set_Ball_5(int b5)
{
	balls[4] = b5;
}//end set ball 5 function
void lottery::set_Ball_6(int b6)
{
	balls[5] = b6;
}//end set ball 6 function
void lottery::set_Bonus(int b7)
{
	balls[6] = b7;
}//end set bonus function
void lottery::set_Machine_Name(string nam)
{
	machine_name = nam;
}
//get methods
int lottery::get_Ball_1 ()
{
	return balls[0];
}//end get ball 1 function
int lottery::get_Ball_2 ()
{
	return balls[1];
}//end get ball 2 function
int lottery::get_Ball_3 ()
{
	return balls[2];
}//end get ball 3 function
int lottery::get_Ball_4 ()
{
	return balls[3];
}//end get ball 4 function
int lottery::get_Ball_5 ()
{
	return balls[4];
}//end get ball 5 function
int lottery::get_Ball_6 ()
{
	return balls[5];
}//end get ball 6 function
int lottery::get_Bonus ()
{
	return balls[6];
}//end get bonus ball  function
string lottery::get_machine_Name()
{
	return machine_name;
}//end get machine name function

void lottery::display_results()
{
	cout<< get_Ball_1() <<"-"<< get_Ball_2() <<"-"<< get_Ball_3() <<"-"<< get_Ball_4() 
		<<"-"<< get_Ball_5() <<"-"<< get_Ball_6() <<"\t"<< "Bonus: "<< get_Bonus() <<endl;
	cout<<"Machine: " <<get_machine_Name() <<endl;
}//end display results

3) I have created the main function like this however it says here array function not defined I dont understand what to do

#include<iostream>
#include<string>
#include"lottery.h";
using namespace std; 
//main function
int main()
{
	//create draws
	lottery lottery_Guinevere[80]([B])[/B];error here right bracket;


}

Given the number of balls in play, it may be better to have a generalized get_Ball() function which take an index argument:

int get_Ball(int ball_no)
    {
        return balls[ball_no - 1]; 
    }

You could even write it as an operator[] function which would allow you to treat it similarly to an array.

OTOH, I'm not sure you want a set_Ball() function at all. The entire point of this is to generate the balls randomly, correct? If so, that should be a (private) operation of the Lottery machine object - setting or changing the values from outside of the lottery machine ought to be prevented. Similarly, the c'tor shouldn't have to take the generated values in at the start, because the purpose of the Lottery object is to do the generating for you.

Im sorry I just got back to this im still new to this programming type i dont fully understand this index and operator I want to create an array from my class but use the default constructor with no values, then lateer I want to set the values as classesI create in the array but it says it cant do that I dont understand.. sorry

Kunal

Member Avatar for v3ga

One of the reasons for OOP is to reuse code, avoid useless repetition. It would be much cleaner if u declare 1 function.

do you mean just do it all in a main function

Hi I think i need to justify this,
the reason im making a class is :
I'm looking to enter the last 6 months draws as objects with arrays I then want the program to look at these draws and tell me what are the top 6 numbers drawn i did this as a class of integers at the beginning but found it was not easy to reference these lster when it came to searching through the objects hence i want to create an array of objects to reference then loop through searching the objects, but in the main function it says I cant create an aray of my objects any ideas how I can overcome this, i thought it would be easier as a class as i can create objects for each draw entering the numbers and then iteratively by an algorithm search through them for the top 6 values
any help would be great
Thanks

Kunal

do you mean just do it all in a main function

No; quite the opposite, in fact. What v3ga was talking about was the getBall[i]x[/i]() functions, versus the single getBall() function I proposed. What he meant was that having six separate functions which do more or less the same operation is a bad design approach; it is better to have a single function that takes a parameter to decide which of the six you should be operating on.

i want to create an array of objects to reference then loop through searching the objects, but in the main function it says I cant create an array of my objects any ideas how I can overcome this

Odd, it certainly should allow you to create such an array. Could you post the code you had tried to use, and the exact error messages you were getting?

1)I have made my class DEFINITION like this:

#include<iostream>
#include<string>
using namespace std;
class lottery
{
public:
	lottery(int, int,int,int,int,int,int,string);
	int get_Ball_1();
	int get_Ball_2();
	int get_Ball_3();
	int get_Ball_4();
	int get_Ball_5();
	int get_Ball_6();
	int get_Bonus();
	string get_machine_Name();

	//set balls
	void set_Ball_1( int );
	void set_Ball_2( int );
	void set_Ball_3( int );
	void set_Ball_4( int );
	void set_Ball_5( int );
	void set_Ball_6( int );
	void set_Bonus( int );
	void set_Machine_Name( string );
	void lottery::display_results();
private:
	int balls[7];
	string machine_name;
};

2)I have created the class implementation like this:

#include<iostream>
#include<string>
using namespace std;
#include"lottery.h";
//constructor
lottery::lottery(int b1, int b2, int b3, int b4, int b5, int b6, int bonus, string name)
{
	set_Ball_1( b1 );
	set_Ball_2( b2 );
	set_Ball_3( b3 );
	set_Ball_4( b4 );
	set_Ball_5( b5 );
	set_Ball_6( b6 );
	//set bonus
	set_Bonus( bonus );
	//set machine name
	set_Machine_Name( name);
}//end constructor
//set methods
void lottery::set_Ball_1(int b1)
{
	balls[0]=b1;
}//end set ball 1 function

void lottery::set_Ball_2(int b2)
{
	balls[1]=b2;
}//end set ball 2 function

void lottery::set_Ball_3(int b3)
{
	balls[2]= b3;
}//end set ball 3 function
void lottery::set_Ball_4(int b4)
{
	balls[3] = b4;
}//end set ball 4 function
void lottery::set_Ball_5(int b5)
{
	balls[4] = b5;
}//end set ball 5 function
void lottery::set_Ball_6(int b6)
{
	balls[5] = b6;
}//end set ball 6 function
void lottery::set_Bonus(int b7)
{
	balls[6] = b7;
}//end set bonus function
void lottery::set_Machine_Name(string nam)
{
	machine_name = nam;
}
//get methods
int lottery::get_Ball_1 ()
{
	return balls[0];
}//end get ball 1 function
int lottery::get_Ball_2 ()
{
	return balls[1];
}//end get ball 2 function
int lottery::get_Ball_3 ()
{
	return balls[2];
}//end get ball 3 function
int lottery::get_Ball_4 ()
{
	return balls[3];
}//end get ball 4 function
int lottery::get_Ball_5 ()
{
	return balls[4];
}//end get ball 5 function
int lottery::get_Ball_6 ()
{
	return balls[5];
}//end get ball 6 function
int lottery::get_Bonus ()
{
	return balls[6];
}//end get bonus ball  function
string lottery::get_machine_Name()
{
	return machine_name;
}//end get machine name function

void lottery::display_results()
{
	cout<< get_Ball_1() <<"-"<< get_Ball_2() <<"-"<< get_Ball_3() <<"-"<< get_Ball_4() 
		<<"-"<< get_Ball_5() <<"-"<< get_Ball_6() <<"\t"<< "Bonus: "<< get_Bonus() <<endl;
	cout<<"Machine: " <<get_machine_Name() <<endl;
}//end display results

3) I have the main function like this it has an error on the right bracket in aray declaration saying error Array function ont allowed

#include<iostream>
#include<string>
#include"lottery.h";
using namespace std; 
//main function
int main()
{
	//create draws
	lottery lottery_Guinevere[80]();error here right bracket;


}#include<iostream>
#include<string>
#include"lottery.h";
using namespace std; 
//main function
int main()
{
	//create draws
	lottery lottery_Guinevere[80]();error here right bracket;
}
Member Avatar for v3ga

That is not how you should create the object it should be
lottery lottery_Guinevere[80] no need of brackets

Sorry for delay over xmas period an new years I tried the code idea the v3gamentioned it says no default constructor created but there is one one in my class
this is beginning to bug me now i thought it be simple..

If you don't declare a constructor the compiler will declare a default constructor for you. However, if you declare a constructor, then there will be no default constructor declared for you, unless you declare it for yourself. In your case your have declared a non-default constructor taking 6 ints as arguments, but you declare the object with no parameters, which requires use of a default constructor, but there is none available. Hence, the error.

So how would i set my constructor as the default constructor
thanks for the help

Kunal

You can't. You simply need to declare a constructor with no parameters/arguments.

class A
{
	public :

		A (); // constructor with no arguments
	        A ( int ); // constructor with one argument
	        .
	        .	        
	        .
	        A ( int, int, int ); // constructor with three arguments

	private :

		int value;
};

A::A () // constructor with no arguments
{
	value = 0;
}

A::A ( int p )
{
	value = p;
}

.
.
And so on, you can have as many constructors as you like.
Member Avatar for v3ga

Sorry. I didn't see your constructor had parameters. If you want to make objects without parameters when you have a parameter-taking constructor you need to overload the constructor with another that does not take any parameters

If I could make a comment on the design of your class...

I'd change the name of your class from Lottery to LotteryResult , since this is what the object actually represents. It does not represent the concept of a lottery in general. I think a Lottery class would generate LotteryResult objects, but not store them. It would probably have a bunch of other functions that don't relate to the actual result.

I think that the original idea you had was sort of correct, with respect to not having a default constructor. I would only have the constructor that takes the parameters for the result. I'd also get rid of all the "Set" methods, so that the members of the class can't be changed later in the program. Can you think of a good reason why you'd want the values of the ball numbers to change after the result has been constructed?

If you follow this course, then you will get the compilation error that you mentioned a few posts ago (when you decided that you needed a default constructor). You can avoid this error by using std::vector to hold all your results instead of an array on the stack. That is, you simply replace

lottery lottery_Guinevere[80];

with

std::vector< LotteryResult > allResults;

(you need to do #include <vector> to use std::vector . Using std::vector has a large number of advantages in this case, but one specific one is that you don't need to default construct any result objects to declare the vector. std::vector also has the advantage that you can dynamically change the size of it, so you can do something like this:

LotteryResult Result1( 1, 2, 3, 4, 5, 6, 7, "Arthur");
LotteryResult Result2( 8, 9,10,11,12,13, 14, "Guinevere");

std::vector< LotteryResult > allResults;
allResults.push_back(Result1);
allResults.push_back(Result2);

If you feel this is too verbose, you can simply do:

std::vector< LotteryResult > allResults;
allResults.push_back( LotteryResult( 1, 2, 3, 4, 5, 6, 7, "Arthur") );

I also noticed that you still have accessors of the form get_ball_2() etc. As mentioned by a bunch of other people, this really isn't the most efficient way to do this. Since you now have your ball values stored in an array (which is the correct thing to use in this case, as opposed to a std::vector ), you can take advantage of this in your code. Instead of six functions you get the different ball values, you can just use one! Like this:

/// Get the value of a ball
size_t GetBall( size_t ballIndex ) const
{
    return M_ballNumbers[ ballIndex ];
}

If you needed a "Set" version of this, you'd simply do:

/// Get the value of a ball
void SetBall( size_t ballIndex, size_t ballValue )
{
    M_ballNumbers[ ballIndex ] = ballValue;
}

Putting all this together, your program might look a bit like this:

#include <iostream>
#include <string>
#include <vector>

class LotteryResult
{
public:
    static const size_t maxBalls = 7;

    /// Constructor populates members with values
    LotteryResult( size_t b1, size_t b2, size_t b3, size_t b4, size_t b5, size_t b6, size_t bonus, const char* machineName )
        : M_machineName( machineName )
    {
        M_ballNumbers[0] = b1;
        M_ballNumbers[1] = b2;
        M_ballNumbers[2] = b3;
        M_ballNumbers[3] = b4;
        M_ballNumbers[4] = b5;
        M_ballNumbers[5] = b6;
        M_ballNumbers[6] = bonus;
    }

    /// Get the value of a ball
    size_t GetBall( size_t ballIndex ) const
    {
        return M_ballNumbers[ ballIndex ];
    }

    /// Special accessor for the bonus ball
    size_t GetBonusBall() const
    {
        // The bonus ball is always the last ball in the list
        return M_ballNumbers[ maxBalls - 1 ];
    }

    /// Get the name of the draw machine
    const std::string& GetMachineName() const
    {
        return M_machineName;
    }

    /// Print results to stdout
    void DisplayResult() const
    {
        for ( size_t i = 0; i < maxBalls - 2; ++i )
            std::cout << GetBall( i ) << "-";
        std::cout << GetBall( maxBalls - 2 ) << ", bonus " << GetBonusBall() << ", Machine: " << GetMachineName() << std::endl;
    }

private:
    size_t M_ballNumbers[7];
    std::string M_machineName;
};

int main()
{
    LotteryResult Result1( 1, 2, 3, 4, 5, 6, 7, "Arthur");
    LotteryResult Result2( 8, 9,10,11,12,13, 14, "Guinevere");

    std::vector< LotteryResult > allResults;
    allResults.push_back(Result1);
    allResults.push_back(Result2);
    allResults.push_back( LotteryResult( 15,16,17,18,19,20,21,"Arthur" ) );

    for ( size_t i = 0; i < allResults.size(); ++i )
        allResults[i].DisplayResult();

    return 0;
}

Hope that helps.

im sorry but all this talk of vectors which from my understanding is an array within an array has confused im sorry your going to have to break it simplier for me sorry
I created a default constructor with no parameters like this

lottery()
{
set_Ball_1(0);
set_Ball_2(0);
set_Ball_3(0);
set_Ball_4(0);
set_Ball_5(0);
set_Ball_6(0);
set_Bonus(0);
}

I edited the class and removed the string so now no machine name, its just 7 integers but now I created an array of values 0 with a default constructor for all draws but i cant use the other constructor to set the values as it says term does not evaluate to a function to take 7 integers hpwever I managed to get it to work with the individual setball methods and using parameters but i wanted to use the class constructor can someone help again sorry to maje it easier to add old results.

Thanks as always

Kunal

in case i wasnt clear my class definition is now

#include<iostream>
#include<string>
using namespace std;
class lottery
{
public:
	lottery();
	lottery(int, int,int,int,int,int,int);
	int get_Ball_1();
	int get_Ball_2();
	int get_Ball_3();
	int get_Ball_4();
	int get_Ball_5();
	int get_Ball_6();
	int get_Bonus();
	//set balls
	void set_Ball_1( int );
	void set_Ball_2( int );
	void set_Ball_3( int );
	void set_Ball_4( int );
	void set_Ball_5( int );
	void set_Ball_6( int );
	void set_Bonus( int );
	void lottery::display_results();
private:
	int balls[7];
};

im sorry but all this talk of vectors which from my understanding is an array within an array has confused im sorry your going to have to break it simplier for me sorry

Sorry to confuse you! Don't worry about the vector stuff, there is lots of stuff on the web about std::vector , so you can easily read about this and other STL containers when you feel like it. I would encourage you to do this sooner rather than later, as they can simplify your programming efforts greatly. Just a note though, it's not correct to say that std::vector is an "array within an array". Really, std::vector is just an array, but with several useful properties, like you can query the size of it, dynamically re-size it and it manages all its own memory etc.

I edited the class and removed the string so now no machine name, its just 7 integers but now I created an array of values 0 with a default constructor for all draws but i cant use the other constructor to set the values as it says term does not evaluate to a function to take 7 integers

You can't really use a constructor like I think you're trying to, it just gets called when you declare a variable of a given type (a Lottery variable in your case), you can't use a constructor to set values in things after the instantiation of an object.

I managed to get it to work with the individual setball methods and using parameters but i wanted to use the class constructor can someone help again sorry to maje it easier to add old results.

Yes, look at the post I made before, It shows how you can do this. Ignore the vector talk for now, just read "array" where you see "vector".

A final note on your class definition, it would be better if it looked like this instead:

class lottery
{
public:
	lottery();
	lottery(int, int,int,int,int,int,int);
	int get_Ball( int ballIndex );
	int get_Bonus();

	//set balls
	void set_Ball( int ballIndex, int ballValue );
	void set_Bonus( int );
	void display_results();
private:
	int balls[7];
};

This interface would be a lot easier to read and is a lot more flexible. If you want to set all the balls in one go after construction, you could simply define a member function to do it for you:

void SetBalls( int* values )
{
   for ( int i = 0; i < 7; ++i )
      set_Ball( i, values[i] );
}

This is actually quite a dangerous thing to do, because it's easy to pass an array to the function with the wrong size. You can avoid this problem using vectors, but maybe we'll leave that for now. Anyway, using this function, you could do:

Lottery myLotteryResult;
int numbers[] = { 1, 2, 5, 7, 12, 45, 4 };
myLotteryResult.SetBalls( numbers );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.