Page 1 of 1

simple c++ calculator create a simple calculator that allows you to calculate whole numbers Rate Topic: -----

#1 mrgoo  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 01-March 09

Post icon  Posted 02 March 2009 - 12:51 PM

Right... I will give you the necessary info to do this. If you know enough about c++ to do this sort of thing yourself this tutorial is not for you. I do not claim that this code will work with every compiler. It however does work with the GNU compiler collection

without further a do.

Creating this calculator is a simple and fun way to get you going with a program that is extensible and can come into actual use. Yes a useful program that the almost absolute beginner can write. All you need to know is how to use cin, cout and have an idea about variables and if statements.

To start off you need to make a new c++ file. If you're using an IDE like code::blocks then just create a new project. and at the top (if it isn't already there) put
 
#include <iostream>
using namespace std;


since this is the only library being used the namespace will make work easier. from there we can begin the main method. we start by declaring all of the variables. in the turorial we will be using the integer (int) and the charcter (char) variables. The integer can be changed later to add functionality.

#include <iostream>

using namespace std;
int main()
{
	 char cmenu;
	 char coperator;
	 int ifirstnum;
	 int isecondnum;
	 int y;
	 int i;
	 i = 2;
	 y = 5;
}


As you can see all of these variables (with the exception of y) has a descriptive name. You should do that to all of your variables as it makes your code more readable. Y is not descriptive as it shares a purpose that does not require such a name.

After you have declared your variables we can start by adding the menu that the program starts up with.We then create a while statement to contain the rest of the code so that we can go to the beginning after the code is executed.Then an if/else if statement is used here. If you have used case statements before go ahead use them but personally I prefer to use else if as I am more comfortable with it.

#include <iostream>

using namespace std;
int main()
{
	 char cmenu;
	 char coperator;
	 int ifirstnum;
	 int isecondnum;
	 int y;
	 int i;
	 i = 2;
	 y = 5;
	 
	 while(i = 2)
	 {

	 cout<<"\ny this is my calculator. To calculate enter g for help enter h and to exit press x:";
	 cin>>cmenu;
	 if(cmenu == 104)
	 {
		  cout<<"enter a value an operator then a value pressing enter each time to use this";		  
	 }
	 else if(cmenu == 120)
	 {
		  break;
	 }
	 else if(cmenu == 103)
	 {
		  //code goes here
	 }
	 else
	 {
		  cout<<"error, incorrect input"
	 }
	 }
}



If your asking about the numbers: characters are based on something called ASCII which is basically a character set in which numbers are assigns to every character in that set. for example 104 is a lower-case h 120 is a lower-case x and 103 is a lower-case g. In case you do not know what \ny is. It is basically a newline \n followed by the y variable. someone correct me if i'm wrong but without that y the code was a mess to look at when executed. I don't know why but I'm sure if you ask someone will tell you. The while statement may or may not be new to you. Basically while the value between the parenthesis is true the code will continually repeat through the loop until either the value is false or there is a break statement. This breaks out of the loop to continue the rest of the code.

next we will take input for the three statements and use a similar statement to determine whether to add subtract multiply or divide the two numbers.

#include <iostream>

using namespace std;
int main()
{
	 char cmenu;
	 char coperator;
	 int ifirstnum;
	 int isecondnum;
	 int y;
	 int i;
	 i = 2;
	 y = 5;
	 
	 while(i = 2)
	 {

	 	  cout<<"\ny this is my calculator. To calculate enter g for help enter h and to exit press x:";
	 	  cin>>cmenu;
	 	  if(cmenu == 104)
	 	  {
			  cout<<"enter a value an operator then a value pressing enter each time to use this";
					
	 	  }
	 	  else if(cmenu == 120)
	 	  {
			  break;
	 	  }
	 	  else if(cmenu == 103)
	 	  {
	 		  cin>>ifirstnum;
	 		  cin>>coperator;
	 		  cin>>isecondnum;
	 		  if(coperator == 43)
	 		  {
				  int ans;
				  ans = ifirstnum + isecondnum;
				  cout<<ans<<endl;
	 		  }
	 		  else if(coperator == 45)
	 		  {
				  int ans;
				  ans = ifirstnum - isecondnum;
				  cout<<ans<<endl;
	 		  }
	 		  else if(coperator == 42)
	 		  {
				  int ans;
				  ans = ifirstnum * isecondnum;
				  cout<<ans<<endl;
	 		  }
	 		  else if(coperator == 47)
	 		  {
				  int ans;
				  ans = ifirstnum / isecondnum;
				  cout<<ans<<endl;
	 		  }
	 		  else
	 		  {
				  cout<<"wrong operator";
			  
	 		  }	  
	 }
	 else
	 {
			  cout<<"error, incorrect input";
		  
	 }
	 }
	 
	 return 0;
}



This is what the basis of the calculator is. it is just a case of using ASCII to determine which operator it is and act accordingly. Of course this is not the best written calculator in the world. You should start by improving upon the code to make it better written. Then you could add further functionality like using floating point numbers and adding new operations like powers. You could even rewrite the calculator so that it can be called from a prompt! But be sure to add comments unlike I have here so others can make use of your code.

This post has been edited by mrgoo: 04 March 2009 - 01:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: simple c++ calculator

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 124
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Posted 04 March 2009 - 01:28 PM

I like it; it's a good start on a calculator project that has great growth potential. As you said, there are countless features that could be added, like trig functions and the like. You could use the '+' chars instead of the ascii symbols (if you felt like it) for the menu stuff. It would be really cool if the program operated on a command line prompt, where you could type in the entire line like 7+8, but that would be difficult (like most things worth doing :) )
Was This Post Helpful? 0
  • +
  • -

#3 ghozt2015  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 34
  • Joined: 19-July 15

Posted 20 July 2015 - 04:44 AM

I really liked this tutorial. It was really helpful for a beginner like myself. I have been dabbling in coding on and off for years now and this is the first project I have completely finished from start to end. I like how the OP took the time and gave detailed description of WHY he did something, and that is something you do not see enough of in other tutorials around the net.

Great job and keep up the good work!
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,870
  • Joined: 31-December 10

Posted 09 May 2017 - 07:38 AM

I hate it when tutorial writers don't show you the things they tried before they got to the solution they like and use for the tutorial. That's a part of the learning process, and therefore should be included in the tutorial.

View Postmrgoo, on 02 March 2009 - 03:51 PM, said:

	 i = 2;
	 y = 5;
	 
	 while(i = 2)
	 {

	 	  cout<<"\ny this is my calculator. To calculate enter g for help enter h and to exit press x:";
	 	  cin>>cmenu;
	 	  if(cmenu == 104)
	 	  {
			  cout<<"enter a value an operator then a value pressing enter each time to use this";
					
	 	  }
	 	  else if(cmenu == 120)
	 	  {
			  break;
	 	  }
	 	  else if(cmenu == 103)
	 	  {
	 		  cin>>ifirstnum;
	 		  cin>>coperator;
	 		  cin>>isecondnum;
	 		  if(coperator == 43)
	 		  {
				  int ans;
				  ans = ifirstnum + isecondnum;
				  cout<<ans<<endl;
	 		  }
	 		  else if(coperator == 45)
	 		  {
				  int ans;
				  ans = ifirstnum - isecondnum;
				  cout<<ans<<endl;
	 		  }
	 		  else if(coperator == 42)
	 		  {
				  int ans;
				  ans = ifirstnum * isecondnum;
				  cout<<ans<<endl;
	 		  }
	 		  else if(coperator == 47)
	 		  {
				  int ans;
				  ans = ifirstnum / isecondnum;
				  cout<<ans<<endl;
	 		  }
	 		  else
	 		  {
				  cout<<"wrong operator";
			  
	 		  }	  
	 }
	 else
	 {
			  cout<<"error, incorrect input";
		  
	 }
	 }


That while loop is an infinite loop because it's setting i to be 2 and checking if 2 is true or false, which it's true. I think you meant to use the equality operator ==.

This post has been edited by vividexstance: 09 May 2017 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1