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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.