Jump to content

Well I am recently learning c++ and I am a total noob at it and that's why I need ur help guys .......

 

I was designing a program to let computer select an random number and user guess it .....depending upon the difference between the actual number and guessed number the computer replies "too far " or " close "or " very close " .......let the actual number be 'x' and guessed number be 'y'.....so now when you enter y ,I used the equation

 

 

' if(y-x>=30) {

Cout<<"very far"<<endl;

 

}

'

 

And likewise the rest of it but the real problem is it only works if y>x and if x>y then it messes things up ......

(You know what I mean to say ) 

 

 

So I created two parts to deal with it (one if x>y and y<x)......

.so now everything runs smooth until you get to a point where first you type a number that is y>x then it gives the valid response @s long as it is y>x .....but when I choose a number x>y .....the switching messes up everything......

 

 

Can somebody find a solution to this ......

 

I just know the basics of programming ............Also is there something like |x>y|>=30 (like you learned in school:it gives me error when I use it )

 

 

Thank you guys ......

Link to comment
https://linustechtips.com/topic/604693-how-to-use-in-c/
Share on other sites

Link to post
Share on other sites

3 minutes ago, Richard Stark said:

 

(one if x>y and y<x)

this is the exact same thing

 

you switched the sign around but also switched the variables

 

so basically you have

x>y and

x>y

 

if you want to have two different things you need to switch ONLY the > OR the variables

so x>y, x<y

 

or

x>y, y>x

 

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/604693-how-to-use-in-c/#findComment-7835893
Share on other sites

Link to post
Share on other sites

2 minutes ago, Enderman said:

this is the exact same thing

 

you switched the sign around but also switched the variables

 

so basically you have

x>y and

x>y

 

if you want to have two different things you need to switch ONLY the > OR the variables

so x>y, x<y

 

or

x>y, y>x

 

I am more of a Java guy, but I do know a little about C++ (actually less than little). But this does seem to make more sense to be honest. 

Link to comment
https://linustechtips.com/topic/604693-how-to-use-in-c/#findComment-7835908
Share on other sites

Link to post
Share on other sites

5 minutes ago, Richard Stark said:

Well I am recently learning c++ and I am a total noob at it and that's why I need ur help guys .......

 

I was designing a program to let computer select an random number and user guess it .....depending upon the difference between the actual number and guessed number the computer replies "too far " or " close "or " very close " .......let the actual number be 'x' and guessed number be 'y'.....so now when you enter y ,I used the equation

 

 

' if(y-x>=30) {

Cout<<"very far"<<endl;

 

}

'

 

And likewise the rest of it but the real problem is it only works if y>x and if x>y then it messes things up ......

(You know what I mean to say ) 

 

 

So I created two parts to deal with it (one if x>y and y<x)......

.so now everything runs smooth until you get to a point where first you type a number that is y>x then it gives the valid response @s long as it is y>x .....but when I choose a number x>y .....the switching messes up everything......

 

 

Can somebody find a solution to this ......

 

I just know the basics of programming ............Also is there something like |x>y|>=30 (like you learned in school:it gives me error when I use it )

 

 

Thank you guys ......

abs(y-x)

will give you the absolute value.

Link to comment
https://linustechtips.com/topic/604693-how-to-use-in-c/#findComment-7835911
Share on other sites

Link to post
Share on other sites

The absolute value, or modulus, is defined as :

cBmnAoz.png

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/604693-how-to-use-in-c/#findComment-7836027
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×