I have created a calcuator that takes in single ints and appends them together to make larger numbers and can do basic functions. Now I have to take this program the .cpp and the .h files and convert them to allow decimal point numbers. I am thinking about changing all the ints to floats but it still doesn't allow me to type in the "." any idea how to fix this?

#include "StdAfx.h"
#include "Calculator.h"

#include<string>
#include<sstream>
#include<iostream>


// The Calculator Implementation (How it works)

Calculator::Calculator(void)
{ 
    accumulator=0;
    display=0;
    pendingOperation=NONE;
    mode=ENTERING;

}

// The equals key has been pressed!
void Calculator::equalsKey(void) {
    // TODO: Should compute any pending operations.
    Calculator::applyPending();

}

std::string Calculator::getDisplay(void) {

    std::ostringstream stringConverter;
    switch(mode) {
        case DISPLAYING:
            stringConverter << accumulator;
            return stringConverter.str();   // Break not nesc after return 
        case ENTERING:
            stringConverter << display;
            return stringConverter.str();   // Break not nesc after return
        case ERROR:
            return std::string("Error");    // Break not nesc after return
        default:
            return std::string("BAD MODE"); // Break not nesc after return
    } 
}


void Calculator::digitKey(int c) {

    switch(mode) {
        case DISPLAYING:
            display=c;
            mode=ENTERING;
            break;
        case ENTERING:
            display=(10*display)+c;
            break;
        case ERROR:
            display=c;
            mode=ENTERING;
            break;
    }
}


std::string Calculator::getMode(void) {
    switch(mode){
        case ENTERING:
            return std::string("Entering");
        case DISPLAYING:
            return std::string("Displaying");
        case ERROR:
            return std::string("Error");
        default:
            return std::string("TODO");
    }
}

std::string Calculator::getAccumulator(void) {

    std::ostringstream stringConverter;
    if(mode==ERROR) {
        return std::string("Error");   
    } else {
        stringConverter << accumulator;
        return stringConverter.str(); 
    } 
}

void Calculator::applyPending(void) {

    switch(pendingOperation){
        case ADD:
            accumulator=accumulator+display;
            mode=DISPLAYING;
            break;
        case SUB:
            accumulator=accumulator-display;
            mode=DISPLAYING;
            break;
        case MULT:
            accumulator=accumulator*display;
            mode=DISPLAYING;
            break;
        case DIV:
            if(display==0){
                mode=ERROR;}
            else {
                accumulator=accumulator/display;
                mode=DISPLAYING;
            }
            break;
        case NONE:
            accumulator=display;
            mode=DISPLAYING;
            break;
    }

}



void Calculator::operationKey(Operation o) {

    switch(mode){
        case ENTERING:
            switch (o){
                case ADD:
                    Calculator::applyPending();
                    pendingOperation=ADD;
                    break;
                case SUB:
                    Calculator::applyPending();
                    pendingOperation=SUB;
                    break;
                case MULT:
                    Calculator::applyPending();
                    pendingOperation=MULT;
                    break;
                case DIV:
                    Calculator::applyPending();
                    pendingOperation=DIV;
                    break;
            }
            break;
        case DISPLAYING:
            switch (o){
                case ADD:
                    pendingOperation=ADD;

                    break;
                case SUB:
                    pendingOperation=SUB;

                    break;
                case MULT:
                    pendingOperation=MULT;

                    break;
                case DIV:
                    pendingOperation=DIV;

                    break;
            }
            break;
        case ERROR:
            break;
    }
}


void Calculator::clear(void){

    accumulator=0;
    display=0;
    pendingOperation=NONE;
    mode=ENTERING;

}

void Calculator::deleteLastDigit(void){

    if(mode=ENTERING){
    display=display/10;
    }

}


// Get the pending operation's name (as a printable string)
std::string Calculator::getPending(void) {

    switch(pendingOperation) {
    case ADD:
        return std::string("add");

    case SUB:
        return std::string("sub");

    case MULT:
        return std::string("mult");

    case DIV:
        return std::string("div");

    default:
        return std::string("unknown");
    }
}

It might need a bit of a re-write, but one way is to put your input into a stringstream and use the '>>' operator to convert to double or whatever type you need, and do your calulations with that.

Edited 3 Years Ago by tinstaafl

Here's a very simple implementation of a console calculator using stringstream to hold the values and the operand. This code doesn't have much in the way of validation or error checking, but it should demonstrate how well using a stringstream works:

#include "stdafx.h"
#include <iostream>
#include <sstream>
using namespace std;

void clrscr()
{
    cout << string( 100, '\n' );
}
void display(string output)
{
    clrscr();
    cout << output << endl;
    cout << "\t/\t*\t-\n";
    cout << "7\t8\t9\t+\n";
    cout << "4\t5\t6\n";
    cout << "1\t2\t3\4\t=\n";
    cout << "0\t.\n";
}
double result(double firstpart,char operand,double secondpart)
{
    double retval;
    switch(operand)
    {
    case '/':
        retval = firstpart / secondpart;
        break;
    case '*':
        retval = firstpart * secondpart;
        break;
    case '-':
        retval = firstpart - secondpart;
        break;
    case '+':
        retval = firstpart + secondpart;
        break;
    }
    return retval;
}
int _tmain(int argc, _TCHAR* argv[])
{
    char input = ' ';
    bool done = false;
    bool clear = false;
    stringstream ss;
    ss << "";
    while(!done)
    {
        display(ss.str());
        if(clear)
        {
            ss.str("");
            clear = false;
        }
        cin >> input;
        if(input != 'q' && input != 'Q')
        {
            if(ss.str() == "" && input != '/' && input != '*' && input != '-' && input != '+' && input != '=')
            {
                ss << input;
            }
            else
            {
                if(ss.str() != "")
                {
                    ss << input;
                }
            }
            if(input == '=')
            {
                double firstpart = 0;
                char operand;
                double secondpart = 0;
                ss >> firstpart >> operand >> secondpart;
                ss << result(firstpart,operand,secondpart);
                clear = true;
            }
        }

    }
    return 0;
}
This article has been dead for over six months. Start a new discussion instead.