Tag Archives: write

TEA BREAK WITH C++: DATA TYPES

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/ This series you may follow and everyday by reading 10 mins in your tea break you can get the essence.

These are the ones that define type of variable or data in general. As we did previously with; 

int sameVar; 

Here the “int” section is a data type. We define it as “integer” by putting “int”. 

There are of course different types of data are possible which are simply; 

char: This is used to store a single character like ‘t’, ‘b’, ‘L’. For the definition of char data type content, you can use ‘’. 

float: To store fractional numbers, up to 7 decimals. 

double: To store fractional numbers, up to 15 decimals. 

bool: To store logical values or Boolean values. This can store “true” or “false” (1 or 0). 

wchar_t: This means “wide character”. This is also like char but has greater size. 

void: This means “without a value”. Mostly used for functions where we do not want to return any value. 

string: String is like character but stores group of characters. To define string content, you need to use “”.

Here you can see the difference between using char and string; 

PROGRAM:

  1. #include <iostream>
  2. using namespace std; 
  3. int main(){
  4. string i = “hello world”; //this is how to define string
  5. char x = ‘h’; // this is how to define character
  6. cout<< i<<endl; 
  7. cout<<x<<endl;
  8. system(“pause”);
  9. }

 OUTPUT: 

Here you also need to be aware that multiple strings can be added to each other with + operator; 

  1. #include <iostream>
  2. using namespace std; 
  3. int main(){
  4. string s1 = “Hello “, s2=”World”, s3;
  5. s3 = s1 + s2; 
  6. cout<<s3<<endl;
  7. system(“pause”);
  8. }

 OUTPUT:

Data Type Modifiers (signed, unsigned, long, short)

Data types also can be defined by signed, unsigned, long or short. 

So you can define data types as; 

unsigned short int, long int, long double, short int etc. 

Signed here means including the negative numbers. If it is unsigned it is only positive numbers and you will have larger positive numbers. So you need to use unsigned when you are not using negative numbers. This is a better approach. On the other hand, you are free to use general int as a data type but as a programmer you need to think about being efficient while coding. Since a program with a less storage is better. 

Long and short are about having a larger or less number range. For example for a 32 bit system, with long int you can work between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. But with short int you can only work between -32,768 and 32,767. On the other hand, normal “int” data type will have the range in between these long and short which is between -2,147,483,648 and 2,147,483,647. So, you can learn these data type storages in deep and use one of them whenever you need. 

EXERCISE: Write a program to calculate a tax from price entered by user. The tax rate must also be defined by the user. 

PROGRAM

  1. /* 
  2. Program that calculates tax of any price entered
  3. version 0.0
  4. author ilker <info@biawd.com>
  5. */
  6. #include <iostream>
  7.  
  8. using namespace std;
  9. int main(){
  10. int rate;
  11. float result, price;
  12. cout<<“Enter the rate for the tax”<<endl;
  13. cin>>rate;
  14. cout<<“Enter the price that tax will be calculated:”<<endl;
  15. cin>>price;
  16. result=price*rate/100;
  17. cout<<“The tax of the price entered is:”<<result<<endl;
  18.  

 OUTPUT

NOTE: You may buy this course in paperbook book format from https://www.amazon.com/dp/B0892HQTGS

TEA BREAK WITH C++: VARIABLES

Since we learned basic input function at programming with C++, we need to talk a little more about the variables. 

Variables are simply naming some data location in your pc. With variables you can store a value. 

Variables can be global, local or block based. Actually, you can use the same variable name in these locations at the same time. 

  1. #include <iostream>
  2. using namespace std;
  3. int sameVar=1; //defining a global variable at header location
  4. int main(){
  5.     int sameVar=2;    //defining a local variable inside int main
  6.     cout<<“local variable:”<<sameVar; //printing a local variable in int main 
  7.     {
  8.     int sameVar=3; //here we define a variable in any block. 
  9.     cout<<“block variable:”<<sameVar; //printing the block variable
  10. }
  11. //reaching a global variable inside program
  12. cout<<“global variable:”<<::sameVar; //we print global variable in a program
  13. }

 Output;

Here the most important thing we need to talk is the 

variable = value logic. 

Always variable is on the left and the value is on the right of the equivalent sign. 

TEA BREAK WITH C++: Basic Input & endl

On line 9 in the previous example we give 

    int firstNumber, secondNumber, sum;

We define integer variables. These are first number with the definition firstNumber, and the second number defined as secondNumber. And the sum. These we defined as integer by writing “int” at the beginning.  

Continue reading TEA BREAK WITH C++: Basic Input & endl

TEA BREAK WITH C++ : Basic Comment Section for All programs

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

Here these lines are comment section giving information about the program, and you need to include these lines so that another programmer can understand what it is about easily. 

Continue reading TEA BREAK WITH C++ : Basic Comment Section for All programs

TEA BREAK WITH C++: Basic Comment Section for All programs

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

Here these lines are comment section giving information about the program, and you need to include these lines so that another programmer can understand what it is about easily. 

Continue reading TEA BREAK WITH C++: Basic Comment Section for All programs

TEA BREAK WITH C++: BASIC INPUT / OUTPUT

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

As we have seen in the previous section, library we are using for basic input / output operations is the iostream library. That is why we need to include that at the beginning. 

Continue reading TEA BREAK WITH C++: BASIC INPUT / OUTPUT

TEA BREAK WITH C++: MY FIRST C++ PROGRAM

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

  1. //my first c++ program
  2. #include <iostream>
  3. using namespace std; 
  4. int main(){
  5. cout<<“hello world!”;
  6. return 0;
  7. system(“pause”);
  8. }
  9. /* This ends 
  10. the file */
Continue reading TEA BREAK WITH C++: MY FIRST C++ PROGRAM

TEA BREAK WITH C++: Paragraph Comments (Multiple Line Comment)

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

Another type of comment is paragraph comment (or multiple line comment) where you start commenting with “*/” and end with “/*”

Continue reading TEA BREAK WITH C++: Paragraph Comments (Multiple Line Comment)

TEA BREAK WITH C++: Line Comments

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

The “//” sign means you are adding a comment. This is called line comment. The things you write after these “//” will not be taken as a command (means these things will not be compiled). It is always a good approach to write down the properties of the code at the beginning like author, for which aim this code is written etc. 

Continue reading TEA BREAK WITH C++: Line Comments

TEA BREAK WITH C++: Opening a New File and printing famous “HELLO WORLD”

NOTE: You may download codes and extra source material for this book from https://bookofengineering.com/easyseries/

You can start a new file by selecting; 

File > New > Source File OR pression Ctrl + N on your keyboard; 

Continue reading TEA BREAK WITH C++: Opening a New File and printing famous “HELLO WORLD”