# I made a Calculator!

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 02-07-2005
digdug4life
I made a Calculator!
Here its done!!!!!!! WOOT WOOT! :D
Code:

```#include <iostream> int addion ( int x , int y ) {  return x + y; } int subtraction ( int x , int y ) {  return x - y; } int multiplication ( int x , int y ) {  return x * y; } int division ( int x , int y ) {  return x / y; } using namespace std; int subtract(); int add(); int multi(); int div();         int main() {   int input;     cout<<"1. Addition\n";   cout<<"2. Subtraction\n";   cout<<"3. Multiplication\n";   cout<<"4. Division\n";   cout<<"Selection: ";   cin>> input;   switch ( input ) {   case 1:                add();     break;   case 2:                subtract();     break;   case 3:                multi();     break;   case 4:                div();     break;   default:                cout<<"Error\n";     break;   }   cin.get(); } int subtract(){     int x;   int y;    cout << "Firt number to subtract:";   cin>> x;   cin.ignore();   cout << "Second number to subtract:";   cin>> y;   cin.ignore();   cout << "The answer is:" << subtraction (x , y );   cin.get(); } int add(){   int x;   int y;    cout << "Firt number to add:";   cin>> x;   cin.ignore();   cout << "Second number to add:";   cin>> y;   cin.ignore();   cout << "The sum is:" << addion (x , y );   cin.get();   } int multi(){   int x;   int y;    cout << "Firt number to multiply:";   cin>> x;   cin.ignore();   cout << "Second number to multiply:";   cin>> y;   cin.ignore();   cout << "The product is:" << multiplication (x , y );   cin.get();   } int div(){   int x;   int y;    cout << "Firt number to divide:";   cin>> x;   cin.ignore();   cout << "Second number to divide:";   cin>> y;   cin.ignore();   cout << "The quotiant is:" << division (x , y );   cin.get();   }```
ok now what?
• 02-07-2005
InvariantLoop
now you need to learn how to indent your code. A good formatting style, will save you a lot of debugging time.
• 02-07-2005
Scribbler
Actually, it is indented. Maybe he could focus a little on placing newlines between functions, and between a couple codeblocks, but other than that it's laid out just fine. (That and maybe move the functions defined at the top to the bottom and add prototypes, but that's just personal preference).

All in all though, he's definately demonstrated significant improvement.
• 02-07-2005
digdug4life
thank you, but i didnt understand the prototype part of the tutorial
• 02-07-2005
digdug4life
Now what should I make?
• 02-07-2005
Asbestos
A kickass game.
• 02-07-2005
digdug4life
And how would i do that?
• 02-07-2005
major_small
Quote:

Originally Posted by digdug4life
Now what should I make?

an expression parser ;)

just kidding... try adding some recursive stuff like factorials and powers...
• 02-07-2005
digdug4life
and how would i do that?
• 02-07-2005
major_small
Quote:

Originally Posted by digdug4life
and how would i do that?

if you're asking me, look around for recursive functions. basically, a recursive function is a function that calls itself from within itself. one example is my signature. that's a function that calls itself. as you may be able to tell, that will leave you in an infinite loop. some compilers will complain about the recursive depth being too deep and kill the program.

now the math:

a factorial is x multiplied by every number less than x until you get to zero. for example:
Code:

`4! = 4*3*2*1 = 24`
raising b to the power of e is the same as saying multiply b by itself e times. for example, 2 to the power of 3, or 2^3 in some notations, can be written as:
Code:

`b5=b*b*b*b*b;`
by now you probably realize that you can do both of these with a loop, and it's true that almost every recursive function you come across can be done in a loop, but sometimes recursive functions are more efficient, and they're definately alot more fun to write.

basically you want to test something in the function, and if it doesn't meet the requirements, return the same function with modified parameters. if it meets the requirements, you'll want to return that value. here's some example code that adds all the numbers from 1 to 100 for you to use (you can modify this for the exponent function):
Code:

```#include<iostream> int recursiveAdd(int); int main() {     std::cout<<recursiveAdd(100);     std::cin.get();     return 0; } int recursiveAdd(int x) {     if(x>1)         return x+recursiveAdd(x-1);     else         return x; }```
• 02-07-2005
homeyg
Try making a program that reverses words and sentences.
• 02-07-2005
Kybo_Ren
Quote:

raising b to the power of e is the same as saying multiply b by itself e times.
Careful... Since e is also used as the constant for Euler's number, this could also mean b raised to the power of (2.7...).

b raised to the power n would be a better way to express that.
• 02-07-2005
cerin
hmmmmmmm........ I guess this is where problem solving part of programming comes in. I don't know how to make it change the equation on users input, but I was thinking that it could do something like input*input=x then.............. :confused: make another loop to do x *input??????? I pondered on this for quite awhile and I can't come with anything else or see how it fits into that recursive program.
• 02-07-2005
andyhunter
I believe I have posted this to you before, however alas I will say it again. This site has many useful resources besides the message boards. For looking into programming ideas to hone your skills you could look at:

Help free challenges

These are good ideas to help you hone your skills.
• 02-07-2005
cgod
I got 4 error's when compiling your code here's some suggestions to making it better :)

Code:

```#include <iostream> using namespace std; int addion ( int x , int y ); // funtion prototypes declares the name and // the parameters of the function good pratice to declare function prototypes beacuse it // is poor programming practive to depend on the order of the functions // as it is quite hard to ensure that all functions are in the correct order // in larger programs int subtraction ( int x , int y ); int multiplication ( int x , int y ); int division ( int x , int y ); // good functions should be short do one thing then return a value int main() {   int input;     cout<<"1. Addition\n";   cout<<"2. Subtraction\n";   cout<<"3. Multiplication\n";    cout<<"4. Division\n";   cout<<"Selection: ";   cin>> input;   switch ( input )   {           // check user imput and give appropiate answer   }   cin.get(); } int addion ( int x , int y ) {         return x+y; } int subtraction ( int x , int y ) {         return x-y; } int multiplication ( int x , int y ) {         return x*y; } int devision ( int x , int y ) {         return x/y; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last