Can't get loop to work or validation

This is a discussion on Can't get loop to work or validation within the C++ Programming forums, part of the General Programming Boards category; I need to make the game Mastermind for my final exam. I have everything working except I can't get the ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    5

    Question Can't get loop to work or validation

    I need to make the game Mastermind for my final exam. I have everything working except I can't get the user inputed guesses to validate and loop if incorrect. Every time I try to put a loop in the program in the getGuess() function it won't loop. Valid choices are W, O, Y, R, B, G.

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    int setCode(void);
    char getGuess(void);
    void gameOver(void);
    void title(void);
    void gameDirections(void);
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f);
    int inexactMatches(char x, char y, char a, char b, char c, char d,
    				   char e, char f);
    void printSequence(char c1, char c2, char c3, char c4);
    int num, exact_count, inexact_counter, counter, count;
    int X_exact_count,A_exact_count,C_exact_count,E_exact_count;
    char code1, code2, code3, code4, color, entered_code1;
    char entered_code2, entered_code3, entered_code4, guess;
    
    int main(void)
    {
    	
    	counter = 0;
    	title();
    	gameDirections();
    	system("PAUSE");
    	system("CLS");
    	srand((long)time(NULL));
    	code1 = setCode();
    	code2 = setCode();
    	code3 = setCode();
    	code4 = setCode();
    	
    	do{
    		
    		cout << "Please enter color sequence:  ";
    		entered_code1 = getGuess();
    		entered_code2 = getGuess();
    		entered_code3 = getGuess();
    		entered_code4 = getGuess();
    		exactMatches(code1, entered_code1, code2, entered_code2,
    					 code3, entered_code3, code4, entered_code4);
    		inexactMatches(code1, entered_code1, code2, entered_code2,
    					   code3, entered_code3, code4, entered_code4);
    		if(exact_count==4)
    		{
    			gameOver();
    			return 0;
    		}
    		counter++;
    	}while(counter <8);
    	gameOver();
    	return 0;
    }
    
    int setCode(void)
    {
    		num = 1+rand()%6;
    		
    		if(num==1)
    			color = 'W';
    		else if(num==2)
    			color = 'R';
    		else if(num==3)
    			color = 'B';
    		else if(num==4)
    			color = 'G';
    		else if(num==5)
    			color = 'O';
    		else if(num==6)
    			color = 'Y';
    
    	return (color);
    }
    char getGuess(void)
    {
    	cin >> guess;
    	
    	return (guess);
    }
    
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f)
    {
    	X_exact_count = 0,A_exact_count = 0,C_exact_count = 0,E_exact_count = 0;
    
    	if(x==y)
    		X_exact_count++;
    	if(a==b)
    		A_exact_count++;
    	if(c==d)
    		C_exact_count++;
    	if(e==f)
    		E_exact_count++;
    
    	exact_count=X_exact_count+A_exact_count+C_exact_count+E_exact_count;
    	cout << exact_count << " exact matche(s)\n";
    	return (exact_count,X_exact_count,A_exact_count,C_exact_count,E_exact_count);
    }
    int inexactMatches(char x, char y, char a, char b, char c, char d,
    				   char e, char f)
    {
    	int Y_count=0, B_count=0, D_count=0, F_count=0;
    
    	if(y==a||y==c||y==e&&y!=x)
    		Y_count++;
    	if(b==x||b==c||b==e&&b!=a)
    		B_count++;
    	if(d==a||d==x||d==e&&d!=c)
    		D_count++;
    	if(f==a||f==c||f==x&&f!=e)
    		F_count++;
    
    	if(Y_count>1)
    		Y_count=1;
    		if(X_exact_count==1)
    			Y_count=0;
    	if(B_count>1)
    		B_count=1;
    		if(A_exact_count==1)
    			B_count=0;
    	if(D_count>1)
    		D_count=1;
    		if(C_exact_count==1)
    			D_count=0;
    	if(F_count>1)
    		F_count=1;
    		if(E_exact_count==1)
    			F_count=0;
    
    	count =Y_count+B_count+D_count+F_count;
    	cout << count << " inexact matche(s)\n";
    	return (count);
    }
    void printSequence(char c1, char c2, char c3, char c4)
    {
    	cout << c1 << " "<< c2 << " " << c3<< " " << c4;
    	return;
    }
    
    void gameOver(void)
    {
    	system("CLS");
    	title();
    	cout << "Thanks for playing.\n";
    	cout << "The correct sequence was ";
    	printSequence(code1, code2, code3, code4);
    	cout << ". "<<endl;
    	return;
    }
    
    void title(void)
    {
    	cout << "()()()()()()()()()()()()()()()\n";
    	cout << "()()()()()MASTERMI";
    	cout << "ND()()()()()\n";
    	cout << "()()()()()()()()()()()()()()()\n\n";
    	return;
    }
    
    void gameDirections(void)
    {
    	cout << "Directions:  A random code of four color pegs will\n";
    	cout << "be generate by the computer.  Each peg is chosen\n";
    	cout << "from one of six colored pegs:  White (W), Red (R),\n";
    	cout << "Blue (B), Green (G), Orange (O), and Yellow (Y).\n";
    	cout << "A player may choose duplicate colors when creating\n";
    	cout << "his/her sequence.  The goal of the game is to guess\n";
    	cout << "the random code generate by the computer in 8 or\n";
    	cout << "less guesses.  If the player cannot accomplish this\n";
    	cout << "task, the game will end, and the code will be shown.\n";
    	cout << "You may only use W, R, B, G, O, or Y when entering\n";
    	cout << "your selection.\n\n\n";
    	return;
    }
    And here is what I had in the function when it was not working:

    Code:
    do{
    	cin << guess;
    	(return guess);
    }while((guess!='W')||(guess!='R')||(guess!='B')||(guess!='G')||(guess!='O')||(guess!='Y'));
    I would like to get the program to ask the user for a new guess when it is invalid. Any help is appreciated. I am trying to get this finished by Monday morning. It is due Tuesday.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,164
    Well, you're returning the guess directly instead of checking it. Move the return outside the loop.
    And btw, there's no need for (void) in the argument list in C++, you can remove it -> int main(void) can be int main().
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Here is what I have and still no luck

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    int setCode(void);
    char getGuess(void);
    void gameOver(void);
    void title(void);
    void gameDirections(void);
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f);
    int inexactMatches(char x, char y, char a, char b, char c, char d,
    				   char e, char f);
    void printSequence(char c1, char c2, char c3, char c4);
    int num, exact_count, inexact_counter, counter, count;
    int X_exact_count,A_exact_count,C_exact_count,E_exact_count;
    char code1, code2, code3, code4, color, entered_code1;
    char entered_code2, entered_code3, entered_code4, guess;
    
    int main()
    {
    	
    	counter = 0;
    	title();
    	gameDirections();
    	system("PAUSE");
    	system("CLS");
    	srand((long)time(NULL));
    	code1 = setCode();
    	code2 = setCode();
    	code3 = setCode();
    	code4 = setCode();
    	printSequence(code1, code2, code3, code4);
    	
    	do{
    		
    		cout << "Please enter color sequence:  ";
    		entered_code1 = getGuess();
    		entered_code2 = getGuess();
    		entered_code3 = getGuess();
    		entered_code4 = getGuess();
    		exactMatches(code1, entered_code1, code2, entered_code2,
    					 code3, entered_code3, code4, entered_code4);
    		inexactMatches(code1, entered_code1, code2, entered_code2,
    					   code3, entered_code3, code4, entered_code4);
    		if(exact_count==4)
    		{
    			gameOver();
    			return 0;
    		}
    		counter++;
    	}while(counter <8);
    	gameOver();
    	return 0;
    }
    
    int setCode(void)
    {
    		num = 1+rand()%6;
    		
    		if(num==1)
    			color = 'W';
    		else if(num==2)
    			color = 'R';
    		else if(num==3)
    			color = 'B';
    		else if(num==4)
    			color = 'G';
    		else if(num==5)
    			color = 'O';
    		else if(num==6)
    			color = 'Y';
    
    	return (color);
    }
    char getGuess(void)
    {
    	do{
    		cin >> guess;
    	}while((guess!='W')||(guess!='R')||(guess!='B')||(guess!='G')||(guess!='O')||(guess!='Y'));
    	return (guess);
    }
    
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f)
    {
    	X_exact_count = 0,A_exact_count = 0,C_exact_count = 0,E_exact_count = 0;
    
    	if(x==y)
    		X_exact_count++;
    	if(a==b)
    		A_exact_count++;
    	if(c==d)
    		C_exact_count++;
    	if(e==f)
    		E_exact_count++;
    
    	exact_count=X_exact_count+A_exact_count+C_exact_count+E_exact_count;
    	cout << exact_count << " exact matche(s)\n";
    	return (exact_count,X_exact_count,A_exact_count,C_exact_count,E_exact_count);
    }
    int inexactMatches(char x, char y, char a, char b, char c, char d,
    				   char e, char f)
    {
    	int Y_count=0, B_count=0, D_count=0, F_count=0;
    
    	if(y==a||y==c||y==e&&y!=x)
    		Y_count++;
    	if(b==x||b==c||b==e&&b!=a)
    		B_count++;
    	if(d==a||d==x||d==e&&d!=c)
    		D_count++;
    	if(f==a||f==c||f==x&&f!=e)
    		F_count++;
    
    	if(Y_count>1)
    		Y_count=1;
    		if(X_exact_count==1)
    			Y_count=0;
    	if(B_count>1)
    		B_count=1;
    		if(A_exact_count==1)
    			B_count=0;
    	if(D_count>1)
    		D_count=1;
    		if(C_exact_count==1)
    			D_count=0;
    	if(F_count>1)
    		F_count=1;
    		if(E_exact_count==1)
    			F_count=0;
    
    	count =Y_count+B_count+D_count+F_count;
    	cout << count << " inexact matche(s)\n";
    	return (count);
    }
    void printSequence(char c1, char c2, char c3, char c4)
    {
    	cout << c1 << " "<< c2 << " " << c3<< " " << c4;
    	return;
    }
    
    void gameOver(void)
    {
    	system("CLS");
    	title();
    	cout << "Thanks for playing.\n";
    	cout << "The correct sequence was ";
    	printSequence(code1, code2, code3, code4);
    	cout << ". "<<endl;
    	return;
    }
    
    void title(void)
    {
    	cout << "()()()()()()()()()()()()()()()\n";
    	cout << "()()()()()MASTERMI";
    	cout << "ND()()()()()\n";
    	cout << "()()()()()()()()()()()()()()()\n\n";
    	return;
    }
    
    void gameDirections(void)
    {
    	cout << "Directions:  A random code of four color pegs will\n";
    	cout << "be generate by the computer.  Each peg is chosen\n";
    	cout << "from one of six colored pegs:  White (W), Red (R),\n";
    	cout << "Blue (B), Green (G), Orange (O), and Yellow (Y).\n";
    	cout << "A player may choose duplicate colors when creating\n";
    	cout << "his/her sequence.  The goal of the game is to guess\n";
    	cout << "the random code generate by the computer in 8 or\n";
    	cout << "less guesses.  If the player cannot accomplish this\n";
    	cout << "task, the game will end, and the code will be shown.\n";
    	cout << "You may only use W, R, B, G, O, or Y when entering\n";
    	cout << "your selection.\n\n\n";
    	return;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What's wrong with it? What is happening that shouldn't be, or vice versa?
    (And you still have one broken return statement with five things in it.)

    As a free bonus gift from me to you, my Hat of Guessing says that you may want to just call a getInput routine once per guess -- the way it stands now if you type YYMY, the three valid characters will be input, and then it will wait for one more valid character.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    I rewrote it to this:
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    int setCode(void);
    char getGuess(void);
    void gameOver(void);
    void title(void);
    void gameDirections(void);
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f);
    int inexactMatches(char x, char y, char a, char b, char c, char d,
    				   char e, char f);
    void printSequence(char c1, char c2, char c3, char c4);
    int num, exact_count, inexact_counter, counter, count, code;
    int X_exact_count,A_exact_count,C_exact_count,E_exact_count;
    char code1, code2, code3, code4, color, entered_code1;
    char entered_code2, entered_code3, entered_code4, guess;
    
    int main(void)
    {
    	
    	counter = 0;
    	title();
    	gameDirections();
    	system("PAUSE");
    	system("CLS");
    	srand((long)time(NULL));
    	code1 = setCode();
    	code2 = setCode();
    	code3 = setCode();
    	code4 = setCode();
    	printSequence(code1, code2, code3, code4);
    	
    	do{
    		do{
    			cout << "Please enter color sequence:  ";
    			entered_code1 = getGuess();
    			entered_code2 = getGuess();
    			entered_code3 = getGuess();
    			entered_code4 = getGuess();
    
    			code=0;
    			if(entered_code1=='W'||entered_code1=='R'||entered_code1=='B'
    				||entered_code1=='G'||entered_code1=='O'||entered_code1=='Y')
    				code++;
    			if(entered_code2=='R'||entered_code2=='W'||entered_code2=='B'
    				||entered_code2=='G'||entered_code2=='O'||entered_code2=='Y')
    				code++;
    			if(entered_code3=='Y'||entered_code3=='O'||entered_code3=='G'
    				||entered_code3=='B'||entered_code3=='R'||entered_code3=='W')
    				code++;
    			if(entered_code4=='W'||entered_code4=='R'||entered_code4=='B'
    				||entered_code4=='G'||entered_code4=='O'||entered_code4=='Y')
    				code++;
    		}while(code!=4);
    			
    
    
    
    		exactMatches(code1, entered_code1, code2, entered_code2,
    					 code3, entered_code3, code4, entered_code4);
    		inexactMatches(code1, entered_code1, code2, entered_code2,
    					   code3, entered_code3, code4, entered_code4);
    		if(exact_count==4)
    		{
    			gameOver();
    			return 0;
    		}
    		counter++;
    	}while(counter<8);
    	gameOver();
    	return 0;
    }
    
    int setCode(void)
    {
    		num = 1+rand()%6;
    		
    		if(num==1)
    			color = 'W';
    		else if(num==2)
    			color = 'R';
    		else if(num==3)
    			color = 'B';
    		else if(num==4)
    			color = 'G';
    		else if(num==5)
    			color = 'O';
    		else if(num==6)
    			color = 'Y';
    
    	return (color);
    }
    char getGuess(void)
    {
    
    	cin >> guess;
    	return (guess);
    }
    
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f)
    {
    	X_exact_count = 0,A_exact_count = 0,C_exact_count = 0,E_exact_count = 0;
    
    	if(x==y)
    		X_exact_count++;
    	if(a==b)
    		A_exact_count++;
    	if(c==d)
    		C_exact_count++;
    	if(e==f)
    		E_exact_count++;
    
    	exact_count=X_exact_count+A_exact_count+C_exact_count+E_exact_count;
    	cout << exact_count << " exact matche(s)\n";
    	return (exact_count,X_exact_count,A_exact_count,C_exact_count,E_exact_count);
    }
    int inexactMatches(char x, char y, char a, char b, char c, char d,
    				   char e, char f)
    {
    	int Y_count=0, B_count=0, D_count=0, F_count=0;
    
    	if(y==a||y==c||y==e&&y!=x)
    		Y_count++;
    	if(b==x||b==c||b==e&&b!=a)
    		B_count++;
    	if(d==a||d==x||d==e&&d!=c)
    		D_count++;
    	if(f==a||f==c||f==x&&f!=e)
    		F_count++;
    
    	if(Y_count>1)
    		Y_count=1;
    		if(X_exact_count==1)
    			Y_count=0;
    	if(B_count>1)
    		B_count=1;
    		if(A_exact_count==1)
    			B_count=0;
    	if(D_count>1)
    		D_count=1;
    		if(C_exact_count==1)
    			D_count=0;
    	if(F_count>1)
    		F_count=1;
    		if(E_exact_count==1)
    			F_count=0;
    
    	count =Y_count+B_count+D_count+F_count;
    	cout << count << " inexact matche(s)\n";
    	return (count);
    }
    void printSequence(char c1, char c2, char c3, char c4)
    {
    	cout << c1 << " "<< c2 << " " << c3<< " " << c4;
    	return;
    }
    
    void gameOver(void)
    {
    	system("CLS");
    	title();
    	cout << "Thanks for playing.\n";
    	cout << "The correct sequence was ";
    	printSequence(code1, code2, code3, code4);
    	cout << ". "<<endl;
    	return;
    }
    
    void title(void)
    {
    	cout << "()()()()()()()()()()()()()()()\n";
    	cout << "()()()()()MASTERMI";
    	cout << "ND()()()()()\n";
    	cout << "()()()()()()()()()()()()()()()\n\n";
    	return;
    }
    
    void gameDirections(void)
    {
    	cout << "Directions:  A random code of four color pegs will\n";
    	cout << "be generate by the computer.  Each peg is chosen\n";
    	cout << "from one of six colored pegs:  White (W), Red (R),\n";
    	cout << "Blue (B), Green (G), Orange (O), and Yellow (Y).\n";
    	cout << "A player may choose duplicate colors when creating\n";
    	cout << "his/her sequence.  The goal of the game is to guess\n";
    	cout << "the random code generate by the computer in 8 or\n";
    	cout << "less guesses.  If the player cannot accomplish this\n";
    	cout << "task, the game will end, and the code will be shown.\n";
    	cout << "You may only use W, R, B, G, O, or Y when entering\n";
    	cout << "your selection.\n\n\n";
    	return;
    }
    its working so far. The inexact matches are still somewhat messed up.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    exactMatches()...
    Here's your function prototype:
    Code:
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f);
    Here's your function call:
    Code:
    		exactMatches(code1, entered_code1, code2, entered_code2,
    					 code3, entered_code3, code4, entered_code4);
    And here's your function definition:
    Code:
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f)
    {
    And finally, here's your function return:
    Code:
    	return (exact_count,X_exact_count,A_exact_count,C_exact_count,E_exact_count);
    There are problems with the above. The call ignores the return value, and the return violates the prototype.

    What are your compiler warnings set to?

    There are similar, but not exact, problems with inexactMatches().

    Todd

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    char getGuess(void)
    {
    	do{
    		cin >> guess;
    	}while((guess!='W')||(guess!='R')||(guess!='B')||(guess!='G')||(guess!='O')||(guess!='Y'));
    	return (guess);
    }
    This is also wrong, in the sense that the while-condition will ALWAYS be "true" - because whatever value guess is, it will be "not equal" to one of the other choices. You need to change your condition.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    int setCode(void);
    char getGuess(void);
    void gameOver(void);
    void title(void);
    void gameDirections(void);
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f);
    int inexactMatches(char x, char y, char a, char b, char c, char d,
    				   char e, char f);
    void printSequence(char c1, char c2, char c3, char c4);
    int num, exact_count, inexact_counter, counter, count, code;
    int X_exact_count,A_exact_count,C_exact_count,E_exact_count;
    char code1, code2, code3, code4, color, entered_code1;
    char entered_code2, entered_code3, entered_code4, guess;
    
    int main(void)
    {
    	
    	counter = 0;
    	title();
    	gameDirections();
    	system("PAUSE");
    	system("CLS");
    	srand((long)time(NULL));
    	code1 = setCode();
    	code2 = setCode();
    	code3 = setCode();
    	code4 = setCode();
    	printSequence(code1, code2, code3, code4);
    	
    	do{
    		do{
    			cout << "Please enter color sequence:  ";
    			entered_code1 = getGuess();
    			entered_code2 = getGuess();
    			entered_code3 = getGuess();
    			entered_code4 = getGuess();
    
    			code=0;
    			if(entered_code1=='W'||entered_code1=='R'||entered_code1=='B'
    				||entered_code1=='G'||entered_code1=='O'||entered_code1=='Y')
    				code++;
    			if(entered_code2=='R'||entered_code2=='W'||entered_code2=='B'
    				||entered_code2=='G'||entered_code2=='O'||entered_code2=='Y')
    				code++;
    			if(entered_code3=='Y'||entered_code3=='O'||entered_code3=='G'
    				||entered_code3=='B'||entered_code3=='R'||entered_code3=='W')
    				code++;
    			if(entered_code4=='W'||entered_code4=='R'||entered_code4=='B'
    				||entered_code4=='G'||entered_code4=='O'||entered_code4=='Y')
    				code++;
    		}while(code!=4);
    			
    
    
    
    		exactMatches(code1, entered_code1, code2, entered_code2,
    					 code3, entered_code3, code4, entered_code4);
    		inexactMatches(code1, entered_code1, code2, entered_code2,
    					   code3, entered_code3, code4, entered_code4);
    		if(exact_count==4)
    		{
    			gameOver();
    			return 0;
    		}
    		counter++;
    	}while(counter<8);
    	gameOver();
    	return 0;
    }
    
    int setCode(void)
    {
    		num = 1+rand()%6;
    		
    		if(num==1)
    			color = 'W';
    		else if(num==2)
    			color = 'R';
    		else if(num==3)
    			color = 'B';
    		else if(num==4)
    			color = 'G';
    		else if(num==5)
    			color = 'O';
    		else if(num==6)
    			color = 'Y';
    
    	return (color);
    }
    char getGuess(void)
    {
    
    	cin >> guess;
    	return (guess);
    }
    
    int exactMatches(char x, char y, char a, char b, char c, char d,
    				 char e, char f)
    {
    	X_exact_count = 0,A_exact_count = 0,C_exact_count = 0,E_exact_count = 0;
    
    	if(x==y)
    		X_exact_count++;
    	if(a==b)
    		A_exact_count++;
    	if(c==d)
    		C_exact_count++;
    	if(e==f)
    		E_exact_count++;
    
    	exact_count=X_exact_count+A_exact_count+C_exact_count+E_exact_count;
    	cout << exact_count << " exact matche(s)\n";
    	return (exact_count);
    }
    int inexactMatches(char x, char y, char a, char b, char c, char d,
    				   char e, char f)
    {
    	int Y_count=0, B_count=0, D_count=0, F_count=0;
    
    	if(y==a||y==c||y==e&&y!=x)
    		Y_count++;
    	if(b==x||b==c||b==e&&b!=a)
    		B_count++;
    	if(d==a||d==x||d==e&&d!=c)
    		D_count++;
    	if(f==a||f==c||f==x&&f!=e)
    		F_count++;
    
    	if(Y_count>1)
    		Y_count=1;
    		if(X_exact_count==1)
    			Y_count=0;
    	if(B_count>1)
    		B_count=1;
    		if(A_exact_count==1)
    			B_count=0;
    	if(D_count>1)
    		D_count=1;
    		if(C_exact_count==1)
    			D_count=0;
    	if(F_count>1)
    		F_count=1;
    		if(E_exact_count==1)
    			F_count=0;
    
    	count =Y_count+B_count+D_count+F_count;
    	cout << count << " inexact matche(s)\n";
    	return (count);
    }
    void printSequence(char c1, char c2, char c3, char c4)
    {
    	cout << c1 << " "<< c2 << " " << c3<< " " << c4;
    	return;
    }
    
    void gameOver(void)
    {
    	system("CLS");
    	title();
    	cout << "Thanks for playing.\n";
    	cout << "The correct sequence was ";
    	printSequence(code1, code2, code3, code4);
    	cout << ". "<<endl;
    	return;
    }
    
    void title(void)
    {
    	cout << "()()()()()()()()()()()()()()()\n";
    	cout << "()()()()()MASTERMI";
    	cout << "ND()()()()()\n";
    	cout << "()()()()()()()()()()()()()()()\n\n";
    	return;
    }
    
    void gameDirections(void)
    {
    	cout << "Directions:  A random code of four color pegs will\n";
    	cout << "be generate by the computer.  Each peg is chosen\n";
    	cout << "from one of six colored pegs:  White (W), Red (R),\n";
    	cout << "Blue (B), Green (G), Orange (O), and Yellow (Y).\n";
    	cout << "A player may choose duplicate colors when creating\n";
    	cout << "his/her sequence.  The goal of the game is to guess\n";
    	cout << "the random code generate by the computer in 8 or\n";
    	cout << "less guesses.  If the player cannot accomplish this\n";
    	cout << "task, the game will end, and the code will be shown.\n";
    	cout << "You may only use W, R, B, G, O, or Y when entering\n";
    	cout << "your selection.\n\n\n";
    	return;
    }
    my IF statements in my inexactMatches() function are messed up. if the random code is say W R O W and user enters WWWW it returns 2 inexact matches and 2 exact. it should be 2 exact and 0 inexact. how can i fix this? thanks

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Can you acknowledge what was pointed out previously?

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    5
    Quote Originally Posted by Todd Burch View Post
    Can you acknowledge what was pointed out previously?
    Yes, I changed the do while and now it works properly.
    I changed the values that are returned to main from exactMatches().

    I posted the new code in my last post. Everything works as needed except the inexactMatches() as I just explained. Any suggestions on the inexactMatches()? Thanks

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Hummm... for starters, look into the precedence of the logical AND and OR operators.

    Todd

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Also, personally, I would be much more inclined to debug your code if you commented it, stating your intention for each of the code sections. People approach solving problems differently, and I've about hit my limit of brain power I want to spend on reverse engineering this.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21