So there is an error in my loops :/
basically the code is for a mastermind game, if you can see my error before I find it feel free to help me out! I will be posting the finished version (When it is fixed!)
#include <iostream>
//only library needed
usingnamespace std;
int main () {
int n = 0, m = 0, p = 0, tmp = 0, tmp2 = 0;//declare variables like this
char guess[10], code[10];
cout << "Enter your Guess: (Example, Blue Red Yellow Green = BRYG )\n";
cin >> guess;
cout << "Enter the Code:\n";
cin >> code;
while (( m <= 3 ) && ( n <= 3 ))
{
if ( guess[n] == code[m] )
{
cout << guess[n] << code[m];//why output this?
cout << "Black " << endl;
n++;
m++;
}
else
{
tmp += n;
tmp2 += m;
m = 0;
do { if ( guess[n] == code[m] ) {
cout << "White ";
p++;
}
else {m++;}
}
while ( (p != 1) && (m < 4) );
m = 0;//not sure what this is for
n = 0;
n = n + tmp;//not sure wha this is for
m = m + tmp2;
}
m++;//not sure what this could possibly do
n++;
}
return 0; // you forgot this
}
Enter your Guess: (Example, Blue Red Yellow Green = BRYG )
Blue
Enter the Code:
Blue
BBBlack
uuBlack
Okay here is what I have now, thanks for all your pointers ui uiho, perhaps this next version might give a more clear understanding of what I am trying to accomplish:
#include <iostream>
usingnamespace std;
int main () {
int n;
int m;
int p;
int tmp;
int tmp2;
char guess[10];
char code[10];
cout << "Enter your Guess: (Example, Blue Red Yellow Green = BRYG )\n";
cin >> guess;
cout << "Enter the Code:\n";
cin >> code;
n = 0;
m = 0;
p = 0;
tmp = 0;
tmp2 = 0;
do {
do {
tmp = guess[n];
tmp2 = code[m];
if (( n == m ) && ( tmp == tmp2))
{cout << n << m << "Black "; p++;}
if (( n == m) && ( tmp != tmp2))
{m++;}
if (( n != m) && ( tmp == tmp2))
{cout << n << m << "White "; p++;}
if (m == 3) { cout << n << m << "None "; p++;}
else {m++;}
} while ( p == 0 );
p = 0;
m = 0;
n++;
} while ( n <=3 );
return 0;
}
Example of input and the error being received :)
Enter your Guess: (Example, Blue Red Yellow Green = BRYG )
RBRB
Enter the Code:
BRBR
03White 03None 10White 21White 30White
Im not sure how things work on here, but I feel as if my topic may require a bump ^^; feel free to correct me if this is the wrong way to approach these boards...
If you're clear in what the problem is, you generally get a response. You've given an example of input and say there's an error. You don't say what the error is.
What is the expected result for the input you gave?
I looked up the game of Mastermind, so I'm reasonably certain I get what you're trying to do here, but you can't expect people to read your mind and know what your code with such informative names as n, m, p, tmp and tmp2 and no comments is supposed to do.
Presumably code is supposed to represent the pattern you're solving for, and you want to compare guess to code and output the appropriate number of black and white pegs.
I actually found it easier to just code the damn thing than deal with the cryptic variable names.
Thank you so much cire. That version seems to work perfectly, and you have greatly helped my understanding! I am sorry about the badly named variables and lack of comments, I will try to improve those things.