Beginners help: if statements


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Beginners help: if statements

  1. #1
    Join Date
    Apr 2004
    Posts
    136

    Beginners help

    Ive been practicing some C++ lately and I cant seem to see why this dont work....I believe theres maybe some ; missing in the if statement ?
    As u can tell from the code im a total newby to this...so any help is appreciated =)

    Code:
    int Main()
    
    {
      int A,B;
    
      cout << "Enter two numbers \n";
    		cin >> A;
    	                cin >> B;
    
    		if A > B
    			cout << "Your first number was bigger \n"
    		else
    			cout << "\n Your second number was bigger \n"
    
    		return 0;
    
    }
    Last edited by process; 06-20-2004 at 04:24 PM.

  2. #2
    Join Date
    May 2004
    Location
    calif
    Posts
    53

    Re: Beginners help

    Originally posted by process
    Ive been practicing some C++ lately and I cant seem to see why this dont work....I believe theres maybe some ; missing in the if statement ?
    As u can tell from the code im a total newby to this...so any help is appreciated =)

    Code:
    int Main()
    
    {
      int A,B;
    
      cout << "Enter two numbers \n";
    		cin >> A;
    	                cin >> B;
    
    		if A > B
    			cout << "Your first number was bigger \n"
    		else
    			cout << "\n Your second number was bigger \n"
    
    		return 0;
    
    }
    make sure you put the ; and the end of your expessions. and also the if should be like this
    Code:
    int main()
    {
     int A, B;
    
    cout << "Enter two numbers: ";
    cin >> A >> B;
    
    if( A > B )
       cout << "Your first number is bigger." << endl;
    else
       cout << "Your second number is bigger." << endl;
    
    return 0;
    }
    hope this helps
    Last edited by ben; 06-20-2004 at 06:27 PM.
    __________________
    Thks! Ben

  3. #3
    Join Date
    Jun 2004
    Location
    canada
    Posts
    27
    make sure you understand that the if statement syntax for c++ is:

    if (conditional) // either true or false only
    { // execute statements if conditional was true
    statements ; // always end a statement with a semicolon
    } // end of block for if statements
    the virtuous lead with their minds.

  4. #4
    Join Date
    Apr 2004
    Posts
    136
    so also the if statements are contained within a block like

    Code:
    if( A > B )
       {
       cout << "Your first number is bigger." << endl;
    else
       cout << "Your second number is bigger." << endl;
       }
    return 0;
    }
    :confused:

  5. #5
    Join Date
    Feb 2004
    Location
    Indianapolis, IN
    Posts
    424
    Originally posted by process


    so also the if statements are contained within a block like

    //..... I assume some code is here that starts a function....
    if( A > B )
    {
    cout << "Your first number is bigger." << endl;

    } // Closing the true part of the if....
    else
    { // starting new block for else (false part of if)....
    cout << "Your second number is bigger." << endl;
    }
    return 0;
    }


    Because the blocks for this if....else statement are one line, you could also have done:


    if( A > B )
    cout << "Your first number is bigger." << endl;
    else
    cout << "Your second number is bigger." << endl;

    return 0;
    }



    Note that the above doesn't show indenting
    -------------------------------------------------
    Bradley L. Jones
    Author, and more.
    QuinStreet / Internet.com
    (Developer.com, CodeGuru.com, DevX, jGuru, and more!)

    -------------------------------------------------

  6. #6
    Join Date
    Apr 2004
    Posts
    136
    thx for all ur help chaps...seems more clear now :cool:

  7. #7
    Join Date
    May 2004
    Posts
    12
    What is the "endl" after each statement? I thought the semi-colon was enough to end a statment. But you guys put << endl;??? what's this for? :confused:

  8. #8
    Join Date
    Apr 2004
    Posts
    136
    signals a new line

    endl
    endline

  9. #9
    Join Date
    May 2004
    Posts
    12
    What's wrong with using \n for signalling a new line? Is "endl" just an alternative?

  10. #10
    Join Date
    May 2004
    Location
    calif
    Posts
    53
    Originally posted by Blade195
    What is the "endl" after each statement? I thought the semi-colon was enough to end a statment. But you guys put << endl;??? what's this for? :confused:
    If you want to have the user input data right after the output as in cout << "please enter your name"; <- leaves cursor on the current line;

    Doing it this way cout << "Please enter your name " << endl;
    moves the cursor to a new line below "Please enter your name".

    I thought I read somewhere that endl clears the buffer where in "\n" does not. Feel free to correct me if im wrong.
    __________________
    Thks! Ben

  11. #11
    Join Date
    Nov 2003
    Posts
    4,118
    endl does two things: i flushes the stream, which means forcing output to the screen without delay, and it moves it to the next line.
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center