Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statements in c++ doubles

Tags:

c++

#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <stdlib.h>

using namespace std;

int main()
{
    int x,y,z;
cout<<"welcome to guessing game\nplayer one pick your number: ";
cin>>x;
if (x < 0)(x > 100);
{
    cout<<"out of number range";
}
Sleep(2000);
system("cls");
cout<<"ok player 2 pick the guess";
cin>>y;
if (x == y){
      cout<<"congrats you got it right";
           }
            else{
            if (x < y){
            cout<<"Go lower";}
            else {
            if (x > y){
            cout<<"higher";}}
            }
system("pause>nul");
return 0;
}

i cant see the get the initial if statement to work no matter what number i type in it would auto display the out of number range. also am i allowed to place the conditions like that soo close like if (x < 0)(x > 100);. also how do i make it soo it returns to the start of the program?

like image 994
sickist Avatar asked Nov 24 '25 08:11

sickist


2 Answers

There is an error:

if (x < 0)(x > 100);
{
    cout<<"out of number range";
}

Should be:

if (x < 0 || x > 100)
{
    cout<<"out of number range";
}

You also need to work on your indentation; those if/else statements towards the bottom look dodgy (I cannot really tell due to the indentation).

like image 82
trojanfoe Avatar answered Nov 25 '25 21:11

trojanfoe


Aside from writing if (x < 0 || x > 100) (and dropping the semicolon), you should be wary of comparing equality on floating point. I would red flag your line if (x == y){ if reviewing your code.

See Floating point comparison

like image 27
Bathsheba Avatar answered Nov 25 '25 21:11

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!