Nikolai C++ Master
k Posts : 24 Points : 43 Reputation : 5 Join date : 2012-04-26
| Subject: 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. | |
|
woefie Owner
Posts : 52 Points : 97 Reputation : 0 Join date : 2012-04-24
| Subject: 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
| |
|
Nikolai C++ Master
k Posts : 24 Points : 43 Reputation : 5 Join date : 2012-04-26
| Subject: Re: C++ Calculator (Incorperates functions, returns, switches, loop) Thu May 17, 2012 11:42 am | |
| I do not understand what you mean? | |
|
Rzdude Beginner
Posts : 78 Points : 110 Reputation : 0 Join date : 2014-06-27
| Subject: 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 | |
|
Sponsored content
| Subject: Re: C++ Calculator (Incorperates functions, returns, switches, loop) | |
| |
|