calculator

This is a discussion on calculator within the C++ Programming forums, part of the General Programming Boards category; This is the first piece of code I have attempted to write in C++ and need some help getting it ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    calculator

    This is the first piece of code I have attempted to write in C++ and need some help getting it to do what I need it to. I am supposed to write a code for a calculator using only pointer and anonymous variables that continuously asks for different operations until the user wants to exit. This is what I have so far and it terminates after asking for the second number. Any help is appreciated, THANK YOU!
    Code:
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    
    int Arithmetic ( int * x , int * y );
    
    
    int main() {
    
    int num1;
    
    int num2;
    
    
    cout << "Please enter an integer value:"; // prompts user to enter number
    cin >> num1; //stores number
    cout << "Please enter a second integer value:"; // prompts user to enter number
    cin >> num2; //stores number
    
    Arithmetic( &num1,&num2 );
    
    return 0;
    
    }
    
    int
     Arithmetic(int * x, int * y)
    
    {
    
    char * charPtr;
    
    int * intPtr2;
    
        
    while ( * charPtr != 'q' ){
    
            charPtr = newchar;
    
            cout << "Please select an operation:(+,-,*,/,%) or enter 'q' to exit"; // prompts user to enter and operation or 0 to exit
    
            cin >> *charPtr; 
    // stores user input
    
            
    switch ( *charPtr )
    
            {
    
                
    case'+':
    
                    *intPtr2 = *x + *y;
    
                    
    break;
    
                
    case'-':
    
                    *intPtr2 = *x - *y;
    
                    
    break;
    
                
    case'*':
    
                    *intPtr2 = *x * *y;
    
                    
    break;
    
                
    case'/':
    
                    *intPtr2 = *x / *y;
    
                    
    break;
    
                
    case'%':
    
                    *intPtr2 = *x % *y;
    
                    
    break;
    
                
    default:
    
                    cout << "invalid operation, enter 'q' to exit";
    
                    
    break;
    
                    }
    
            cout << *intPtr2;
    
        }
    
    return
     0;
    
    }
    

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Please edit your post so that the code doesn't have a crap-load of useless tags in it before you post it.

    Eg.
    [B ][SIZE=2 ][COLOR=#7f0055 ][SIZE=2 ][COLOR=#7f0055 ]#include[/COLOR ][/SIZE ][/COLOR ][/SIZE ][/B ][SIZE=2 ][COLOR=#2a00ff ][SIZE=2 ][COLOR=#2a00ff ]<iostream>[/COLOR ][/SIZE ][/COLOR ][/SIZE ]
    [B ][SIZE=2 ][COLOR=#7f0055 ][SIZE=2 ][COLOR=#7f0055 ]#include[/COLOR ][/SIZE ][/COLOR ][/SIZE ][/B ][SIZE=2 ][COLOR=#2a00ff ][SIZE=2 ][COLOR=#2a00ff ]<cmath>[/COLOR ][/SIZE ][/COLOR ][/SIZE ]

    That is, so your code looks like this
    Code:
    #include<iostream>
    #include<cmath>
    #include<iomanip>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    That is, make sure you paste "text" as opposed to pasting "HTML" or some other overly decorated format.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Also make sure to indent your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. age calculator... :/
    By mikko93 in forum C Programming
    Replies: 3
    Last Post: 09-15-2011, 12:25 PM
  2. C++ Calculator Help plz
    By Caldark in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2010, 01:13 AM
  3. Calculator help
    By andreasleon in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2007, 06:51 PM
  4. Please help with this calculator
    By Asbestos in forum C++ Programming
    Replies: 9
    Last Post: 03-08-2005, 01:00 PM
  5. Need help in RPN calculator
    By Lost__Soul in forum C Programming
    Replies: 14
    Last Post: 05-29-2003, 03:01 PM

Tags for this Thread


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