Skip to content
Snippets Groups Projects
Commit ac133c73 authored by joshrandall8478's avatar joshrandall8478
Browse files

added guess history

parent 55af64fa
No related branches found
No related tags found
No related merge requests found
{
"configurations": [
{
"name": "macos-clang-arm64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "${default}",
"cppStandard": "${default}",
"intelliSenseMode": "macos-clang-arm64",
"compilerArgs": [
""
]
}
],
"version": 4
}
\ No newline at end of file
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
\ No newline at end of file
No preview for this file type
......@@ -10,11 +10,13 @@ int main() {
int randNumber = rand() % 9 + 1;
int guess = -1;
int guesses = 5;
int guessArr[5];
int correctGuess = 0;
while (guess != randNumber && guesses > 0 && correctGuess == 0){
printf("%s %d %s", "Pick a random number between 1 and 9. You have", guesses, "guesses left\n");
printf("\n[1-9]: ");
scanf("%d", &guess);
guessArr[5-guesses] = guess;
guesses--;
if(guess < 1 || guess > 9){
printf("Invalid guess.\n\n");
......@@ -26,16 +28,29 @@ int main() {
printf("\nLarger.\n\n");
}
else{
int guessHistoryLength = 5 - guesses;
int guessHistory[guessHistoryLength];
for(int i = 0; i < (guessHistoryLength); i++){
guessHistory[i] = guessArr[i];
}
printf("\n%s %d\n", "Correct! The correct answer was", randNumber);
printf("==================================\n");
printf("%s %d\n", "Guesses used: ", (5-guesses));
printf("%s", "Guess history: ");
for(int i = 0; i < (guessHistoryLength); i++){
printf("%d ",guessHistory[i]);
}
correctGuess = 1;
}
}
if (correctGuess != 1){
printf("%s %d\n", "Damn. The correct answer was ", randNumber);
printf("%s %d\n", "Damn. The correct answer was", randNumber);
printf("==================================\n");
printf("%s %d\n", "Guesses used: ", (5-guesses));
printf("%s", "Guess history: ");
for(int i = 0; i < (sizeof(guessArr) / sizeof(guessArr[0])); i++){
printf("%d ",guessArr[i]);
}
}
return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment