#Broken statement, please help im new.

201 messages · Page 1 of 1 (latest)

sharp lanternBOT
#

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.

sweet aurora
#

Tell me where did you get this code from?

jaunty sentinel
#

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

sweet aurora
#

Can you explain the logic?

jaunty sentinel
#

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.

sweet aurora
#

Ok what is this part supposed to do

if(input > 20 || input < 0)```
jaunty sentinel
#

simply display "The Guess Should be from 1-20!" if the number entered is out of the range 1-20\

eager folio
#

i think you meant to compare number there, not input

sweet aurora
#

I think your own variable names are confusing you.

jaunty sentinel
#

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?

eager folio
#
auto rng = std::default_random_engine(std::random_device()());
auto dist = std::uniform_int_distribution<int>(1, 20);
const int correctnumber = dist(rng);
jaunty sentinel
#

that

#

what is even that

#

I changed my code

#

possible fix

jaunty sentinel
#
#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,

jaunty sentinel
#

./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

eager folio
#

misleading indentation

sweet aurora
#

Wait a second why do you have two while loops.

lament grotto
#

it’s going to have him do a bunch of inputs for no reason

#

u only need one

jaunty sentinel
#

i think the first should have simply been an if staement rather than while

#

fixed that

#

one 1 now

lament grotto
#

ur tries counter is broken too

jaunty sentinel
#

How so

sweet aurora
#

Not really the inner loop shouldn't exist at all.

lament grotto
jaunty sentinel
jaunty sentinel
sweet aurora
#

The while(true) should be omitted.

#

It has no use.

jaunty sentinel
#

alright

lament grotto
#

it should decrement after a guess that is valid

#

send new code again

jaunty sentinel
#

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.

sweet aurora
#

It simply breaks, the program has no intention to continue if the guess is correct.

sweet aurora
lament grotto
jaunty sentinel
lament grotto
#

oh wait its 8 tries

#

yeah it should still decrement

sweet aurora
#

"The guess should be...."
Look below this line.

jaunty sentinel
#

i fixed that to a continue; and its still having the same issue

sweet aurora
lament grotto
lament grotto
#

^^^

jaunty sentinel
#

sure

#

1 sec

sweet aurora
jaunty sentinel
#
#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;

}
lament grotto
#
 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

jaunty sentinel
#

is that where it should be

lament grotto
#

also for future reference i wouldnt use using namespace std;

jaunty sentinel
#

noted

#

i dont understand whats broken

lament grotto
#

its just good programming practice to do std:: when using the standard library

sweet aurora
lament grotto
#

is it compiling

jaunty sentinel
# lament grotto 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.

lament grotto
#

send code again i'll compile it on my pc

#

updated code

jaunty sentinel
#
#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;

}
sweet aurora
#

ChatGPT for the win.

jaunty sentinel
#

ok snape, if your no longer here to help, I appreciate ur time, and have a good night man

lament grotto
#

LMFAO

lament grotto
#

nah i think he might have

jaunty sentinel
#

cheers mate

lament grotto
#
  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

jaunty sentinel
jaunty sentinel
lament grotto
#

no its fine as is

#

ok from what i see

jaunty sentinel
#

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

lament grotto
#
  1. correct number should be calling srand()
  2. you have two cin>>number calls
  3. you have an infinite loop for an invalid input
#

i 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

jaunty sentinel
#

im not understanding where the actual while loop should begin

lament grotto
#

your range check should be if((number > 20 || number < 1) && number != -1)

lament grotto
#

basically entire program is going into it

jaunty sentinel
#

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;

}
lament grotto
#

;compile

inner lindenBOT
#
Critical error:

You must attach a code-block containing code to your message or quote a message that has one.

lament grotto
#
#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
inner lindenBOT
#
Compiler Output
<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
lament grotto
#

it doesnt even compile

jaunty sentinel
#

indeed

lament grotto
#

read compiler error first

#
  1. you have an extra semi colon after while(true)
#
  1. if((number > 20 || number < 1) && number != -1)) has an extra closing parentheses
#

wait reading it again

#

do you have to use a while loop

jaunty sentinel
#

I sont understand why you would

lament grotto
#

while true should be {} instead of ;

jaunty sentinel
#

dont

lament grotto
jaunty sentinel
#

oh, yes

lament grotto
#

you while loop should be off your tries value not a boolean

#

like while(tries >0)

jaunty sentinel
#

what do you mean

lament grotto
#

you're using a boolean value

#

instead of your tries value

#

youre just having the while loop run forever if its true

jaunty sentinel
#

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

lament grotto
#

no you should have while(tries > 0) instead of while(true)

jaunty sentinel
#

I see

sharp lanternBOT
#

@jaunty sentinel Has your question been resolved? If so, type !solved :)

jaunty sentinel
#

nope 😄

#

im fucking dying bot

lament grotto
#

send current code again

jaunty sentinel
#
#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;
inner lindenBOT
#
Compiler Output
<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
lament grotto
#

okay

#

move the bottom brace of your while() statement below if(tries == 0) method

#

also you're missing a brace below return

jaunty sentinel
#

can you give me a shown example, i dont see what you mean, the brace is below the staement? it it not

lament grotto
# jaunty sentinel can you give me a shown example, i dont see what you mean, the brace is below th...
#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;
}```
inner lindenBOT
#
Program Output
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
jaunty sentinel
#

what tf

#

this is fucking my brain

lament grotto
#

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;

jaunty sentinel
#

ohhhhhhhhhhhhhhhh

#

fml

lament grotto
#

well thats just to compile it

#

you have other issues

jaunty sentinel
#

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*

lament grotto
#

you were missing 2 braces

#

one for while and one for closing main

jaunty sentinel
#

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

lament grotto
#

anyway now that its all good

#

id do !solved

jaunty sentinel
#

OOOOOOOOOOOOOH SHIT

lament grotto
#

yeah

jaunty sentinel
#

thanks

lament grotto
#

np!

jaunty sentinel
#

sorry im new and a dumb ass, im learning a lot

lament grotto
#

its ok im learning too

jaunty sentinel
#

thanks for your time

lament grotto
#

np good luck with the rest of your stuff

#

!solved

sharp lanternBOT
jaunty sentinel
#

thanks, ive done good until this

#

!solved

sharp lanternBOT
#

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

delicate willow
sweet aurora
lament grotto
#

3.5 would have done it in a heartbeat

delicate willow
#
#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

lament grotto
#

yep

#

that was with one prompt i assume

delicate willow
#

yea, second prompt for input validation, and done

lament grotto
#

with larger codebases i bet its a pain

delicate willow
#

very powerful for beginners tho

lament grotto
#

say you wouldn't know how to iterate through a bunch of images would you

#

with different filepaths

lament grotto
timber phoenix
#

Lol, I had a professor at university who accused EVERRYONE of copying code.

Like, his most common phrase when looking at someone's code was "which site did you copy this function from?"

#

Kind of demoralizing after you had been writing this until 2 a.m.

sweet aurora
#

My baddest of bads.

#

Genuinely.