Array Program Getting Error
Nov 21, 2013 at 1:51am UTC
I have all the proper code and when I run it, it just keeps looping when you enter the letters. I also used a function to make it so you could enter both capital and lowercase letters. It did not work and I got an error saying guess() was corrupted. I am completely past my knowledge at point and any help would be great!
Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#include <iostream>
using namespace std;
const int NUM_PEGS = 4;
class MasterMind
{
public :
MasterMind();
void gameLoop();
int numAllRight(char guess[]);
int numHalfRight(char guess[]);
bool isGoodGuess(char guess[]);
private :
char secret[NUM_PEGS];
};
int main()
{
MasterMind brain;
brain.gameLoop();
return 0;
}
MasterMind::MasterMind()
{
for (int i=0; i<4; i=i+1){
switch (rand()%5)
{
case 0:
secret[i]= 'B' ;
break ;
case 1:
secret[i]= 'R' ;
break ;
case 2:
secret[i]= 'G' ;
break ;
case 3:
secret[i]= 'P' ;
break ;
case 4:
secret[i]= 'Y' ;
break ;
}
}
}
void MasterMind::gameLoop()
{
char guess[NUM_PEGS];
cout << "What color pegs(in numerical order)???\n" ;
cin >> guess;
while (!isGoodGuess(guess))
{
cout << "Please re-enter your guess\n" ;
cin >> guess;
}
}
int MasterMind::numAllRight(char guess[])
{
int pNum = 0;
for (int i = 0; i < NUM_PEGS; i = i + 1)
{
if (guess[i] == secret[i])
pNum = pNum + 1;
}
return pNum;
}
int MasterMind::numHalfRight(char guess[])
{
int pNum = 0;
bool check[NUM_PEGS];
for (int i = 0; i < NUM_PEGS; i = i + 1)
{
if (guess[i] == secret[i])
check[i] = false ;
else
check[i] = true ;
}
for (int i = 0; i < NUM_PEGS; i = i + 1)
{
if (check[i] == true )
{
for (int j = 0; j < NUM_PEGS; j = j + 1)
{
if (check[j] == true && i != j)
if (secret[j] == guess[i])
pNum = pNum + 1;
}
}
}
return pNum;
}
bool MasterMind::isGoodGuess(char guess[])
{
for (int i=0; i < NUM_PEGS; i = i+1)
{
if (guess[i] != 'B' ||'b' && guess[i] != 'R' ||'r' && guess[i] != 'G' ||'g' && guess[i] != 'P' ||'p' && guess[i] != 'Y' ||'y' )
return false ;
}
return true ;
}
Last edited on Nov 21, 2013 at 2:29am UTC
Topic archived. No new replies allowed.