SkidForums


Where skids go hackers
 
HomeFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

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

View previous topic View next topic Go down 
AuthorMessage
Nikolai
C++ Master
C++ Master


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

PostSubject: C++ Calculator (Incorperates functions, returns, switches, loop)   Sat 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
View user profile
woefie
Owner
Owner
avatar

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

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

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


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

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

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


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

PostSubject: Re: C++ Calculator (Incorperates functions, returns, switches, loop)   Fri 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
View user profile
Sponsored content




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

Back to top Go down
 
C++ Calculator (Incorperates functions, returns, switches, loop)
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
-
» Kat switches teams
» Heresy Combat Calculator
» Death Loop
» Lightning Returns Being Marketed Primarily Under the PS Banner
» Lightning Returns Discussion

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