#Idk what's wrong with my code, how do i fix it

1 messages · Page 1 of 1 (latest)

gaunt shoalBOT
#

@weak wing

Cedro Uploaded Some Code

This is a number guessing game I made (not complete yet obviously)
The 1st half works fine but everything starting from the line while (num != random) { doesnt work
When i debug it, i input num and it's supposed to check the value of num and output a message but instead nothing happens?? why??

Uploaded these files to a Gist
weak wing
#

wtf

still crag
#

Uploaded files get automatically converted into text files uploaded to a github gist for security reasons.

weak wing
#

ok then

still crag
#

We really don't want people downloading random files from any of the chats

weak wing
#

if you say so ig

still crag
#

We've had people trying to get malicious code to server members

weak wing
#

ye i get what you're saying

still crag
#

!(cin >> num)

You doing bitwise shifts?

weak wing
#

uh

#

what

still crag
#

is a bitwise operator

weak wing
#

ok i'm going to admit something

#

i'm not taking any c++ lessons, i'm just learning this casually and the way i'm doing it is random bullshit go....so i actually dont know any technical terms

#

i just want my code to work and hopefully learn the way to make it work, even if i never get to understand why it works...but ofc if you wanna take the time to eli5 technical terms i wouldnt mind

still crag
#
char a = 5; // 5 (00000101)

printf("a << 1 = %d", a << 1)
// 5 (00000101) bitshifted to left by 1 resulting in 10 (00001010)
#

What are you trying to accomplish on that line?

weak wing
#

i'm tryna validate the input, and someone told me that's a faster way to do it

#

bc when i tried to do it in a more beginner friendly way it didnt work

#

on my 1st attempt i tried to make the code check if num was an integer

#

but for some reason, when i did it the usual way (checking if int(num) == num) it also recognized letters and symbols as integers

#

i didnt know how to fix it

#

someone suggested i do it this way instead, and it worked, so that's how i'm doing it now 🤷‍♂️

#

uhh am i making sense

still crag
#

Yeah

#

Hmm

weak wing
#

sorry

#

i'm bad at this

#

i dont know how or when my code broke

still crag
#

Did it work?

#

And sorry, I was wrong on the bitwise shift

#

cin >> num reads the cin until non-integer/EOF etc

weak wing
weak wing
#

so idk i might be remembering some details wrong

#

but you will see i used the same statement on another variable called difficulty above

#

and in that case it worked fine

weak wing
still crag
#

Hmm, tried to use an online compiler to run that

#

And it just refuses to compile

weak wing
#

huh??????

#

that's weird

still crag
#

Oh, there we go

#

Hmmm

weak wing
#

intense sweating

still crag
#

Yeah there's definitely some weirdness going on

#

It works if you fail the first thing, which kicks you into the proper loop

weak wing
#

wha-

#

you mean the num part works if you deliberately input something invalid in the difficulty part???

#

or what

still crag
#

After the difficulty selection

#

You gotta fail the first guess attempt

#

After which the loops continue running

weak wing
#

oh

#

uhhh can you identify why

still crag
#

Not that good with C++ unfortunately

weak wing
#

i-

#

cries

#

and yet there doesnt seem to be any obvious errors in the code......why is c++ like this 😭

#

oh well

still crag
#

I'm unsure what's going on with that code, but I'm guessing it's logical errors in the checks and how they're laid out

weak wing
#

guess i'll wait for c++ experts to help

weak wing
weak wing
#

ok so you said i only need to do the cin >> num thing once

#

but uh...how

#

those are kinda necessary for the loop to repeat properly, no?? otherwise it would just, idk, ask once and then stop

silver bear
#
while(totalguesses < maxguessesallowed) {
   cin >> num;

   if(cin.fail()) {
        cout << "enter a valid number";
        cin.clear();
        continue;
    }

   if(num > random) {
       ...
   } else if(num < random) {
       ...
   } else {
       cout << "you won!\n";
       ...
   }
   totalguesses++;
}
weak wing
#

but there are so many other conditions to take into account

#

what if num isnt even an integer??

#

then that would break the program

#

so i have to check first right??

#

oh

#

wait does the word continue cause the program to move to if(num > random) {

#

or does it cause it to go back to the start of the loop

silver bear
#

like it goes to the start again

weak wing
#

oh

#

and break just quits the loop??

silver bear
#

yeah

weak wing
#

ohhhh

#

aight lemme try this and see

#

thx

weak wing
#

?

#

um

#

not really

#

not that i'm that good at anything else

#

why

#

i mean

#

i can kinda write python....

#

and i started with pseudocode....

#

it's just, i obviously am not a pro lol

#

i can write them well enough to pass exams, that's it

#

future career considerations ig

#

and also bc i feel like waiting for someone to teach me would be futile

#

you're not tryna tell me i'm too dum dum to do this, are you kek

#

well...that was the plan yeah

#

but i assume some apps are made with c++ as well

#

a software engineer would prob be my backup plan tbh

#

i know game development is a shitty job, yadda yadda

#

but like

#

i have no idea what else i could possibly be interested in lol

#

even if only slightly

#

ye i guess

#

so um

#

what really were you tryna say lol

still crag
#

@north quest Did you have anything you were going to help with, or did you just feel the need to get that off your chest immediately after joining?

#

Not a good look

weak wing
#

wha-

#

um, did i just fall for some kind of elaborate discord scam ohheavens

still crag
#

I mean, it's kinda weird that the first thing you do after joining is tell someone they shouldn't try to learn what they're learning...

weak wing
#

also i just noticed

#

you also have the beginner flair

#

omg lmao

#

ok that's on me for being a fucking idiot, thanks hex kek

#

well not like i was about to change my opinion anyway, no harm done

still crag
#

Yehh, C++ is an useful language if you put in the effort to learn it, and you're on your way to learning C++ a lot more

weak wing
#

if you say so lol

#

tho tbh i prob couldnt take a c++ exam if i wanted to

#

you did tell me to start off with smth easier so like....

still crag
#

C++ is as good as any language

#

It's useful for you, so no reason why you shouldn't start learning that immediately

weak wing
#

ye

#

i just thought someone wanted to offer me assurance as to why i find it hard...

#

and then it turned out they didnt pick either the c++ or the pro flairs so...lmao

#

anyway

#

my original problem's alr solved so imma just dip lol

still crag
#

Yeah, was kinda off-topic for what you needed help with

weak wing
#

thx anyways

still crag
#

Np

#

Good that Jacob managed to help you with the problem after I miserably failed at helping you kek