I made a Calculator!

This is a discussion on I made a Calculator! within the C++ Programming forums, part of the General Programming Boards category; Here its done!!!!!!! WOOT WOOT! Code: #include <iostream> int addion ( int x , int y ) { return x ...

  1. #1
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91

    Talking I made a Calculator!

    Here its done!!!!!!! WOOT WOOT!
    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?
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    now you need to learn how to indent your code. A good formatting style, will save you a lot of debugging time.
    When no one helps you out. Call google();

  3. #3
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    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.

  4. #4
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    thank you, but i didnt understand the prototype part of the tutorial
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  5. #5
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    Now what should I make?
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    A kickass game.

  7. #7
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    And how would i do that?
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by digdug4life
    Now what should I make?
    an expression parser

    just kidding... try adding some recursive stuff like factorials and powers...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    and how would i do that?
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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;
    }
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Try making a program that reverses words and sentences.

  12. #12
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    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.
    Last edited by Kybo_Ren; 02-07-2005 at 06:21 PM. Reason: clarity

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    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.............. 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.
    My computer is awesome.

  14. #14
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  15. #15
    Banned
    Join Date
    Oct 2004
    Posts
    250
    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;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c calculator
    By xmadmaxx in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 02:15 AM
  2. C Calculator Problem
    By ShadeS_07 in forum C Programming
    Replies: 6
    Last Post: 07-09-2008, 05:58 PM
  3. big integer calculator! addition/subtraction algorithm
    By counton in forum C Programming
    Replies: 12
    Last Post: 11-22-2007, 03:42 AM
  4. Calculator in C?
    By daveS in forum C Programming
    Replies: 5
    Last Post: 08-04-2003, 11:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21