2
#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?

sickist
  • 89
  • 6
  • Put the thing in a while(true) loop – OldProgrammer Jul 03 '13 at 15:00
  • 3
    `if (x<0)(x>100);` is equivalent to `if (x<0) { (void)(x>100); }`. The following compound statement is not related to the `if`. – aschepler Jul 03 '13 at 15:01
  • everyone here has explained why the if doesnt work as expected. I'll answer the part about the loop. You don't want to go back to the beginning of the program, you just want to go back to asking for the number again. use a `do while` loop (see below) – Russell Uhl Jul 03 '13 at 15:07
  • 1
    @abelenky, are you sure? http://ideone.com/gZCxim – Mark Ransom Jul 03 '13 at 15:24
  • @MarkRansom: Wow, I'm truly surprised that compiles. It looks *so* wrong. Point taken, earlier comment deleted. – abelenky Jul 03 '13 at 15:44

6 Answers6

6

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

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • better (just nitpicking): `if (x < 0 or x > 100)` – utnapistim Jul 03 '13 at 15:00
  • 2
    @utnapistim, In your opinion, maybe. Everyone who knows C, C++, Java, C#, etc knows what || does. Unfortunately, `or` just tends to make people think it's some non-standard extension or something. – chris Jul 03 '13 at 15:06
  • @chris is right. || is well known in C++. `or` is only used by people abusing C++, trying to make it look like something else. – abelenky Jul 03 '13 at 15:08
4

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

Community
  • 1
  • 1
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

nobody else is actually answering your second question: how to loop it, here you go:

int x;
cout << "Welcome to the guessing game\n";
do {
    cout << "Please enter a number from 0 to 100: ";
    cin >> x;
} while (x < 0 || x > 100);
Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
2

You have written

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

First remove the semi colon. Second did you mean

if ((x < 0) || (x > 100))
{
    cout<<"out of number range";
}
doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • 1
    I don't think they meant to return 0, otherwise the 2nd player would never get an opportunity to guess, let alone the first player getting another shot at guessing correctly without restarting the entire program. – Ricky Mutschlechner Jul 03 '13 at 15:06
  • I was trying to deal with two problems at a time, not four ;-) I'd rather fuss about the code alignment than the extra brackets, TBH. – doctorlove Jul 03 '13 at 16:50
1

try this:

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

if (x < 0 || x > 100)
{
    cout<<"out of number range";
}
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
0

There are a few notable syntax errors:

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

First of all, you can't just put two conditions side by side like that in C++ that I know of. You'd have to separate them with || for OR, or && for AND (in most cases - there are some others).

Also, you had a ; at the end of your if statement. I believe that doing this in C++ will result in some problems too.

Your final code should look like:

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

The << endl; part is optional. This adds a new line to your output, for more readability next time you write something.

Also, to loop your entire game repeatedly, I would use a do-while loop. You can learn about them here.

Ricky Mutschlechner
  • 4,291
  • 2
  • 31
  • 36