Making a calculator (newbie)

This is a discussion on Making a calculator (newbie) within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to make make myself a calculator, very simple one. Code: #include <iostream> using namespace std; int main() ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Making a calculator (newbie)

    Hi,

    I'm trying to make make myself a calculator, very simple one.

    Code:
    #include <iostream>
          using namespace std;
          int main()
    
    cout << "Please enter the first number ";
    cin >> numberone;
    cin.ignore();
    cout << "Please enter the second number ";
    cin >> numbertwo;
    cin.ignore();
    cout "Please enter whether you wish to add (+), subtract (-), mulitiply (*) or divide (/) ";
    cin >> type;
    cin.ignore();
    My plan is to use the variable 'type' to store the calculation type ( +,-,*,/) but am unsure of what to use to perform the calculation.

    A Switch was my initial plan, but from what I know, the syntax requires either an integer or a char to be contained in the "switch ( this bit ) { ... ]"

    I need a recommendation on what to use, if not a Switch.

    Hope that makes sense.

    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Like
    case '+': answer = a + b; break;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Use a switch.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    do
    Code:
    switch(type){
        case '+':
            //code
            break;
        case'-':
            //code
            break;
        default:
            //handle error
    }

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The code inside main() needs to be enclosed inside a pair of braces:
    Code:
    int main() {
      // code
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 06:00 AM
  2. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 10:59 PM
  3. Making control...
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2004, 01:42 PM
  4. Replies: 2
    Last Post: 01-13-2003, 12:28 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM

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