Sinisterly
C++ tutorial part II - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C)
+--- Thread: C++ tutorial part II (/Thread-C-tutorial-part-II)



C++ tutorial part II - chipp - 04-29-2012

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:

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();
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:

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/


RE: C++ tutorial part II - chipp - 05-16-2012

i don't see any opinions for this post... i'm wonderin'...


RE: C++ tutorial part II - chipp - 05-16-2012

i don't see any opinions for this post... i'm wonderin'...


RE: C++ tutorial part II - mrLyosha - 08-25-2012

in your example integer and double are same number, and if I'm not mistaken integer should be round number


RE: C++ tutorial part II - chipp - 08-27-2012

try to compile it first...


RE: C++ tutorial part II - Frooxius - 08-31-2012

Just a few corrections:

1) C++ has no string datatype. There's the string object, which is implemented as a class in the STD library and has defined various functions for string operations, fully integrated with the OOP model, however it actually still uses character array to store the text data. Imagine it as the character array wrapped around various functions to make it easier to use.

2) 2.34 is not a decimal number, it's a double literal (its datatype is double). C/C++ doesn't have decimal datatype, C# however does (it has higher precision than double)

3) There's no rounding taking place. By converting a floating point number (double or float) into integer datatype, you don't round it. It is truncated instead - the fractional part is simply thrown away. So even if it was 2.99, after storing it as an integer it would be 2, not 3.
Additionally, float and double datatypes are capable of storing numbers much larger than even the largest integer datatype can store, so trying to store these as an integer would result in nonsense value. You should be extremely careful when doing this, as it can easily lead to bugs and very odd program behavior.

4) Initializing is extremely important. Variable declaration only reserves a space for the variable, but the value there is some leftover - garbage data from anything that was there before.

5) Regarding the boolean datatype. C++ treats it more or less like an integer number (in older versions of C, there was no boolean datatype, it simply used integers). Any non-zero value is interpreted as true and only zero is interpreted as false. If you tried to convert boolean back to integer, then true would result in number 1, false in number 0 (usually, don't rely on this, some compilers can choose to implement it differently).

So you can do something like
Code:
int line = 8; while(line--) /* do something */

You would normally use for loop for this, but it's just an example. Integer is now used as a condition in the loop. It starts at 8, which is interpreted as true and it will decrement in each cycle until it reaches zero, which is interpreted as false, so the loop stops.

It is an equivalent of writing
Code:
int line = 8; while(line-- != 0) /* do something */

Some safer languages like C# for example will actually force you to write it this way, to prevent confusing situations, C++ does it like this partially because of backwards compatibility and partially because it's very close to how many processors work (they have conditional jump instructions that use an integer value determine if to jump or not and it interprets anything nonzero as true and zero as false).

6) cin.get() doesn't "pause the screen", it pauses the execution of your program and transfers control to the operating system, which then waits for user's input. When user presses Enter, the input is sent to your program by the operating system and it is resumed. You can actually use cin.get() to get input data from the user, but here you simply ignore whatever he typed and just wait for the Enter.

7) If you're going about basic datatypes, don't include string, because it's not a basic datatype in C++ (it is in some other languages though). Instead, strings are done like this:

Code:
const char *sentence = "Fluffy string";

It's basically a pointer to the beginning of the character array which contains the sequence "Fluffy string", which is stored in the string table by the compiler. If you want to make a string that you can write to (never try modify data of string declared like above, that's why I declared it as const), then do it like this:
Code:
char myString[10];
This character array can store 10 characters, but one character (null - it terminates the string - marks its end) is automatically added in many situations, so you can store only 9 actual printable characters.