• May 20, 2012, 07:11:12 PM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Lesson #2  (Read 177 times)

Vega

  • Fear is my Ally...
  • Command General
  • Mack Daddy
  • *
  • Posts: 1,320
    • View Profile
Lesson #2
« on: October 02, 2010, 10:33:33 PM »

//Lesson #2 Declaring integer's and initialize a zero.

#include <iostream>      //add function definitions for input and output

int main()            //the curly brace marks the start of the function's body
{
   int footBalls = 0;      //declare an integer varible, initial to zero
   footBalls = 10;

   std::cout<<std::endl;
   std::cout<< "Our Team has " //notice how this statement spans multiple lines
      "only " <<footBalls << " balls "
      << std::endl << "for" <<
      " practice. ";
   std::cout << "No more... No Less." << std::endl;
   std::cout << "The End." << std::endl;
system("pause");

   return 0;
}
// end of code

In this example, you probably noticed that the "using namespace sts;" was left out.
Instead of using this statement, the example uses the scope resolution operator ( :: ),
a double colon, to designate the namespace of the cout command. Either method is
acceptable, but the safest way to ensure that there are no conflicts between the
functions you create and the function in a library you are using is to use the scope
resolution operator. If you use the the namespace 'using' statement, you risk having
conflicts. Also notice how string literals must have a closing quotation mark
if they span more than one line (std::cout << "Our team has"...).
Logged

Adam

  • First Lieutenant
  • Wanker
  • *
  • Posts: 278
    • View Profile
Re: Lesson #2
« Reply #1 on: October 03, 2010, 03:18:01 PM »

theres way too many std's in that code. i hope you were wearing a condom when you wrote that
Logged

williebonney

  • Claymore Magnet
  • Tactical General
  • Mack Daddy
  • *
  • Posts: 1,196
  • I'm all out of bubblegum
    • View Profile
    • Total Eclipse
Re: Lesson #2
« Reply #2 on: October 03, 2010, 07:59:04 PM »

Here is the comparable java code.  It's STD free.  I didn't test it though so there may be a mistake or two in there.


public static void main(String[] args)
{
  int footBalls = 0;
  footBalls = 10;
  String lineSeparator = System.getProperty("line.separator"); // creates a platform independent new line

  System.out.println(lineSeparator );
  System.out.println("Our Team has ");
  System.out.println("only" + footBalls + "balls" + lineSeparator + "for practice");
  System.out.println("No more... No Less." + lineSeparator);
  System.out.println("The End." + lineSeparator);
}
Logged
King of using little guns that shouldn't do what they do and finding stray claymores

Vega

  • Fear is my Ally...
  • Command General
  • Mack Daddy
  • *
  • Posts: 1,320
    • View Profile
Re: Lesson #2
« Reply #3 on: October 03, 2010, 09:48:17 PM »

So the "System.out.println" does the basic function as cout. What about if you want it to ask for a users input Willie? In c++ its a simple cin command? like so...

std::cout<< "Enter your Name please?";
cin >> theName;

Thats as easy as it gets when asking the user for information you want to store as a string? Is it the same in java? And if I am hosting C++ lessons, I am going to make a new one for you titled Java Lessons, so start teaching me some basics lol..
Logged