SkidForums
SkidForums
SkidForums
Would you like to react to this message? Create an account in a few clicks or log in to continue.


Where skids go hackers
 
HomeLatest imagesSearchRegisterLog in

 

 C++ Calculator (Incorperates functions, returns, switches, loop)

Go down 
3 posters
AuthorMessage
Nikolai
C++ Master
C++ Master



Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

C++ Calculator (Incorperates functions, returns, switches, loop) Empty
PostSubject: C++ Calculator (Incorperates functions, returns, switches, loop)   C++ Calculator (Incorperates functions, returns, switches, loop) EmptySat May 12, 2012 12:34 pm

Code:

#include <iostream>

using std::cout; //Since we're only deadling with cin and cout.
using std::cin;

//These are the prototypes for our functions.
int TakeInput();
char Operation();
int Add(int x, int y);
int Subtract(int x, int y);
int Multiply(int x, int y);
int Divide(int x, int y);

int main()
{
   int nOption;
   do  //A do-while loop meaning it will run the loop at least once until a condition that id doesn't like is met.
   {
   cout << "Enter 1 to perform a mathematical operation, enter 2 to quit->";
   cin >> nOption;
   switch(nOption) //Using a simple switch statement for option.
      {
   case 1:
      {
         int x;
         x = TakeInput();
         char c;
         c = Operation();
         int z;
         z = TakeInput();
         int myResult;
         switch(c) //A switch statement inside a switch statement - yes it's very possible.
         {
         case '+': //Notice since I am taking a char input I use the ' ' to enclose the characters.
            {
               myResult = Add(x,z);
               break;
            }
         case '-':
            {
               myResult = Subtract(x,z);
               break;
            }
         case '*':
            {
               myResult = Multiply(x,z);
               break;
            }
         case '/':
            {
               myResult = Divide(x,z);
               break;
            }
         default:
            break;
         };
         cout << "\nYour result is->" << myResult;
         break;
      }
   case 2:
      {
         exit(1);
      }
   default:
      {
      cout << "\nI could not understand your input\n\n";
      break;
      }
      }
   }
   while (nOption != 1);
   cin.get();
   cin.ignore();
   return 0;
}
int TakeInput()
{
   cout << "\nEnter an integer->";
   int integer;
   cin >> integer;
   return integer;
}
char Operation()
{
   cout << "\nEnter an operand( + , - , * , / )->";
   char operand;
   cin >> operand;
   return operand;
}
int Add(int x, int y)
{
   int result;
   result = x+y;
   return result;
}
int Subtract(int x, int y)
{
   int result;
   result = x-y;
   return result;
}
int Multiply(int x, int y)
{
   int result;
   result = x*y;
   return result;
}
int Divide(int x, int y)
{
   int result;
   result = x/y;
   return result;
}
For some of you starting off C++, this might be a decent start. Take the time to read it.
All though I have not done a loop tutorial yet which I plan to do next.
Back to top Go down
woefie
Owner
Owner
woefie


Awesome
Posts : 52
Points : 97
Reputation : 0
Join date : 2012-04-24

C++ Calculator (Incorperates functions, returns, switches, loop) Empty
PostSubject: Re: C++ Calculator (Incorperates functions, returns, switches, loop)   C++ Calculator (Incorperates functions, returns, switches, loop) EmptyWed May 16, 2012 9:40 am

the way of building is new to me O>o
Back to top Go down
http://skid.forum-leaks.com
Nikolai
C++ Master
C++ Master



Awesome k
Posts : 24
Points : 43
Reputation : 5
Join date : 2012-04-26

C++ Calculator (Incorperates functions, returns, switches, loop) Empty
PostSubject: Re: C++ Calculator (Incorperates functions, returns, switches, loop)   C++ Calculator (Incorperates functions, returns, switches, loop) EmptyThu May 17, 2012 11:42 am

I do not understand what you mean?
Back to top Go down
Rzdude
Beginner
Beginner



Posts : 78
Points : 110
Reputation : 0
Join date : 2014-06-27

C++ Calculator (Incorperates functions, returns, switches, loop) Empty
PostSubject: Re: C++ Calculator (Incorperates functions, returns, switches, loop)   C++ Calculator (Incorperates functions, returns, switches, loop) EmptyFri Jun 27, 2014 5:40 am

How to use that code Im really really more than new to c++ and it looks pretty cool
Back to top Go down
Sponsored content





C++ Calculator (Incorperates functions, returns, switches, loop) Empty
PostSubject: Re: C++ Calculator (Incorperates functions, returns, switches, loop)   C++ Calculator (Incorperates functions, returns, switches, loop) Empty

Back to top Go down
 
C++ Calculator (Incorperates functions, returns, switches, loop)
Back to top 
Page 1 of 1
 Similar topics
-
» Simple calculator in C++

Permissions in this forum:You cannot reply to topics in this forum
SkidForums ::   :: Coding :: Coding :: C/C++-
Jump to: