HTML Help
HTML Help Central . com

» Online Users: 102

0 members and 102 guests
No Members online
Most users ever online was 695, 02-16-2011 at 07:01 AM.

» Site Navigation

 > FAQ

» Domains For Sale

Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    1,940

    C++: Tutorial 1 [cout, cin]

    Ok, I'm not sure if there is at all a want for tutorials on C++ but you must understand I'm extremely bored , and all of my friends went skateboarding and due to my injury I couldn't hang out with them . So anyway here we go...

    Welcome to C++: Tutorial 1, in this tutorial we will learn the basics of console programming. We will learn how to write text onto the screen using the "cout" function. We will also learn how to take input from the user using the "cin" function.

    Ok, so open up your compiler (Iíll be writing this tutorial using Microsoft Visual C++, but you can use any compiler) and begin by including the library (header file) that holds these functions.
    Code:
    #include <iostream.h>
    #include tells the compiler to include whatever header file follows it. The iostream.h header file includes the standard functions that we will be using today, later on you will being including other header files but for now this will do.

    The next segment of code opens of the main integer of the code.
    Code:
    int main()
    Don't spend to much time worrying about this code right now if you don't understand what I mean by "integer". It simply means that it returns a value, but it will become clearer later on in C++. For now just know that you need to have this code in your C++.

    The next line,
    Code:
    {
    Is fairly easy to remember you can call this the "opening tag" if you please. Similar to HTML you need to open and close certain things this is the opening and later we will close bracket.

    Now we finally get to the actual syntax to write something on the screen.
    Code:
    	cout<<"Hello World! I wrote this with C++"<<endl;
    cout<< is the standard output code in C++. It will output whatever is after it in quotes. You close the Output with <<. Then "endl" ends the line, you don't have to end the line, you can also write "flush", that will allow you to write more stuff on that line. And finally as always in C++ end the code with a semi-colon ";".

    The next line we add is to return something for the integer.
    Code:
    	return 0;
    All functions have to return something. return 0 means that the program will quit. Don't forget the semi-colon!

    The final line of the code is the closing bracket.
    Code:
    }
    Any opening bracket must have a closing bracket, here it is.

    Here is the complet code,
    Code:
    #include <iostream.h>	
    
    int main()
    {
    	cout<<"Hello World! I wrote this with C++"<<endl;
    	return 0;
    }
    One thing, C++ is case sensitive, that means that you can't right "cout" "CoUt" it just won't work .

    When you compile this program it will write "Hello World! I wrote this with C++" on the screen. There you go that wasn't hard huh?

    Ok now onto taking input from the user.

    We'll begin the code just like the last one,
    Code:
    #include <iostream.h>	
    
    int main()
    {
    Now we'll add something new, we are going to declare a variable to hold the users input.
    Code:
    	int number;
    There are many ways to declare a variable, this type of variable can only hold a number though.

    Next we'll declare a variable that can hold a letter.
    Code:
    	char letter;
    This will hold one letter the user inputs, possibly in a later tutorial I will explain how to store more then one letter. There are a few other ways to declare variables but you don't need to worry about them now.

    Ok the next line we'll just ask the user to input a number.
    Code:
    	cout<<"Please enter a number: "<<flush;
    You already knew how to write this though.

    Now we'll store the number the user inputted.
    Code:
    	cin>>number;
    cin>> function is used to get the input and then it is stored as "number" the variable that we created before. Notice the semi-colon on this line, almost all lines within the main() code end with semi-colons.

    Next we'll ask the user to input a letter.
    Code:
    	cout<<"Please enter a letter: "<<flush;
    Again you already know how to do this.

    And now we'll take the user-inputted letter.
    Code:
    	cin>>letter;
    You should know how to do this .

    Next we'll display the inputted number and letter just to show it worked.

    Code:
    	cout<<"Your number, "<<number<<" your letter "<<letter<<endl;

    The cout function can display variables as well as text you give it. As you see the variables don't have quotes around them. You also must close the output stream in order to write them onscreen (if this make since).

    Finally we'll end the code with the code you learn in the first half.
    Code:
    	return 0;
    }
    And the complete code...

    Code:
    #include <iostream.h>	
    
    int main()
    {
    	int number;
    	char letter;
    	cout<<"Please enter a number: "<<flush;
    	cin>>number;
    	cout<<"Please enter a letter: "<<flush;
    	cin>>letter;
    	cout<<"Your number, "<<number<<" your letter "<<letter<<endl;
    	return 0;
    }

    There you have it, you've learned the basics of C++, your a programmer now . I hope you liked the tutorial. I know its long but if you got to this part I commend you.

    Like always if you have any questions or comments please post them.

    -Element

    P.S.- Sorry if this tutorial was long and boring but it took about an hour to write and it took my mind off my friggen collar bone for a little.
    Last edited by element; 06-01-2002 at 07:24 PM.

  2. #2
    Join Date
    Feb 2001
    Location
    Stockholm, Sweden
    Posts
    1,483
    The C++ main method is actually an integer?! Wow. I'll be damned. Quite logical though when you think about it. In Java you don't really specify it, main is a world of its own.
    :elin: HamsterBox.com finished. And stop accusing me of being serious!

    - What is your one purpouse in life?
    - To explode of course!

  3. #3
    Join Date
    Dec 2000
    Location
    Wales, UK
    Posts
    1,780
    Very posh. I'll go back to my qbasic
    Neil Graddage
    Supermod
    All round good egg
    :welsh:Proud to be welsh!:welsh:

    I am currently playing: Pharoh
    I am currently listening to: Fleetwood Mac

    Mail me: neilg AT htmlhelpcentral.com

  4. #4
    Join Date
    Jul 2002
    Location
    NJ, US
    Posts
    141
    are u sure u need to specify int? I think it works without it too.

  5. #5
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    1,940
    Originally posted by dwarrior
    are u sure u need to specify int? I think it works without it too.
    Well you might be able to get away with not defining the type of variable in your code, however it is very bad practice! You should always define the type of variables you use.

  6. #6
    Join Date
    May 2001
    Location
    Austin
    Posts
    428
    I almost always define mine as void, no need to really return a type for main. You can end void programs with just return; so, there aren't any dissadvantages there.
    -Jonathan
    [i]To err is human. To blame it on a computer is even more so. [i]

  7. #7
    noctec Guest

    Smile

    you can always use:

    void main()

    and not return anything.

    also, if you're just clicking on your compiled program in windows you'll probably want to put getch() at the bottom.

Similar Threads

  1. Looking for a tutorial
    By mrickert in forum Miscellaneous
    Replies: 2
    Last Post: 09-13-2005, 04:12 PM
  2. php tutorial
    By decka in forum Tutorials, Tips & Tricks
    Replies: 13
    Last Post: 01-27-2004, 02:48 AM
  3. C++: Tutorial 3 [for, while, do while]
    By element in forum Tutorials, Tips & Tricks
    Replies: 5
    Last Post: 08-14-2002, 01:56 PM
  4. C++ Tutorial
    By JonathanATX in forum Tutorials, Tips & Tricks
    Replies: 3
    Last Post: 04-06-2002, 10:42 PM
  5. Javascript tutorial
    By Kougar in forum Javascript / AJAX / VBScript
    Replies: 2
    Last Post: 07-28-2001, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Powered by vBadvanced CMPS v4.1.1