#number guessing game semantic error
1 messages · Page 1 of 1 (latest)
why does'nt the ```cpp
bool playGame funcion did'nt execute ?
#include <iostream>
#include <random>
bool
playGame(int guesses, int number) {
for (int count{}; count <= guesses; ++count) {
if (number >= guesses)
std::cout << "Your guess is too high\n";
return true;
if (number <= guesses)
std::cout << "Your guess is too low\n";
return true;
;
}
}
bool playAgain() {
std::cout << "Would you like to play again y/n\n";
char ch{};
std::cin >> ch;
if (ch == 'y')
return true;
else if (ch == 'n')
return false;
}
int main() {
std::random_device rd;
std::seed_seq ss{rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()};
std::mt19937 mt{ss}; // initializtion of seed_seq
std::uniform_int_distribution die{1, 100};
constexpr int guesses{7}; // the user has this many guesses
do {
int number{die(mt)}; // this is the number the user needs to guess
std::cout << "Let's play a game. I'm thinking of a number between 1 and "
"100. You have "
<< guesses << " tries to guess what it is.\n";
bool won{playGame(guesses, number)};
if (won)
std::cout << "Correct! You win!\n";
else
std::cout << "Sorry, you lose. The correct number was " << number << '\n';
} while (playAgain());
std::cout << "Thank you for playing.\n";
}
I don't want to be offtopic but does the bot detect keywords/preprocessor directives/etc to start formatting?
It'd be cool to train a model to better identify code blocks, but, it'd be a tad tricky
bool
playGame(int guesses, int number) {
for (int count{}; count <= guesses; ++count) {
if (number >= guesses)
std::cout << "Your guess is too high\n";
return true;
if (number <= guesses)
std::cout << "Your guess is too low\n";
return true;
;
}
}```
This is pretty cursed formatting
clang format got thrown off by the bool playGame funcion did'nt execute ? being included in the source
but yes
I don't know tbh. bool won{playGame(guesses, number)}; doesn't look very standard but it should work. Have you tried a more standard syntax bool won = playGame(guesses, number); and have you gone through your program with a debugger?
@stiff pier
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
!solved