Orbiter-Forum  

Go Back   Orbiter-Forum > Blogs > mojoey
Register Blogs Orbinauts List Social Groups FAQ Projects Mark Forums Read

Notices

Rate this Entry

My First C++ endeavour Day 1

Posted 02-01-2012 at 08:04 PM by mojoey
Updated 02-01-2012 at 11:53 PM by mojoey

So, recently I decided to learn C++ and found a nice tutorial on the basics of C++ so, below, you can see my progress. I will be posting these semi-regularly.

Code:
// my first program in C++

#include <isostream>;
using namespace std;

int main ()
{
cout << "Hello World!";
cout << "I'm a C++ program!";
cout << "Want to be friends?";
return 0;
}
fairly basic I know...

then i did this

Code:
//basic variable code and stuff
#include <isostream>;
 using namespace std; 
int main () 
{ 
int a, b; 
int result;
 a = 5;
 b = 2;
 a = a + 1;
 result = a - b;
 cout << result;
 return 0;
 }
in this few lines i told the program to compute 5+1+2=? a being 5 b being 2, then i told it to use a, add one to it, then subtract the result of that to variable b. then i told it to display the result in a simple text line.


so, after a bit, i got onto strings and stuff, and this is an example of string that i typed up

Code:
// my first string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystring;
mystring = "This is the first string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
a simple string code, moving on!

thats all for now, this blog will be updated throughout the day with new experiences for me
Posted in Uncategorized
Views 2092 Comments 14
« Prev     Main     Next »
Total Comments 14

Comments

  1. Old Comment
    Samuel Edwards's Avatar
    Good Job. I know the tutorial you are using.
    Posted 02-01-2012 at 08:34 PM by Samuel Edwards Samuel Edwards is offline
  2. Old Comment
    Scruce's Avatar
    As a follow on, try using the 'DEFINE' operation for your result.

    Below:
    Code:
    Using namespace std;
    Write:
    Code:
    #DEFINE result a - b
    Then delete this line from the main function:
    Code:
    result = a - b;
    Posted 02-01-2012 at 08:41 PM by Scruce Scruce is online now
    Updated 02-01-2012 at 08:43 PM by Scruce
  3. Old Comment
    Izack's Avatar
    Why is main always given as an integer returning zero? Forgive my ignorance, but why not declare it as a void?
    Code:
    #include <iostream>
    void main()
    {
      cout << "Wazzap?";
    }
    Posted 02-01-2012 at 11:51 PM by Izack Izack is offline
  4. Old Comment
    mojoey's Avatar
    thats the way the tutorial laid it out, and i went with that.
    Posted 02-02-2012 at 12:07 AM by mojoey mojoey is offline
  5. Old Comment
    PeriapsisPrograde's Avatar
    void mains are technically incorrect. A return of 0 indicates proper termination.
    Posted 02-02-2012 at 03:42 AM by PeriapsisPrograde PeriapsisPrograde is offline
  6. Old Comment
    mojoey's Avatar
    'This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.'

    thats what the tutorial said
    Posted 02-02-2012 at 03:55 AM by mojoey mojoey is offline
  7. Old Comment
    Izack's Avatar
    Hmm, they're encouraged in Java...I'm not really used to C.

    Edit: Yeah, all apps require a main method to run, that's a common convention. I don't think there's anything about them having to return anything, though. I can't think of an obvious reason main() would need to return anything, anyway. As it is, if you declare a method to return an integer [int main()] and it always returns zero, it doesn't need to return anything. void main() means it doesn't return anything, so no return statement is needed. But whatever, it's still best to follow the tutorial.

    For what it's worth, all of my Java programs begin with a " public static void main() "
    Posted 02-02-2012 at 04:00 AM by Izack Izack is offline
    Updated 02-02-2012 at 04:06 AM by Izack
  8. Old Comment
    mojoey's Avatar
    the 'return 0;' line terminates the process started under int main () i think
    Posted 02-02-2012 at 04:14 AM by mojoey mojoey is offline
  9. Old Comment
    Izack's Avatar
    No, the "}" at the end terminates it. the 'return' line means that the function will return whatever is after it, in this case a zero. Usually you'd see some variable being manipulated and returned. Say in your program up there, you could do this:
    Code:
    int myMethod () 
    { 
    int a, b; 
    int result;
     a = 5;
     b = 2;
     a = a + 1;
     result = a - b;
     return result;
    }
    and then you'd be able to specify somewhere else:
    Code:
    void main()
    {
     int i = myMethod();
     cout << i;
    }
    Do you see what I mean here?
    Posted 02-02-2012 at 04:25 AM by Izack Izack is offline
  10. Old Comment
    orb's Avatar
    Quote:
    Originally Posted by Izack View Comment
    No, the "}" at the end terminates it. the 'return' line means that the function will return whatever is after it, in this case a zero. Usually you'd see some variable being manipulated and returned.
    The "return some_int_value" called from 'main' doesn't directly terminate the process (nor the closing parenthesis of the 'main' function), but returns execution to the CRT code (added by the linker) which called the 'main' function at the beginning, after initializing all the internal C/C++ runtime stuff.

    The returned value is used as of the program, which is passed by CRT to either 'ExitProcess' WinAPI function in Windows, '_exit' kernel call in Linux/Unix, '0x4C' function of '0x21' interrupt in DOS, or other system function that terminates the process. The status code can be then used by calling program or script (batch) to decide what to do next (for example if the called program was non-interactive). Returning 0 usually means that the program finished its execution without errors (where errors are not unhandled exceptions, but program's internal errors, for example like "no argument passed", "file not found", etc).

    At least Windows, DOS, and Unix/Linux use integer exit status codes, and the same such argument for program termination functions, so that's why the 'main' is of int type.
    Posted 02-02-2012 at 06:48 PM by orb orb is online now
  11. Old Comment
    MeDiCS's Avatar
    I guess some clarifications are needed. First, while 'void main()' may be acceped by some compilers, it's explicitly forbidden by the C standard. Plus, besides the fact that main() may be declared with zero or two arguments (or even more, depending on the platform), it is otherwise like any other function.

    Conceptually, the main function is called by the OS when the program is started, thus returning from it is the same as exiting the program (the same as calling exit()). The closing bracked ('}') is simply a token marking the end of the function definition in the source code, and is not the same as returning from it. This may be a bit confusing, since, if the code compiles, the compiler must add a 'return 0' at the end of the function. *

    The value returned from the main function is actually used and does have a meaning in some contexts. A 0 means that everything went well, and any other value implies that something went wrong. In some cases, the value is used to tell exactly what made the program fail. xargs, for example, is a command-line utility used in Unix-like systems which returns different values depending on the specific error.

    This value is not actually used by the user (unless you're in the command-line or using a debugger), but mostly by other programs or scripts. For example, a script may use it to decide if a command needs to be retried, or even if it can be retried.

    In Java, the main function does not return anything since the program implicitly returns 0 when all threads die. This means a Java app automatically quits when main() ends and you did not meddled with multithreading. However, System.exit() may be called directly if you actually need to return an exit code.

    Two wiki articles which may be useful: and


    * But even if it works, please don't do this

    EDIT:

    EDIT2: In the first two examples, you typed 'isostream' instead of 'iostream'
    Posted 02-02-2012 at 07:14 PM by MeDiCS MeDiCS is offline
    Updated 02-02-2012 at 07:20 PM by MeDiCS (obs)
  12. Old Comment
    Izack's Avatar
    Oh.

    Last time I assume Java and C have something in common, right there.
    Posted 02-02-2012 at 08:12 PM by Izack Izack is offline
  13. Old Comment
    MeDiCS's Avatar
    Quote:
    Originally Posted by Izack View Comment
    Oh.

    Last time I assume Java and C have something in common, right there.
    Well, they both do return an int to the OS, but Java does that behind the programmer's back.
    Posted 02-02-2012 at 11:04 PM by MeDiCS MeDiCS is offline
  14. Old Comment
    Izack's Avatar
    Quote:
    Originally Posted by MeDiCS View Comment
    Java does that behind the programmer's back.
    Java does a lot of things behind the programmer's back, it would seem.
    Posted 02-03-2012 at 01:26 AM by Izack Izack is offline
 

All times are GMT. The time now is 01:48 PM.

Quick Links Need Help?


About Us | Rules & Guidelines | TOS Policy | Privacy Policy

Orbiter-Forum is hosted at Orbithangar.com
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 2007 - 2012, Orbiter-Forum.com. All rights reserved.