Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

-=I am lost in putting together functions in main()=-

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Here is something neat that i wanna to try to make:

intro:
The game of Mastermid is played as follow: One player (the code maker) chooses a secret arrangement of colored pegs

and the other player (the code breaker) tried to guess it. The code breakers put forth an arrangement of colored

pegs, and the code maker reports two number:
1. The number of pegs that are correct color and in the correct postion.
2. The number of pegs that are the correct color regardless of whether they are in the correct postion



Task:
Write a program that plays the part of the code maker in this game. The code will consist of an arrangement of 3

"pegs" each with a "color" from 1 to 5. The program output may look similar to:


Guess 1:
First peg: 3
Second peg: 4
Third peg: 2
You have 1 correct peg(s) and 2 correct color(s)
Guess 2:
First peg: 3
Second peg: 4
Third peg: 5
You have 1 correct peg(s) and 3 correct color(s)
Guess 3:
First peg: 3
Second peg: 5
Third peg: 4
You have 3 correct peg(s) and 3 correct color(s)
You have borken the code in 3 guesses



here is my code:
Code:
#include <iostream.h>
#include <stdlib.h>
//----------------------------------------------------------------------
void GeneratePegColor (int &peg1, int &peg2, int &peg3);
void UserGuess (int guess1, int guess2, int guess3);
int  ReturnNumberOfCorrectPeg (int Peg1, int Peg2, int Peg3,int guess1, int guess2, int guess3);
int  ReturnNumberOfCorrectPegColor (int Peg1, int Peg2, int Peg3,int guess1, int guess2, int guess3);

//----------------------------------------------------------------------
int main()
{
int GuessTaken = 1;
GeneratePegColor(peg1, peg2, peg3);
do{
	cout << &quot;Guess &quot; << GuessTaken;
   UserGuess(guess1, guess2, guess3);
   cout << &quot;You have &quot; << ReturnNumberOfCorrectPeg () << &quot; peg(s)&quot; << ReturnNumberOfCorrectPegColor(); << &quot; color(s)&quot;;
	GuessTaken++;
while (CorrectPosition < 3);
	cout << &quot;You have broken the the code in &quot; << GuessTaken << &quot; guesses&quot;;
return(0);
}

//-----------------------------------------------------------------------
void GeneratePegColor (int &peg1, int &peg2, int &peg3)
/* Generate random peg colors */
{
do{
	peg1 = random(5)+1;
	peg2 = random(5)+1;
} while (peg1==peg2);
do {
	peg3 = random(5)+1;
} while ((peg3==peg1)||(peg3==peg2));
}

//-----------------------------------------------------------------------
void UserGuess (int guess1, int guess2, int guess3)
/* Display number of Guess taken, and ask the user to enter the guesses */
{
cout << &quot;\tFirst peg (1-5): &quot;;
cin >> guess1;
cout << &quot;\tSecond peg (1-5): &quot;;
cin >> guess2;
cout << &quot;\tThird peg (1-5): &quot;;
cin >> guess3;

while (guess1==guess2||guess1==guess3||guess3==guess2){
	cout << &quot;**WARNING**: Please enter three DIFFERENT colors again.&quot;<<endl;
	cout << &quot;\tFirst peg (1-5): &quot;;
	cin >> guess1;
	cout << &quot;\tSecond peg (1-5): &quot;;
	cin >> guess2;
	cout << &quot;\tThird peg (1-5): &quot;;
	cin >> guess3;
}
}
//-----------------------------------------------------------------------
int  ReturnNumberOfCorrectPeg (int Peg1, int Peg2, int Peg3,int guess1, int guess2, int guess3, int %CorrectPosition)
// Decide number of correct position
{
int CorrectPosition = 0;
if (guess1==peg1){
	CorrectPosition++;
}
else if (guess2==peg2){
	CorrectPosition++;
}
else if (guess3==peg3){
	CorrectPosition++;
}
return (CorrectPosition);

//-----------------------------------------------------------------------
int  ReturnNumberOfCorrectPegColor (int peg1, int peg2, int peg3,int guess1, int guess2, int guess3, int &CorrectColor++)
// Decide number of correct peg color regardless of wheter they are in correct position
{
int CorrectColor = 0;
if ((guess1==peg1)||(guess1==peg2)||(guess1==peg3)){
	CorrectColor++;
}
else if ((guess2==peg1)||(guess2==peg2)||(guess2==peg3)){
	CorrectColor++;
}
else if ((guess3==peg1)||(guess3==peg2)||(guess3==peg3)){
	CorrectColor++;
}
return(CorrectColor);
}

//-----------------------------------------------------------------------




My problem is that i couldn't get each function to communicate with each other in main() WITH PROPER VARAIBLES. I compiled my code, and most errors occured in main(), but I really don't know how to fix those error. This my first doing functions without any programming background.


I hope you guys/girls will help me &quot;fix the codes&quot; for me and tell me the spots where i got messed up. This code is driving me crazy, but i am willing to learn.


100 thanx to whoever took the time helping me out:bowdown: :bowdown: :bowdown:
 
You have quite a few errors in your code. I fixed the main() for you. and let you figure out the left.

int main()
{
int GuessTaken = 1;
int peg1,peg2,peg3;
int guess1,guess2,guess3;
int CorrectPosition;
GeneratePegColor(peg1, peg2, peg3);
do{
cout << &quot;Guess &quot; << GuessTaken;
UserGuess(guess1, guess2, guess3);
cout << &quot;You have &quot; << ReturnNumberOfCorrectPeg (peg1,peg2,peg3,guess1,guess2,guess3);
cout << &quot; peg(s)&quot; << ReturnNumberOfCorrectPegColor(peg1,peg2,peg3,guess1,guess2,guess3) << &quot; color(s)&quot;;
GuessTaken++;
}while (CorrectPosition < 3);
cout << &quot;You have broken the the code in &quot; << GuessTaken << &quot; guesses&quot;;
return(0);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top