Closed. This question needs details or clarity . It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post .
Closed 3 days ago.
Improve this question
This program is a secret 5-digit code guessing game. Each digit of the code is between 0 and 9, and digits cannot repeat in the code. After entering a digit one at a time, the player receives feedback for each digit in the form of a symbol: *, +, or -. Each symbol has a different meaning:
- means the digit is in the code and it is in the correct position
- means the digit is in the code but it is not in the correct position
- means the digit is not in the code
I am having trouble with the search_array function, specifically what value should be in the function call for the parameter 't'.
Secondly, I can't get the second do-while loop to work correctly, I figured that these problems went hand-in-hand with each other.
This code is from a past assignment and the directions stated that we couldn't change any of the functions given to us, but that we could add variables and addtional functions as needed. I would like to adhere to these rules as much as possible.
I have tried multiple things to solve this issue, such as putting in the number of digits (5) and trying to put an array of all the numbers in the five digit code (which wouldn't work because t is an int). I put 'i' in there as a placeholder until I could find a solution.
Here is my current code:
#include <iostream>
using namespace std;
void game_instructions(int n);
bool contains_duplicates(int a[], int n);
bool search_array(int a[], int n, int t);
int main()
{
//Step 1: Declare your variables and constants
const int SIZE = 5; //this constant stores the length/size of the code
int randcode[SIZE]; //this array stores the code generated randomly by the computer
int guess[SIZE];
const int SIZE2 = 5;
int target[SIZE2]{ 1, 7, 4, 0, 9 };
//this array stores the code inputted by the player/user
//you may add more variables as needed
//Step 2: Output game instructions by calling function game_instructions
game_instructions(SIZE);
//Step 3: Generate a random code and store it in the array randcode one digit at a time.
//Each digit should be between 0 and 9 and should be stored as one array element
//Recall that rand() % 10 can be used to generate a number between 0 and 9
do
{
for (int i = 0; i < SIZE; i++)
{
randcode[i] = rand() % 10;
cout << randcode[i];
}
//Step 4: Repeat step 3 if the array randcode contains duplicates
//You must use function contains_duplicates to implement this step
} while (contains_duplicates(randcode, SIZE));
do
{
//Step 5: Ask the user for his guess and store it in the array guess.
//Read one digit at a time, validate it to make sure it is between 0 and 9, and store it as one array element
cout << "\nEnter your guess for the 5 digit code(one digit at a time) :\n";
for (int g = 0; g < SIZE; g++)
{
cout << "Enter digit " << (g + 1) << ":";
cin >> guess[g];
while (guess[g] < 0 || guess[g] > 9)
{
cout << "Your guess must be between 0 and 9.\n";
cout << "Please re-enter: ";
cin >> guess[g];
}
}
//Step 6: Output the array guess on a single line with a space after each element (see the sample output provided)
for (int g = 0; g < SIZE; g++)
{
cout << guess[g] << " ";
}
cout << "\n";
//Step 7: Compare the randomly generated code (randcode) with the user's guess (guess)
//and display feedback for each digit as: *, + or –, as explained below:
//For each digit in the user's guess do the following:
// If it matches the digit from the random code (both value and position), output *
// Otherwise, if it appears anywhere in the random code, output + (use function search_array here)
// Otherwise, output -
for (int i = 0; i < SIZE; i++)
{
if (guess[i] == randcode[i])
cout << "* ";
else if (search_array(randcode, 5, i))
{
cout << "+ ";
}
else
cout << "- ";
}
} while (guess != randcode);
//Step 8: Repeat steps 5,6,7 until all digits have been guessed correctly
//Step 9: Output congratulations message
cout << "\nGood job! You guessed the code!";
}
void game_instructions(int n)
//This function outputs the game instructions.
//Its parameter n represents the length of the code.
{
cout << "A random " << n << " digit code has been generated. You have to guess it. \n";
cout << "For every digit you will receive feedback in the form of *, + or - \n";
cout << " * means the digit is in the code and it is in the correct position.\n";
cout << " + means the digit is in the code but it is not in the correct position.\n";
cout << " - means the digit is not in the code.\n";
}
bool search_array(int a[], int n, int t)
//This function searches the array a (of size n) for a target value t.
//If t is found in the array the function returns true; otherwise it returns false.
{
for (int i = 0; i < n; i++)
{
if (a[i] == t) return true;
}
return false;
}
bool contains_duplicates(int a[], int n)
//This function searches the array a (of size n) and returns true if the array contains duplicates; otherwise, it returns false.
{
for (int i = 0; i < n; i++)
{
//compare element a[i] with all the remaining elements from index i+1 to n
for (int j = i + 1; j < n; j++)
{
if (a[i] == a[j]) return true;
}
}
return false;
}
1条答案
按热度按时间ozxc1zmp1#
仅仅通过向程序中添加一些调试输出,您就可以获得很多见解。
此外,编译时带有警告会告诉您:
SIZE2
和target
变量未被使用,并且,主要地,guess != randcode
自C++20起已弃用。您最好将其更改为memcmp
。调用
search_array(randcode, 5, i)
在数组randcode
(大小为5
)中查找值i
,但该块试图实现的是知道用户输入的给定数字是否在randcode
中;并且该给定数是guess[i]
。对于用户输入的每个数字:
guess[i] == randcode[i]
,即用户是否同时猜到了数字和位置,在这种情况下,打印'*'
。'+'
打印;否则为'-'
。Demo(https://godbolt.org/z/MT6x91s3n)