C++ tutorial part II 04-29-2012, 09:12 AM
#1
Data Types
ok, finally my second part of C++ tutorial is comes up... based on reference from cplusplus.com/doc/tutorial, now we're talking about data types...
data types in C++ including: integers, doubles (numbers that has number after the decimal point), char (characters), strings (strings of characters), boolean (true or false data types), and so forth...
so, for the example for various data types is as below:
there's if and else statement which will be explained in our next tutorial...
alright, first, for this code:
we're trying to assign a decimal value to an integer data type. therefore, the value is rounded (in the integer case, it always performed for rounding down operation) because it's an integer and not a decimal (i'm talking about decimal value that has a fraction which value is more than zero and less than one behind the point mark ".") value. so the value stored in the round_num variable is 2
at the next line, we're initializing a double data type which can store a decimal value. so, our 2.34 can be stored perfectly within the variable...
move down one line, there's string type we just declared (the sentence). notice that for using string data type, we, first, have to include the <string> library first... string can store (lots of) strings of characters just in one variable (in this case sentence)
the last is a boolean data type, which only have two possible value: true or false. for addition, as far as i know, true is representing non-zero value either is negative or positive integers and [b]false is representing zero value (please correct this line).
in nearly the last line we can see the code:
it's a method from the istream[ class for pausing the screen until the user press the "ENTER" key (and in this case, after "ENTER" key is pressed, the program exits...
additional example for using string
cin is read the user's input until user:
1. press enter
2. cin found an space (" ") character between user's input, e.g: 1 2 3 <-- reads 1 and then store it in a variable, and the remaining 2 and 3 sre in the input buffer until it stored in the next variablle in the next extraction operation
3. something else i don't know yet...
for strings, when user enter the name: "chipp zanuff", what stored in the string is just the "chipp" ("zanuff" is still remain in the input buffer).
how to take all the user's input without leaving anything within the buffer? we're using the getline() method which is included in the <string> library. i'll try to give an example:
the getline() function extract all the input buffer from the cin and stores it in the some_string variable...
feel free to correct any part of this tutorial if necessary, as usual...
references:
http://cplusplus.com/doc/tutorial/variables/
http://cplusplus.com/reference/string/getline/
ok, finally my second part of C++ tutorial is comes up... based on reference from cplusplus.com/doc/tutorial, now we're talking about data types...
data types in C++ including: integers, doubles (numbers that has number after the decimal point), char (characters), strings (strings of characters), boolean (true or false data types), and so forth...
so, for the example for various data types is as below:
Code:
#include <iostream>
#include <string> //for using "string" data type
using namespace std;
int main () {
//i heard that initializing is a good programming practice...
int round_num = 2.34;
double decimal = 2.34;
bool bools = false;
string sentence = "press ENTER to exit the program...";
cout << "the number 2.34 in integer data type is: " << round_num << endl;
cout << "the number 2.34 in double data type is: " << decimal << endl;
if (decimal < round_num)
bools = true;
//else, the bools remains the same: false
if (bools)
cout << "decimal is less than the integer" << endl;
else
cout << "decimal is greater or equal than the integer" << endl;
cout << sentence;
cin.get(); //pause the screen until "enter" key is pressed
return 0;
}there's if and else statement which will be explained in our next tutorial...
alright, first, for this code:
Code:
int round_num = 2.34;we're trying to assign a decimal value to an integer data type. therefore, the value is rounded (in the integer case, it always performed for rounding down operation) because it's an integer and not a decimal (i'm talking about decimal value that has a fraction which value is more than zero and less than one behind the point mark ".") value. so the value stored in the round_num variable is 2
at the next line, we're initializing a double data type which can store a decimal value. so, our 2.34 can be stored perfectly within the variable...
move down one line, there's string type we just declared (the sentence). notice that for using string data type, we, first, have to include the <string> library first... string can store (lots of) strings of characters just in one variable (in this case sentence)
the last is a boolean data type, which only have two possible value: true or false. for addition, as far as i know, true is representing non-zero value either is negative or positive integers and [b]false is representing zero value (please correct this line).
in nearly the last line we can see the code:
Code:
cin.get();additional example for using string
cin is read the user's input until user:
1. press enter
2. cin found an space (" ") character between user's input, e.g: 1 2 3 <-- reads 1 and then store it in a variable, and the remaining 2 and 3 sre in the input buffer until it stored in the next variablle in the next extraction operation
3. something else i don't know yet...
for strings, when user enter the name: "chipp zanuff", what stored in the string is just the "chipp" ("zanuff" is still remain in the input buffer).
how to take all the user's input without leaving anything within the buffer? we're using the getline() method which is included in the <string> library. i'll try to give an example:
Code:
#include <iostream>
#include <string>
using namespace std;
int main () {
string some_string;
cout << "enter your name: ";
[b]getline (cin, some_string)[/b];
cout << "welcome, " << some_string << endl;
cin.get();
return 0;
}the getline() function extract all the input buffer from the cin and stores it in the some_string variable...
feel free to correct any part of this tutorial if necessary, as usual...
references:
http://cplusplus.com/doc/tutorial/variables/
http://cplusplus.com/reference/string/getline/




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)