I have the following code (as you will see im using the same template as earlier...:$

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    int z = 1;
    int x = 2;
    int y = 3;
    bool b; 
    b = (x < y) && (x > z);
    cout << b;
    system("PAUSE");
    return 0;
}

When you run this code it returns 1. Im wondering why? I thought it would return the value of x since x is in both statements and yet it doesnt.

>I thought it would return the value of x since x is in both statements and yet it doesnt.
Interesting reasoning. The reason it doesn't return x is because a boolean expression will always result in a boolean value (true or false).

Your code returns 0 (see the last statement of main ;)).
It PRINTS 1. Logical expression value is true, true printed as 1 (false as 0).
What's a problem? You want to print bool value - that's the value printed.
If you want to print x - do that!

By tradition the two arguments to main() are declared as argc and argv -- you can name them anything you wish but custom dictates those two names int main(int argc, char* argv[]) how do you know your program returns 1? The code you posted returns 0, not 1 (see the return statement).

>> thought it would return the value of x
why? x is not the return value (last line of the program). [edit]Yes, I realize this is not really what you meant, but when writing programs you have to be very specific about questions to avoid ambiguity and confusion [/edit]

And you don't need these two lines :)

#include <cstdio>
#include <cstdlib>

I wouldn't recommend writing always the same, although it's the easiest way

And one thing more. I realise you are just starting in C++, but try not to use system("PAUSE"); Just enter this instead of that: cin.get(); In that way you won't be able to press "any key", you will have to press enter, but for starting programs it shouldn't bother you. And in that way the code is much more standard.
HTH

This article has been dead for over six months. Start a new discussion instead.