• Log in or sign up for an account here: https://www.sythe.org/login

Novice C++ question

distracted

Active Member
Joined
Jun 26, 2009
Sythe Gold
0
Messages
127
I began reading a PDF version of C++ Primer-Fourth Edition and am stuck on an exercise.

1) Write a program to print the larger of two inputs supplied by
the user.

This is what I have so far. Please clarify what I am doing wrong; don't simply post the correct code.

Code:
 #include <iostream>

using namespace std;

int main()
{
    int x, y;
    cout << "Enter two numbers to determine which one is greater";
    cin >> x >> y;

    if (x > y)
{

        cout << x "is the greater variable" ;
}
    else
    {


    cout << y "is the greater variable" ;
    }
    return 0;

}


Thanks.
 
With c++ (and other languages) for each variable/string you want to output, you must have a concatenation operator between them.

With c++ it's "<<" for outputs. With PHP it's ".", with C# it's "+".

Your code is correct, just the line which outputs the text has one small error.

Code:
if (x > y)
{
    cout << x [U][B]<<[/B][/U] "is the greater variable" ;
}
else
{
    cout << y [U][B]<<[/B][/U] "is the greater variable" ;
}
I've bolded and underlined the code which would fix it.
 
totally correct blinkilla, btw if you wanna check my c++ guide and tell me what you think of it would be gr8 will be updating it soon again
 
Thanks. I thought my mistake was a bug as opposed to a syntax error.
 
Runescape Gamblingrunescape gambling
Back
Top