Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++: Generate a random, 5 digit number as a string. The player needs to tr

ID: 638217 • Letter: U

Question

Using C++:

Generate a random, 5 digit number as a string.

The player needs to try to guess this 5 digit number.

The program should provide feedback to the player letting them know if they got any of the digits correct, using the Feedback Key (or hints) below:
1 = indicates Right Number, Right Place.
2 = indicates Right Number, Wrong Place.
0 = indicates Wrong Number.
For example:

If the computer program generates 12345 as the random number.

The player needs to make guesses in order to figure out what the secret, random number is. If the player guesses 11235, the computer should respond with the feedback code of: 10221.

Repeat step 2 and 3 until the player guesses the whole 5 digit number correctly (or, you can allow the player to do 10, 15, 20 tries).

If the player guesses it correctly, display the message "You guessed it correctly!" If the player doesn't guess it within the limited number of tries, display the message "You couldn't guess it. Game over."

Explanation / Answer

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;

int main()
{
   // init rand seed
   srand(time(NULL));
   int secnum = 11257; //((rand() % 99999) + 1);

   //split secnum into digits
   int numArr[5];
   int temp = secnum;
   cout << secnum << endl;
   //starts from unit digit
   for(int i=0; i <5; i++) {

       numArr[i] = temp % 10;
       temp = (int) temp/10;
   }

// for(int i=0; i <5; i++) {

      // cout << numArr[i] << " ";
// }

   int guess = 0;
   cout << "Guess a 5 digit number" << endl;


    int allCrt = 0;
   for(int i=0; i < 15; i++) {
      int msg[5] = {0,0,0,0,0};
      allCrt = 0;
       cin >> guess;
       cout << "your guess : "<<guess << endl;
       for(int j =0; j < 5; j++) {
    
           //get the digit from unit digits,tens digits etc
           int digit = guess %10;
          // cout << digit << " #### digit "<<endl;
           guess = guess/10;
           //check the digit present in secnumm or nnot
       
           for(int k=0; k <5; k++) {
                if(digit == numArr[j]) {
                       msg[j] = 1;
                         allCrt++;
                       break;
               } else if(digit == numArr[k]) {
                   
                    if( j == k) {
                       msg[k] = 1;
                       allCrt++;
                    }
                    else
                        msg[k] = 2;
    //                    cout << digit << "--## "<<numArr[k] << "--"<< msg[k] << "-- "<<k << endl;
                    if(j ==k)
                    break;
               }
           }
     
       }
  
  
        if(allCrt == 5) {
            cout << "You guessed it correctly! : " << endl;
            break;
        }

       cout << "   error: " ;
        for(int k = 4; k >=0; k--) {
               cout << msg[k] ;
       
       }
       cout << endl;
  
   }

    if(allCrt != 5) {
            cout << "You couldn't guess it. Game over" <<endl;
     
        }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote