Thread: Beginner!

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    13

    Beginner!

    I had to write a simple program using functions in C style now I need to re-write my current program using classes in C++ style. I have no idea where to begin, my code is only like 100 lines though so hopefully it won't be too hard.

    Is it difficult changing it to classes? Any advice?

    Thanks alot

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If I factor in the fact that you are probably a beginner and as thus do not write any extremely complicated code, I am going to make an educated guess by saying it will probably be easy. I can be wrong since there is no way to be 100% sure by guessing.

    Anyway, what you should do is probably just take all the functions and them inside a class.
    Then take all global variables and make them member variables.
    Then sort out which functions should be public and which protected/private.

    If you want more concrete advice, you need to post some code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Quote Originally Posted by Elysia View Post
    If I factor in the fact that you are probably a beginner and as thus do not write any extremely complicated code, I am going to make an educated guess by saying it will probably be easy. I can be wrong since there is no way to be 100% sure by guessing.

    Anyway, what you should do is probably just take all the functions and them inside a class.
    Then take all global variables and make them member variables.
    Then sort out which functions should be public and which protected/private.

    If you want more concrete advice, you need to post some code.
    " it will probably be easy" haha! not so sure :-).

    Thanks for the suggestions, any chance I could send you my code then? Just so you could see whether it's going to be possible or I might need to re-write?

    Thanks alot!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Post the code on the forum.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Basically a game of 301 darts and both players need to get to 0.

    Code:
    #include <iostream>#include <ctime>
    
    
    int BullFunction(int percent) // throw dart function
    {
    	int r = rand()%100;
    	if(r<percent) // hit
    	return 50; // return 50 for bullseye hit to my while loop
    	else // miss
    	return 1 + rand()%20; // return random number between 1 and 20
    }
    
    
    int SingleFunction(int target)
    {
    // return single or a neighbour
    	if (target>20)
    		{
    		target = 20;
    		}
    
    
    	int board[22] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5, 20, 1}; // darts board
    	int r = rand()%100;
    	int i;
    	if(r<80) // hit
    	return target;
    	else { // miss
    	//to find neighbours search for single in board
    	i = 0;
    	do {
    	i++;
    	} while(board[i] != target);
    	// now return left or right neighbour, each equally likely
    	if(rand()%2==0)
    	return board[i-1];
    	else
    	return board[i+1];
    		}
    }
    
    
    int main()
    {
    
    
    	srand((unsigned)time(0)); //change rand each time
    
    
    	int score = 301; // start score
    	int throws = 0; // start throws
    	int sidthrows = 0; //start sids throws
    
    
    	int i = 0;
    
    
    	int coin;
    
    
    	int wins = 0;
    	int sidwins = 0;
    
    
    	int flipjoewins = 0;
    	int flipsidwins = 0;
    
    
    	//COUNTERS
    
    
    	int counts[200]; // loop counter
    	for (int i = 0; i < 200; i++) counts[i] = 0; // initialise
    
    
    	int countsidthrows[200]; // loop counter
    	for (int i=0; i < 200; i++) countsidthrows[i] = 0; // initialise
    
    
    	int countwins [2];
    	for (int i = 0; i < 2; i++) countwins[i] = 0;
    
    
    	int countsidwins [2];
    	for (int i = 0; i < 2; i++) countsidwins[i] = 0;
    
    
    	int countflipjoewins [2];
    	for (int i = 0; i < 2; i++) countflipjoewins[i] = 0;
    
    
    	int countflipsidwins [2];
    	for (int i = 0; i < 2; i++) countflipsidwins[i] = 0;
    
    
    	for (i = 0; i < 1000; i++)
    {
    	// JOE
    	score = 301;
    	throws = 0;
    
    
    	while (score >= 100) // while loop will continue until 100 is greater than my score
    	{
    		throws++; score = score - BullFunction(70); // add 1 to throws; score = score - ThrowDart(); takes off whatever Throwdart(); function returns
    	}
    
    
    	while (score <= 99 && score > 50) // while loop will continue until my score is between 100 and 50
    	{
    		throws++; score = score - SingleFunction(score - 50);
    		if (score < 50 && score > 1)
    			{
    			throws++; 
    			score = 50;
    			}
    	}
    
    
    	while (score == 50) // while loop will continue until 0 is greater than my score
    	{
    		throws++; score = score - BullFunction(70);
    		if (score < 50 && score > 1)
    			{
    			throws++; 
    			score = 50;
    			}
    	}
    
    
    	//SID
    	score = 301;
    	sidthrows = 0;
    
    
    	while (score >= 100) // while loop will continue until 100 is greater than my score
    	{
    		sidthrows++; score = score - BullFunction(72); // add 1 to throws; score = score - ThrowDart(); takes off whatever Throwdart(); function returns
    	}
    
    
    	while (score <= 99 && score > 50) // while loop will continue until my score is between 100 and 50
    	{
    		sidthrows++; score = score - SingleFunction(score - 50);
    		if (score < 50 && score > 1)
    			{
    			sidthrows++; 
    			score = 50;
    			}
    	}
    
    
    	while (score == 50) // while loop will continue until 0 is greater than my score
    	{
    		sidthrows++; score = score - BullFunction(72);
    		if (score < 50 && score > 1)
    			{
    			sidthrows++; 
    			score = 50;
    			}
    	}
    
    
    	if (throws < sidthrows) 
    		wins = 1 ;
    	else 
    		wins = 0 ;
    
    
    	if (sidthrows < throws) 
    		sidwins = 1 ;
    	else 
    		sidwins = 0 ;
    
    
    	coin = 1 + rand()%2;
    		
    	switch(coin) {		
    		if (coin == 1)
    		{
    	case 1:
    			if (throws < sidthrows)
    			flipjoewins = 1 ;
    			if (throws > sidthrows)
    			flipjoewins = 0 ;
    		}
    
    
    		if (coin == 2)
    		{
    			case 2:
    			if (sidthrows < throws)
    			flipsidwins = 1 ;
    			if (sidthrows > throws)
    			flipsidwins = 0 ;
    		}
    	
    	}
    	
    	counts [throws]++;
    	countsidthrows [sidthrows]++;
    	countwins [wins]++;
    	countsidwins [sidwins]++;
    	countflipjoewins [flipjoewins]++;
    	countflipsidwins [flipsidwins]++;
    }
    
    
    for (i=7; i<16; i++)
    {
    std::cout<<"\nNumber of games ending in "<< i << " throws: "<<(counts[i]*100)/1000 << "%";
    }
    std::cout<<"\nAverage number of throws to win is:" << (countsidthrows[i] + counts[i])*0+9<<std::endl;
    
    
    
    
    //joe first
    std::cout<<"\n\nJoe throwing first wins out of 1000 games: "<<(countwins[1]*100)/1000 << "%" <<std::endl;
    //sid first
    std::cout<<"\nSid throwing first wins out of 1000 games: "<<(countsidwins[1]*100)/1000 << "%" <<std::endl;
    
    
    //flipping
    std::cout<<"\n\nFlipping to see who goes first with Joe throwing first wins out of 1000: "<<(countflipjoewins[1]*100)/1000 << "%" <<std::endl;
    //sid first
    std::cout<<"\nFlipping to see who goes first with Sid throwing first wins out of 1000: "<<(countflipsidwins[1]*100)/1000 << "% \n" <<std::endl;
    
    
    return 0;
    }

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You have two classes, Game and Player. Game might end up including two Player objects. Both your functions belong in Game. All of your variables belong in one class or the other. Probably the only things you'll have to initialize in main() will be a Game and two Players (if they are not part of a Game object).

    IMO, the Game should include the Players. This will mean you have very little code left in main -- mostly, just the cout stuff. Don't put any of that in a class. Instead, have the class methods return results, then you report them in main() via cout.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    So I don't need to re-write all my code? Just re-position it?

    Okay I did what you said and created a class game, in my main I put my counters. Having some problems with he players though, I've got it to run and it just shows 0% for everything.

    Thanks
    Last edited by Valdo; 03-26-2012 at 03:00 PM.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Valdo View Post
    So I don't need to re-write all my code? Just re-position it?
    Well, you may have to change some stuff but you don't have to start from scratch with the mechanics and everything no.

    Okay I did what you said and created a class game, in my main I put my counters. Having some problems with he players though, I've got it to run and it just shows 0% for everything.
    Keep posting your code and being specific about your problems.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Thanks alot for helping me!

    Here's my classes attempt:

    Code:
    #include <iostream>#include <ctime>
    
    
    class dartist{
    public:
    	int skill;
    
    
    	int Bull(int skill) // throw dart function
    	{
    		int r = rand()%100;
    		if(r<skill) // hit
    		return 50; // return 50 for bullseye hit to my while loop
    		else // miss
    		return 1 + rand()%20; // return random number between 1 and 20
    	}
    
    
    	int Single(int target)
    	{
    		if (target>20)
    			{
    			target = 20;
    			}
    
    
    		int board[22] = {20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5, 20, 1}; // darts board
    		int r = rand()%100;
    		int i;
    		if(r<80) // hit
    		return target;
    		else { // miss
    		//to find neighbours search for single in board
    		i = 0;
    		do {
    		i++;
    		} while(board[i] != target);
    		// now return left or right neighbour, each equally likely
    		if(rand()%2==0)
    		return board[i-1];
    		else
    		return board[i+1];
    			}
    	}
    
    
    		int darts(){
    
    
    			srand((unsigned)time(0)); //change rand each time
    
    
    			int score = 301; // start score
    			int throws = 0; // start throws
    			int sidthrows = 0; //start sids throws
    			int i = 0;
    
    
    			int coin;
    
    
    			int wins = 0;
    			int sidwins = 0;
    
    
    			int flipjoewins = 0;
    			int flipsidwins = 0;
    
    
    			//COUNTERS
    
    
    			int counts[200]; // loop counter
    			for (int i = 0; i < 200; i++) counts[i] = 0; // initialise
    
    
    			int countsidthrows[200]; // loop counter
    			for (int i=0; i < 200; i++) countsidthrows[i] = 0; // initialise
    
    
    			int countwins [2];
    			for (int i = 0; i < 2; i++) countwins[i] = 0;
    
    
    			int countsidwins [2];
    			for (int i = 0; i < 2; i++) countsidwins[i] = 0;
    
    
    			int countflipjoewins [2];
    			for (int i = 0; i < 2; i++) countflipjoewins[i] = 0;
    	
    			int countflipsidwins [2];
    			for (int i = 0; i < 2; i++) countflipsidwins[i] = 0;
    
    
    	for (i = 0; i < 1000; i++)
    		{
    			score = 301;
    			throws = 0;
    
    
    			//JOE
    
    
    			while (score >= 100) // while loop will continue until 100 is greater than my score
    			{
    				throws++; score = score - Bull(skill); // add 1 to throws; score = score - ThrowDart(); takes off whatever Throwdart(); function returns
    			}
    
    
    			while (score <= 99 && score > 50) // while loop will continue until my score is between 100 and 50
    			{
    				throws++; score = score - Single(score - 50);
    				if (score < 50 && score > 1)
    					{
    					throws++; 
    					score = 50;
    					}
    			}
    
    
    			while (score == 50) // while loop will continue until 0 is greater than my score
    			{
    				throws++; score = score - Bull(skill);
    				if (score < 50 && score > 1)
    					{
    					throws++; 
    					score = 50;
    					}
    			}
    
    
    			//SID
    			score = 301;
    			sidthrows = 0;
    
    
    			while (score >= 100) // while loop will continue until 100 is greater than my score
    			{
    				sidthrows++; score = score - Bull(skill); // add 1 to throws; score = score - ThrowDart(); takes off whatever Throwdart(); function returns
    			}
    
    
    			while (score <= 99 && score > 50) // while loop will continue until my score is between 100 and 50
    			{
    				sidthrows++; score = score - Single(score - 50);
    				if (score < 50 && score > 1)
    					{
    					sidthrows++; 
    					score = 50;
    					}
    			}
    
    
    			while (score == 50) // while loop will continue until 0 is greater than my score
    			{
    				sidthrows++; score = score - Bull(skill);
    				if (score < 50 && score > 1)
    					{
    					sidthrows++; 
    					score = 50;
    					}
    			}
    
    
    			if (throws < sidthrows) 
    				wins = 1 ;
    			else 
    				wins = 0 ;
    
    
    			if (sidthrows < throws) 
    				sidwins = 1 ;
    			else 
    				sidwins = 0 ;
    
    
    			coin = 1 + rand()%2;
    		
    			switch(coin) {		
    			if (coin == 1)
    			{
    				case 1:
    				if (throws < sidthrows)
    				flipjoewins = 1 ;
    				if (throws > sidthrows)
    				flipjoewins = 0 ;
    			}
    
    
    			if (coin == 2)
    			{
    				case 2:
    				if (sidthrows < throws)
    				flipsidwins = 1 ;
    				if (sidthrows > throws)
    				flipsidwins = 0 ;
    			}
    			}
    		}
    
    
    		counts [throws]++;
    		countsidthrows [sidthrows]++;
    		countwins [wins]++;
    		countsidwins [sidwins]++;
    		countflipjoewins [flipjoewins]++;
    		countflipsidwins [flipsidwins]++;
    	
    		return 0;
    	}
    };
    
    
    void main(){
    			dartist joe;
    			dartist sid;
    			int i = 0;
    			int counts[200]; // loop counter
    			for (int i = 0; i < 200; i++) counts[i] = 0; // initialise
    
    
    			int countsidthrows[200]; // loop counter
    			for (int i=0; i < 200; i++) countsidthrows[i] = 0; // initialise
    
    
    			int countwins [2];
    			for (int i = 0; i < 2; i++) countwins[i] = 0;
    
    
    			int countsidwins [2];
    			for (int i = 0; i < 2; i++) countsidwins[i] = 0;
    
    
    			int countflipjoewins [2];
    			for (int i = 0; i < 2; i++) countflipjoewins[i] = 0;
    	
    			int countflipsidwins [2];
    			for (int i = 0; i < 2; i++) countflipsidwins[i] = 0;
    
    
    			for (i=7; i<16; i++)
    			{
    			std::cout<<"\nNumber of games ending in "<< i << " throws: "<<(counts[i]*100)/1000 << "%";
    			}
    			std::cout<<"\nAverage number of throws to win is:" << (countsidthrows[i] + counts[i])*0+9<<std::endl;
    
    
    
    
    			//joe first
    			std::cout<<"\n\nJoe throwing first wins out of 1000 games: "<<(countwins[1]*100)/1000 << "%" <<std::endl;
    			//sid first
    			std::cout<<"\nSid throwing first wins out of 1000 games: "<<(countsidwins[1]*100)/1000 << "%" <<std::endl;
    
    
    			//flipping
    			std::cout<<"\n\nFlipping to see who goes first with Joe throwing first wins out of 1000: "<<(countflipjoewins[1]*100)/1000 << "%" <<std::endl;
    			//sid first
    			std::cout<<"\nFlipping to see who goes first with Sid throwing first wins out of 1000: "<<(countflipsidwins[1]*100)/1000 << "% \n" <<std::endl;
    
    
    			joe.skill = 70;
    			sid.skill = 72;
    }

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There's a few easier ways to initialize an array to all zeros. This will do it:

    Code:
    int counts[200] = { 0 };
    The curly braces are actually for initializing each element, eg:

    Code:
    int array[3] = { 1000, 2000, 3000};
    However, if you don't include all the elements, the rest are set to zero. So if you just set the first one to zero in braces, all the rest will be too.

    The return type of main() is int, not void.

    Now think about how you can incorporate the code in main, which is a game between two dartist objects, into a class, "dartGame". You could use an array or vector for the players, meaning there could be any number of them, but if it is easier for you, just use two, at least until you get everything working properly.

    I'm not really sure what you are doing in that last for loop.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    13
    Right I'll try adding a new class, and change the main() to int... Using an array or vectors for the player is hard for me I'm just learning that. I just need it working for two players. And the last loop is so that it says 7 throws 8 throws 9 throws, etc.

    Thanks again!

    Quote Originally Posted by MK27 View Post
    There's a few easier ways to initialize an array to all zeros. This will do it:

    Code:
    int counts[200] = { 0 };
    The curly braces are actually for initializing each element, eg:

    Code:
    int array[3] = { 1000, 2000, 3000};
    However, if you don't include all the elements, the rest are set to zero. So if you just set the first one to zero in braces, all the rest will be too.

    The return type of main() is int, not void.

    Now think about how you can incorporate the code in main, which is a game between two dartist objects, into a class, "dartGame". You could use an array or vector for the players, meaning there could be any number of them, but if it is easier for you, just use two, at least until you get everything working properly.

    I'm not really sure what you are doing in that last for loop.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Wouldn't an expert "dartist" (aka, darter) aim for triple twenty (60 points), not the bullseye (50 points)?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner help
    By mouse666666 in forum C Programming
    Replies: 2
    Last Post: 07-27-2011, 12:26 AM
  2. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  3. beginner ?
    By braincrash in forum C Programming
    Replies: 2
    Last Post: 02-18-2003, 03:33 AM
  4. C++ beginner
    By fishcrazzy2000 in forum C++ Programming
    Replies: 2
    Last Post: 08-18-2002, 10:49 PM