Novice C++ question

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

Novice C++ question
  1. Unread #1 - Sep 24, 2011 at 4:26 PM
  2. distracted
    Joined:
    Jun 26, 2009
    Posts:
    127
    Referrals:
    0
    Sythe Gold:
    0

    distracted Active Member

    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.
     
  3. Unread #2 - Sep 24, 2011 at 4:30 PM
  4. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6
    Discord Unique ID:
    282000633404456960
    Discord Username:
    sogord

    blindkilla Guru
    $25 USD Donor New

    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.
     
  5. Unread #3 - Sep 25, 2011 at 2:02 AM
  6. spots13
    Joined:
    Feb 29, 2008
    Posts:
    213
    Referrals:
    0
    Sythe Gold:
    0

    spots13 Active Member
    $5 USD Donor New

    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
     
  7. Unread #4 - Sep 25, 2011 at 4:28 AM
  8. distracted
    Joined:
    Jun 26, 2009
    Posts:
    127
    Referrals:
    0
    Sythe Gold:
    0

    distracted Active Member

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

Users viewing this thread
1 guest


 
 
Adblock breaks this site