Making a basic calculator in C++ Part (4/8)

Hello guys, it’s been almost 2 months since I wrote my last article. I have been quite busy with academics but I think I will finish this series within the next 10 days.
So, last time I asked you all to write code to design the basic UI of our calculator. I hope you all would have written the code by now. You can have a look at my code below.
//function to return the string of equation user inputs
string inputData()
string s;
cout << "Enter -1 to exit the program and -8 to try again" << endl;
cout << "Enter what you want to calculate: "; cin >> s;
return s;

int main()
string s = inputData();
if (s.equals("-1")
return 0;
else if (s.equals("-8"))
s = inputData();

In the above function we came across some new functions, so, let’s discuss them:

.equals() : We cannot compare strings like integers and use a function .equals() to compare them.

system(“CLS”): This is a function to clear the screen on the output console. To use this function you have to include stdlib.h header file in your code. You can import this header file by writing #include <stdlib.h>.

The important point to note: We can exit from the program by simply writing return 0 anywhere inside a function. This statement causes the main function to shut down and hence, our program is closed.

Check this also


In simple words, a string is a collection/array of characters. Unlike int, the string is not a primitive data type but it is an object ( you will learn about objects in details in coming tutorials) of string class. Indexing of character in a string starts from 0. You can use str[index] to retrieve the character at the respective index. We can manipulate strings using inbuilt functions. To use these, you have to import a string header file into your code.
Some of the function are discussed below:

length(): This function is used to retrieve the length of the string. For example, “techistaan”.length() will output 10.
strcat(): Although you can combine two strings directly using + operator you can also use strcat() function to concatenate two strings. For example, strcat(“asha”,”sa892″) will output ashasa892.

Typecasting of variables:

Sometimes we want to change the object of string type to int or variable of double type to int or some other variable type transformation. This process of changing variable types is called typecasting of variables. We can typecast the variables as follows:

int x = (int) 4.0; //change double to int
int y = (int) "8"; //change string to int
double z = (double) x; // change int to double

Play with strings and typecasting a little bit and you will become comfortable with them. Try to learn about more strings functions on your own and in the next part, I will show you how to create these functions manually. I will call out this a day then! Good night and best of luck!

Stay tuned with Techistaan to get unbiased smartphone & gadgets reviews and latest tech updates.


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button

Adblock Detected

Please consider supporting us by disabling your ad blocker