Coding Help

WackyJacky

Head Gardener
Joined
Dec 14, 2007
Messages
274
Location
USA
Was messing around with a program we wrote in class. Trying to write it in C++, with a bit of added functionality. However I keep getting the wrong output. Any help? (even if you don't know C++ it's simple enough that you can probably figure out what's wrong.)

Code:
#include <iostream>

using namespace std;

int main()
{
    int a, b, c;

    cout << "enter a: ";
    cin >> a;
    cout << "enter b: ";
    cin >> b;
    cout << "enter c: ";
    cin >> c;

    if (a <= b)
    {
        if (a=b)
        {
            cout << a << " is equal to " << b << endl;
        }

        if (a<b)
        {
            cout << a << " is less than " << b << endl;
        }

    }

    if (b <= c)
    {
        if (b=c)
        {
            cout << b << " is equal to " << c << endl;
        }

        if (b<c)
        {
            cout << b << " is less than " << c << endl;
        }
    }

    else
    {
        cout << a << " was not less than or equal to " << b
             << " which was not less than or equal to " << c << endl;

    }

    return 0;
}

Output that it returns:

enter a: 1
enter b: 1
enter c: 3
1 is equal to 1
3 is equal to 3
 

Weeble

Community Manager
Administrator
Joined
Dec 13, 2007
Messages
869
Location
UK
One glaring error is that when you're 'comparing' equality between a/b and b/c you're actually re-assigning them.
a=b should be a==b. Same with b=c.
Currently, the program is effectively calculating whether b is the same as b and whether c is the same as c. Which they are.

C++ is fairly different to C :p
 

LAFiN

Tree Surgeon
Joined
Dec 15, 2007
Messages
746
You should also be using ELSE IF statements instead of multiple IFs inside the main IF statements.
 

Weeble

Community Manager
Administrator
Joined
Dec 13, 2007
Messages
869
Location
UK
I'd probably do something similar to this:

Code:
#include <iostream>

using namespace std;

int main()
{
    int a, b, c;

    cout << "enter a: ";
    cin >> a;
    cout << "enter b: ";
    cin >> b;
    cout << "enter c: ";
    cin >> c;

    if ( a <= b || a <= c )
    {
        if ( a <= b )
            cout << a << (( a == b ) ? " is equal to " : " is less than ") << b << endl;
        if ( b <= c )
            cout << b << (( b == c ) ? " is equal to " : " is less than ") << c << endl;
    }
    else
    {
        cout << a << " was not less than or equal to " << b
             << " which was not less than or equal to " << c << endl;
    }

    return 0;
}
 
Last edited:

Azzer

Administrator
Staff member
Administrator
Joined
Dec 13, 2007
Messages
1,215
Though Weeble, if a was 2 and b was 7 and c was 5, your output would be:

"2 was not less than or equal to 7 which was not less than or equal to 5".

Which makes no sense :p

(for WackyJacky's sake, because the first part of Weeble's if statement "a<=b" was true but "b<=c" was false, which made the if statement default to the "else" which had no clauses)
 

WackyJacky

Head Gardener
Joined
Dec 14, 2007
Messages
274
Location
USA
Weeble - I tried to run your new version and got a runtime error:

syntax error near unexpected token `('

If it's working for you it may be because I'm on a Mac? Though I don't see anything in the code that looks like "windows only."
 

Weeble

Community Manager
Administrator
Joined
Dec 13, 2007
Messages
869
Location
UK
If you want to keep it 'readable' then just add two new bool variables: PlayerATurn and PlayerBTurn. Initialise them with PlayerATurn = True and PlayerBTurn = False then switch them every time a (valid) move is made. Obviously you'll have to check that that player can actually make a move whenever their key is pressed first, else it'll just ignore the variables.
 

Lupus

Head Gardener
Joined
Dec 14, 2007
Messages
279
lulz my uni work is following me!!

(doing beginner java programming as a piss about module to make up credits for my chem degree)
 
Top