Stuyvesant Computer Programming Club
Data Types
Home
Lessons
Topics Covered
Club Policies
Links
Contact Information
Downloads

All about Cout, Cin, and Data Types

In the last lesson, you learned how to start a program, compile it, and run it. Now, time to learn some basic C++ programming. First lets analyse the code from the previous section:
#include <iostream.h>
#include <iomanip.h>
int main()
{
    cout<<"Hello World"<<endl;
    return 0;
}
The #include statements are library files. Cout, endl, (and later cin) are functions and you have to have the first two lines in every basic C++ program. Function will be discussed in the next lesson. Libraries will be discussed at a later time. If you want to know about it now, feel free to ask during the club meetings.
int main()
{
}
is the main function. Everything between the { and } will be done first in every C++ program. main will be discussed in detail later too, right now, just know that you need it there.
Now, the next thing is the cout<<"Hello World"<<endl;
cout is a function that prints whatever is after it onto the computer screen. It is similar to the print statements in java and python. "Hello World" is a string, or something like a sentence. Strings have to be in "". endl is a stream manipulator. It addes a new line character (like when you press enter on a keyboard).
The return 0; statement is one of those things that are always needed as well, so just put that there for now. Also notice that at the end of each line, there is a ; You need that there at the end of each C++ line (except the includes and functions), like in java. C++ is also case-sensitive, which means if you wrote int Main() or Cout<<"Hello World"<<endl; it would not work.
NOTE IF YOU ARE USING THE NEW DEV-CPP (5.0): Dev-C++ will report a few warnings if you use "antiquated headers" such as <iostream.h> or <iomanip.h>. You can ignore these warnings and the program will still run. (but if you have an error, it will not compile)

Data Types:
For now, heres are some basic data types.
int               an integer. There is no decimals. It includes numbers such as 0, 1, 2, 3
double    a float, or basically any number. Such as 5.1, 7.0, or 15.532
char      a character, such as 'a', 'b', 'y', 'z'. Note characters have to be between ' and '
string    a string, something like "Hello World". (Also note that in dev-cpp, strings are red)
 
Cin:
Here is a sample code:
#include <iomanip.h>
#include <iostream.h>
int main()
{
    int x, z;
    int y;
    cout<<"Enter two integers: ";
    cin>>x>>y;
    //this is a comment.
    //Everything i type after these two /s will not be compiled
    z = 5;
    cout<<"x = "<<x<<" y = "<<y<<" z = "<<z<<endl;
    double a;
    cout<<"Enter"<<" a "<<"decimal integer: ";
    cin >> a;
    cout<<"a = "<<a<<endl;
    char c;
    cout<<"Enter a character: ";
    cin >> c;
    cout<<"c = "<<c<<endl;
    return 0;
}
To use a data type, you must declare it (like in line 5, 9, 13). You give it a value (like with int z) or let the user input the value with the function cin.
C++ supports chaining. There are many examples of chaining in this program.
cout<<"x = "<<x<<" y = "<<y<<" z = "<<z<<endl;
could have been written as
cout<<"x = ";
cout<<x;
cout<<" y = ";
cout<<y;
cout<<" z = ";
cout<<z;
cout<<endl;
but programmer (on average) are lazy and wont do something like that.
Also note that a statement such as cout<<z; will output the value of z;
Similarly, int x, z; can be written as
int x;
int z;
Looking at the declaration int x; This is a variable (as opposed to constants). This means that you can change the value of x. The int in front of it means that it is a variable integer. x is a variable name, or identifier. You can call it whatever you like, for example: int first_integer or int hello3 or something. But the variable name must start with a character and it cannot contain special characters, such  '#', '%', '*', ' ' (a space), etc. For example, int first integer, int 3hello, or x# are illegal integer declarations.

Another rule is that your identifier can not match any key word of the C++ language nor your compiler's specific ones since they could be confused with these. For example, the following expressions are always considered key words according to the ANSI-C++ standard and therefore they must not be used as identifiers:

asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t
//this is a comment.
//Everything i type after these two /s will not be compiled
like it says, this is a comment. Sometimes programmers like to leave comments in their code so that the reader can follow his/her code better. Everything after the // will be ignored by the compiler.

String declarations are a bit harder and will be discussed later

Lesson written by Shun the prez, except for the list of forbidden varibale names. I got that from http://www.cplusplus.com/doc/tutorial/tut1-2.html

Enter supporting content here