for a class I have been asked to create a random number guessing game that generates a random 5 digit number then prompts the user to guess the number. The game then generates feedback to help the user make a better guess (assuming they didn't guess right the first time). For example if the first number in both the random number array and the user guess array match the game outputs a 1, if the first number in the guess array does not match the first number in the random number array but matches one of the other numbers, the game outputs a 2; if the first number in the guess array does not match any numbers in the random number array, the game outputs a 0 (i. e. if random number array = 31350 and user guesses 32010, the feedback would print as 10221). I think I have it mostly done but the feedback portion is not working properly. Here is my code so far.
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
bool checkGuess(int numberToGuess[], int guess[], int size);
int _tmain(int argc, _TCHAR* argv[])
{
const int DIGITS = 5;
int numberToGuess[DIGITS]; //declares array holding the random string of numbers
int guess[DIGITS]; //declares array holding the user guesses
srand(time(NULL)); //random seed
cout << "Welcome to the number guessing game!\n";
cout << "In this game you will try to guess a random 5 digit number" << endl;
for (int i = 0; i < DIGITS; i++) //loop to generate the random number
{
numberToGuess[i] = rand() % 9 + 1;
}
do
{
cout << "Please enter your guess: " << endl; //prompts user for their guess
cin >> guess[1, 2, 3, 4, 5];
bool comparedGuess = checkGuess(numberToGuess, guess, 5);
if (comparedGuess == false)
{
for (int i = 0; i < DIGITS; i++)
{
if (guess[i] == numberToGuess[i])
{
guess[i] = 1;
}
else if (guess[i] == numberToGuess[0, 1, 2, 3, 4])
{
guess[i] = 2;
}
else
{
guess[i] = 0;
}
}
}
for (int i = 0; i < DIGITS; i++)
{
cout << guess[i];
}
cout << endl;
}
while (guess[DIGITS] != numberToGuess[DIGITS]);
cout << "You guessed it correctly!" << endl;
system("pause");
return 0;
}
bool checkGuess(int numberToGuess[], int guess[], int size) //function to compare user guess to random number
{
if (guess[size] == numberToGuess[size])
{
return true;
}
else
{
return false;
}
}
Aucun commentaire:
Enregistrer un commentaire