Adblock breaks this site

Novice C++ question

Discussion in 'Programming General' started by distracted, Sep 24, 2011.

  1. distracted

    distracted Active Member

    Joined:
    Jun 26, 2009
    Posts:
    127
    Referrals:
    0
    Sythe Gold:
    0
    Novice C++ question

    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.
     
  2. blindkilla

    blindkilla Guru
    $25 USD Donor New

    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord
    Novice C++ question

    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.
     
  3. spots13

    spots13 Active Member
    $5 USD Donor New

    Joined:
    Feb 29, 2008
    Posts:
    213
    Referrals:
    0
    Sythe Gold:
    0
    Novice C++ question

    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
     
  4. distracted

    distracted Active Member

    Joined:
    Jun 26, 2009
    Posts:
    127
    Referrals:
    0
    Sythe Gold:
    0
    Novice C++ question

    Thanks. I thought my mistake was a bug as opposed to a syntax error.
     
< Need Runescape Forum for Dicing | SendMessage in vb.net? >


 
 
Adblock breaks this site