You are using a version without Ads of this website. Please, consider donating:

### Need help with this pls!!!!! Cant figure it out

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166`` ``````#include #include #include #include #include #include #include using namespace std; const int MAXDIGITS = 8; // picks a number between 1 and "maxValidDigit" (inclusive) int pickDigit (int maxValidDigit) { return 1 + int((rand() / (RAND_MAX + 1.0)) * maxValidDigit); } // selects a code (Hint: use pickDigit) //************************************* // write function "pickCode" here //************************************* // see if the user entered "quit" in any mixture of upper / lower case bool eqQuit(char guess[]) { int i, len = strlen(guess); char quit[]= "quit"; // if the length isn't 4, it's not quite if (len!=4) { return false; } // length is 4, so convert to all lower case and compare to string "quit" for (i=0;i> setw(MAXDIGITS + 2) >> guessString; cin.ignore (INT_MAX, '\n'); // discard reset of input line if (eqQuit(guessString)) { return false; } if (strlen(guessString) != (unsigned) numberOfDigits) { cout << "*** Invalid code (bad length) ***" << endl; } else { allValid = true; // make assumption for (i = 0; i < numberOfDigits; i++) { if (!isdigit(guessString[i])) { allValid = false; break; } guess[i] = guessString[i] - '0'; if ((guess[i] == 0) || (guess[i] > highestValidDigit)) { allValid = false; break; } } if (allValid) { return true; // code successfully obtained } cout << "*** Invalid code (bad digit) ***" << endl; } } } // analyzes the guess by the user to figure out how many blacks and whites // the user gets to help him/her make a better next guess //********************************************* // write function analyzeGuess here //********************************************* int main () { int digits, highestValidDigit, code[MAXDIGITS], guess[MAXDIGITS], blacks, whites; char reply; // "seed" the random number generator with the current system // time. this ensures that we won't get exactly the same // sequnce of "random" numbers every time we run the program. srand (time(NULL)); //**************************************************************************** // Insert code that reads in the number of digits and the highest valid digit. // This code should loop until valid values are entered. //**************************************************************************** do { // get the code that the user must guess pickCode (digits, code, highestValidDigit); /* // useful when debugging cout << "The code is "; for (int i = 0; i < digits; i++) { cout << code[i]; } cout << endl; */ for (;;) { if (!readGuess (digits, guess, highestValidDigit)) { // user has quit system("PAUSE"); return 0; } // work out how many blacks and whites should be displayed analyzeGuess (digits, code, guess, blacks, whites); if (blacks == digits) { cout << "Congratulations - you have broken the code!!" << endl; break; } cout << "Blacks : " << blacks << " Whites: " << whites << endl; } cout << "Do you wish to play again (Y/N): "; cin >> reply; cin.ignore (INT_MAX, '\n'); // discard rest of input line } while (toupper(reply) == 'Y'); system("PAUSE"); return 0; }``````
Last edited on
Cant figure out how to write function analyze guess...
Please wrap your code in [code] tags
@Zaita , i have done that
http://www.cplusplus.com/forum/articles/1295/
Please provide a better description of how your analyse function will work. And what you have attempted so far.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: