When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
201 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Tell me where did you get this code from?
I made it my self as simple practice to get better and learn more
as I said im verry new and simply wanna get bettr
Can you explain the logic?
essentially, i simply want it to state the rules at the beginning, have the user input a random guess as asked, if that number is correct, it tells you it is, if not it says not, and it the number is outside of the range 1-20, i want it to tell you.
Ok what is this part supposed to do
if(input > 20 || input < 0)```
simply display "The Guess Should be from 1-20!" if the number entered is out of the range 1-20\
i think you meant to compare number there, not input
But you're taking the input into an int called number.
I think your own variable names are confusing you.
ahhh i should use number in place of input in that string due to things, like how number is already defined, in places, like cin >> number;
is that right?
auto rng = std::default_random_engine(std::random_device()());
auto dist = std::uniform_int_distribution<int>(1, 20);
const int correctnumber = dist(rng);
Are you taking some course?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
const int correctnumber = rand() % 20 + 1;
int number;
string input;
const int one = 1;
double tries = 8;
cout << "Welcome To The Higher or Lower Game!" << endl;
cout << "You Have 8 Tries!\nThe Number Will Be Between 1 & 20!\nType -1 to Give up! & Good Luck" << endl;
while (tries > 0) {
cout << "Enter A Number: ";
cin >> number;
while (true) {
cin >> number;
if (number > 20 || number < 1){
cout << "The Guess Should be from 1-20!\n";
break;
}
if (number == -1) {
cout << "Would You Like To Give Up? (Y/N)" << endl;
cin >> input;
if (input == "Y"){
break;
}
else if (input == "N"){
cout << "Enter A Number: " << endl;
continue;
}
}
else if (number == correctnumber) {
cout << "You Are Correct" << endl;
break;
}
else if (number < correctnumber) {
cout << "To Low!" << endl;
}
else if (number > correctnumber) {
cout << "To High!" << endl;
}
else {
cout << "Incorrect, Try Again! " << endl;
}
tries --;
cout << "You Have " << tries << " Tries Left!" << endl;
if (tries == 0) {
cout << "You Have No More Tries!\nThe Correct Number Was: " << correctnumber << "." << endl;
break;
}
}
return 0;
}
there are a few syntax erros now,
yes, im at a community college a few weeks in
./main.cpp:72:2: error: expected '}'
72 | }
| ^
./main.cpp:7:12: note: to match this '{'
7 | int main() {
| ^
1 error generated.
make: *** [Makefile:10: main] Error 1
i dont understand
misleading indentation
Wait a second why do you have two while loops.
i saw that too
it’s going to have him do a bunch of inputs for no reason
u only need one
i think the first should have simply been an if staement rather than while
fixed that
one 1 now
ur tries counter is broken too
How so
Not really the inner loop shouldn't exist at all.
decrementing is misplaced
what does this mean
explain?
alright
if (number > 20 || (number < 1 && number != -1)) {
cout << "The Guess Should be from 1-20!" << endl;
continue; // and ask for input again
}
it should decrement after a guess that is valid
send new code again
Why
everything seems to be running smoothy, but now that i test it, when i write a number outside of 1-20, it just exits the program, doesnt display "The Guess Should be from 1-20!", doesnt continue, nothing.
It simply breaks, the program has no intention to continue if the guess is correct.
That's because you have a break statement where it tests the range of the input.
ah i just assumed its supposed to decrement like you're given 20 guesses
what do you mean, i dont see where your talking about
"The guess should be...."
Look below this line.
i fixed that to a continue; and its still having the same issue
No the program just exits, there is no need.
if you get 8 wrong tries it should exit yeah
Show the code.
^^^
If you get it correct same thing happens.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
const int correctnumber = rand() % 20 + 1;
int number;
string input;
double tries = 8;
cout << "Welcome To The Higher or Lower Game!" << endl;
cout << "You Have 8 Tries!\nThe Number Will Be Between 1 & 20!\nType -1 to Give up! & Good Luck" << endl;
if (tries > 0) {
cout << "Enter A Number: ";
cin >> number;
}
cin >> number;
if (number > 20 || number < 1) {
cout << "The Guess Should be from 1-20!" << endl;
continue;
}
if (number == -1) {
cout << "Would You Like To Give Up? (Y/N)" << endl;
cin >> input;
if (input == "Y"){
break;
}
else if (input == "N"){
cout << "Enter A Number: " << endl;
continue;
}
}
else if (number == correctnumber) {
cout << "You Are Correct" << endl;
break;
}
else if (number < correctnumber) {
cout << "To Low!" << endl;
}
else if (number > correctnumber) {
cout << "To High!" << endl;
}
else {
cout << "Incorrect, Try Again! " << endl;
}
tries --;
cout << "You Have " << tries << " Tries Left!" << endl;
if (tries == 0) {
cout << "You Have No More Tries!\nThe Correct Number Was: " << correctnumber << "." << endl;
break;
}
}
return 0;
}
Where tf is the loop.
if (tries > 0) {
cout << "Enter A Number: ";
cin >> number;
}```
is being checked before entering the loop
you want to have that inside of it
because youre counting the tries with each time it decrements
rn where you have it placed it's only counting once
I added
while (true) {
above cin >> number;
is that where it should be
also for future reference i wouldnt use using namespace std;
its just good programming practice to do std:: when using the standard library
At this point, put it wherever it works the best.
is it compiling
everything seems to be running smoothy, but now that i test it, when i write a number outside of 1-20, it just exits the program, doesnt display "The Guess Should be from 1-20!", doesnt continue, nothing.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
const int correctnumber = rand() % 20 + 1;
int number;
string input;
double tries = 8;
cout << "Welcome To The Higher or Lower Game!" << endl;
cout << "You Have 8 Tries!\nThe Number Will Be Between 1 & 20!\nType -1 to Give up! & Good Luck" << endl;
if (tries > 0) {
cout << "Enter A Number: ";
cin >> number;
}
while (true) {
cin >> number;
if (number > 20 || number < 1) {
cout << "The Guess Should be from 1-20!" << endl;
continue;
}
if (number == -1) {
cout << "Would You Like To Give Up? (Y/N)" << endl;
cin >> input;
if (input == "Y"){
break;
}
else if (input == "N"){
cout << "Enter A Number: " << endl;
continue;
}
}
else if (number == correctnumber) {
cout << "You Are Correct" << endl;
break;
}
else if (number < correctnumber) {
cout << "To Low!" << endl;
}
else if (number > correctnumber) {
cout << "To High!" << endl;
}
else {
cout << "Incorrect, Try Again! " << endl;
}
tries --;
cout << "You Have " << tries << " Tries Left!" << endl;
if (tries == 0) {
cout << "You Have No More Tries!\nThe Correct Number Was: " << correctnumber << "." << endl;
break;
}
}
return 0;
}
You can't convince me that you wrote this code, ever.
ChatGPT for the win.
ok snape, if your no longer here to help, I appreciate ur time, and have a good night man
LMFAO
Thanks and appreciated.
nah i think he might have
cheers mate
else if (number < correctnumber) {
cout << "To Low!" << endl;
}
else if (number > correctnumber) {
cout << "To High!" << endl;
}```
gpt wouldnt use the wrong too
he defo wrote it
I dont think he understands that i am new, and am learning, this is a big project for me, and its currently not working, hence why I went looking for help.
in the position that is in, wouldnt it be an if, rather than an else if?
can you send me the whole code as you have it, to compare to what i have now, to see what the difference is, because i may be lost on what lines they are now
this shit is a lot, and im ready to say fuck it and give up on it
id prefer not to
but this is annoying me
cin>>number callsi didnt write any code im just saying what was wrong
fix one by doing srand(time(0)); to actually make a random number tho
if(tries>0) should only be in the while loop
im not understanding where the actual while loop should begin
your range check should be if((number > 20 || number < 1) && number != -1)
right after welcome message
basically entire program is going into it
heres my current code now
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
const int correctnumber = rand() % 20 + 1;
int number;
string input;
double tries = 8;
cout << "Welcome To The Higher or Lower Game!" << endl;
cout << "You Have 8 Tries!\nThe Number Will Be Between 1 & 20!\nType -1 to Give up! & Good Luck" << endl;
while (true);
if (tries > 0) {
cout << "Enter A Number: ";
cin >> number;
}
if((number > 20 || number < 1) && number != -1)) {
cout << "The Guess Should be from 1-20!" << endl;
continue;
}
if (number == -1) {
cout << "Would You Like To Give Up? (Y/N)" << endl;
cin >> input;
if (input == "Y"){
break;
}
else if (input == "N"){
cout << "Enter A Number: " << endl;
continue;
}
}
else if (number == correctnumber) {
cout << "You Are Correct" << endl;
break;
}
else if (number < correctnumber) {
cout << "To Low!" << endl;
}
else if (number > correctnumber) {
cout << "To High!" << endl;
}
else {
cout << "Incorrect, Try Again! " << endl;
}
tries --;
cout << "You Have " << tries << " Tries Left!" << endl;
if (tries == 0) {
cout << "You Have No More Tries!\nThe Correct Number Was: " << correctnumber << "." << endl;
break;
}
}
return 0;
}
;compile
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
const int correctnumber = rand() % 20 + 1;
int number;
string input;
double tries = 8;
cout << "Welcome To The Higher or Lower Game!" << endl;
cout << "You Have 8 Tries!\nThe Number Will Be Between 1 & 20!\nType -1 to Give up! & Good Luck" << endl;
while (true);
if (tries > 0) {
cout << "Enter A Number: ";
cin >> number;
}
if((number > 20 || number < 1) && number != -1)) {
cout << "The Guess Should be from 1-20!" << endl;
continue;
}
if (number == -1) {
cout << "Would You Like To Give Up? (Y/N)" << endl;
cin >> input;
if (input == "Y"){
break;
}
else if (input == "N"){
cout << "Enter A Number: " << endl;
continue;
}
}
else if (number == correctnumber) {
cout << "You Are Correct" << endl;
break;
}
else if (number < correctnumber) {
cout << "To Low!" << endl;
}
else if (number > correctnumber) {
cout << "To High!" << endl;
}
else {
cout << "Incorrect, Try Again! " << endl;
}
tries --;
cout << "You Have " << tries << " Tries Left!" << endl;
if (tries == 0) {
cout << "You Have No More Tries!\nThe Correct Number Was: " << correctnumber << "." << endl;
break;
}
}
return 0;
}```
;compile
;compile
<source>: In function 'int main()':
<source>:24:52: error: expected primary-expression before ')' token
24 | if((number > 20 || number < 1) && number != -1)) {
| ^
<source>:33:9: error: break statement not within loop or switch
33 | break;
| ^~~~~
<source>:37:7: error: continue statement not within a loop
37 | continue;
| ^~~~~~~~
<source>:43:7: error: break statement not within loop or switch
43 | break;
| ^~~~~
<source>:63:7: error: break statement not within loop or switch
63 | break;
| ^~~~~
<source>: At global scope:
<source>:68:3: error: expected unqualified-id before 'return'
68 | return 0;
| ^~~~~~
<source>:70:1: error: expected declaration before '}' token
70 | }
| ^
Build failed
it doesnt even compile
indeed
read compiler error first
if((number > 20 || number < 1) && number != -1)) has an extra closing parentheseswait reading it again
do you have to use a while loop
I sont understand why you would
while true should be {} instead of ;
dont
i mean in your assignment
oh, yes
what do you mean
you're using a boolean value
instead of your tries value
youre just having the while loop run forever if its true
should i just rid the while loop?
at this point, i dont give a fuck about the grade, i just wanna get this shit working in a way i can understand it
no you should have while(tries > 0) instead of while(true)
I see
@jaunty sentinel Has your question been resolved? If so, type !solved :)
send current code again
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
const int correctnumber = rand() % 20 + 1;
int number;
string input;
double tries = 8;
cout << "Welcome To The Higher or Lower Game!" << endl;
cout << "You Have 8 Tries!\nThe Number Will Be Between 1 & 20!\nType -1 to Give up! & Good Luck" << endl;
while (tries > 0) {
cout << "Enter A Number: ";
cin >> number;
}
if(number > 20 || number < 1) {
cout << "The Guess Should be from 1-20!" << endl;
continue;
}
if (number == -1) {
cout << "Would You Like To Give Up? (Y/N)" << endl;
cin >> input;
if (input == "Y"){
break;
}
else if (input == "N"){
cout << "Enter A Number: " << endl;
continue;
}
}
else if (number == correctnumber) {
cout << "You Are Correct" << endl;
break;
}
else if (number < correctnumber) {
cout << "To Low!" << endl;
}
else if (number > correctnumber) {
cout << "To High!" << endl;
}
else {
cout << "Incorrect, Try Again! " << endl;
}
tries --;
cout << "You Have " << tries << " Tries Left!" << endl;
if (tries == 0) {
cout << "You Have No More Tries!\nThe Correct Number Was: " << correctnumber << "." << endl;
break;
}
}
return 0;
;compile
<source>: In function 'int main()':
<source>:24:9: error: continue statement not within a loop
24 | continue;
| ^~~~~~~~
<source>:31:9: error: break statement not within loop or switch
31 | break;
| ^~~~~
<source>:35:7: error: continue statement not within a loop
35 | continue;
| ^~~~~~~~
<source>:41:7: error: break statement not within loop or switch
41 | break;
| ^~~~~
<source>:61:7: error: break statement not within loop or switch
61 | break;
| ^~~~~
<source>: At global scope:
<source>:66:3: error: expected unqualified-id before 'return'
66 | return 0;
| ^~~~~~
Build failed
okay
move the bottom brace of your while() statement below if(tries == 0) method
also you're missing a brace below return
can you give me a shown example, i dont see what you mean, the brace is below the staement? it it not
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
const int correctnumber = rand() % 20 + 1;
int number;
string input;
double tries = 8;
cout << "Welcome To The Higher or Lower Game!" << endl;
cout << "You Have 8 Tries!\nThe Number Will Be Between 1 & 20!\nType -1 to Give up! & Good Luck" << endl;
while (tries > 0) {
cout << "Enter A Number: ";
cin >> number;
if(number > 20 || number < 1) {
cout << "The Guess Should be from 1-20!" << endl;
continue;
}
if (number == -1) {
cout << "Would You Like To Give Up? (Y/N)" << endl;
cin >> input;
if (input == "Y"){
break;
}
else if (input == "N"){
cout << "Enter A Number: " << endl;
continue;
}
}
else if (number == correctnumber) {
cout << "You Are Correct" << endl;
break;
}
else if (number < correctnumber) {
cout << "To Low!" << endl;
}
else if (number > correctnumber) {
cout << "To High!" << endl;
}
else {
cout << "Incorrect, Try Again! " << endl;
}
tries --;
cout << "You Have " << tries << " Tries Left!" << endl;
if (tries == 0) {
cout << "You Have No More Tries!\nThe Correct Number Was: " << correctnumber << "." << endl;
break;
}
}
return 0;
}```
;compile
Welcome To The Higher or Lower Game!
You Have 8 Tries!
The Number Will Be Between 1 & 20!
Type -1 to Give up! & Good Luck
Enter A Number: The Guess Should be from 1-20!
Enter A Number: The Guess Should be from 1-20!
Enter A Number: The Guess Should b
all you had to do was move the brace from your while statement to have all of your code be inside of it
and you were missing the closing brace for main below return 0;
i used an extra brace i didnt need below that staemebtm and should have simply put it under return 0; to make it all in the loop
is that right?
staement*
statement*
you were missing the brace below return regardless
you were missing 2 braces
one for while and one for closing main
where was the one for the while that I was missing, thats the part im lost on
i understand the other shit i messed up
but that
no
the one for while should have been right above return 0
anyway now that its all good
id do !solved
OOOOOOOOOOOOOH SHIT
yeah
thanks
np!
sorry im new and a dumb ass, im learning a lot
its ok im learning too
thanks for your time
You can only close threads you own
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
i doubt chatgpt would have bugs
Try 3.0
gpt could have easily done it
3.5 would have done it in a heartbeat
#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
// Initialize random seed
std::srand(static_cast<unsigned int>(std::time(nullptr)));
// Generate a random number between 1 and 20
int numberToGuess = std::rand() % 20 + 1;
int playerGuess;
int attempts = 8;
std::cout << "Welcome to the Higher or Lower game!" << std::endl;
std::cout << "I'm thinking of a number between 1 and 20. You have 8 attempts to guess it." << std::endl;
for (int i = 0; i < attempts; ++i) {
std::cout << "Attempt " << i + 1 << ": Enter your guess: ";
std::cin >> playerGuess;
if (playerGuess == numberToGuess) {
std::cout << "Congratulations! You guessed the correct number: " << numberToGuess << std::endl;
break;
} else if (playerGuess < numberToGuess) {
std::cout << "Higher!" << std::endl;
} else {
std::cout << "Lower!" << std::endl;
}
if (i == attempts - 1) {
std::cout << "Sorry, you've run out of attempts. The correct number was: " << numberToGuess << std::endl;
}
}
return 0;
}```
eh no input validation but it works
yea, second prompt for input validation, and done
with larger codebases i bet its a pain
very powerful for beginners tho
say you wouldn't know how to iterate through a bunch of images would you
with different filepaths
true