Thread: C++ Beginner Simple Calculator

  1. #1
    Registered User
    Join Date
    Oct 2001

    Question C++ Beginner Simple Calculator

    I am writing a program that models a simple calculator. The user will imput an operand and number and the calculator, starting w/zero, will continually accumulate a new sum, until it is promped to quit. The only operands I am using are (+,-,*,/,^, and q for quit).
    I don't understand how the computer can intake the operand and number in the same command and read them and do the computation. I understand how it can read them separately. I know I can accumulate using a loop, but still lost.
    * Any direction will be appreciated!

  2. #2
    Registered User
    Join Date
    Sep 2001
    One way to do it is to read the entire input into a string. Parse the string separating number ( double would be the most flexible ) from the operators. Put both in separate queues (first in first out array ). When done take the first two numbers and use the first operator, put that value in your accumulator and get the next two numbers and perform the operation of the second operator, and so on. This method has no operator presidence but you could put that in with multiple passes

    a hint on the string to float would be the atof function.

    And remember if its not an operator or a space it should be checked to see if its a number. If it is none of the above its bad data.

  3. #3
    read them all in as characters.
    If you are in a loop, just use if statements.
    such as first read = 2

    if(!isdigit(char)){ // if char is not a digit
    operand = char;
    save digit // arbitrary

    This is really simple if you have a stack class.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    You want the calculator to be able to evaulate expressions like

    3 + 2 * 7


    It's actually not as trivial as it may seem initially.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple calculator in C
    By AlphaMac in forum C Programming
    Replies: 11
    Last Post: 10-14-2006, 01:26 AM
  2. Calculator - Past the basics of 2+2
    By Revekius in forum C Programming
    Replies: 11
    Last Post: 07-29-2006, 11:36 PM
  3. help with simple calculator (beginner)
    By philthemn in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2006, 04:05 AM
  4. Debugging Calculator
    By raytro2 in forum C Programming
    Replies: 3
    Last Post: 02-06-2003, 04:07 PM
  5. Java Calculator
    By datainjector in forum A Brief History of
    Replies: 7
    Last Post: 11-12-2002, 08:39 PM