Thread: Newbie help

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Newbie help

    Hi there, I am fairly new to programing and can't seem to figure out what I am doing wrong here. I am working out of a SAMS teach yourself C++ book, and am busy on a calculator.

    When I compile and run the code the answer generated is not correct. It seems as if the input is not affecting the answer.

    I would greatly appreciate any advise.

    Code:
    #include <iostream>
    
    #include "PromptModule.h"
    #include "ErrorHandlingModule.h"
    
    using namespace std;
    
    char GetOperator(void)
    {
          char theOperator = 0;
          
          cout << "Operator: ";
          cin >> theOperator;
    }
    
    float GetOperand(void)
    {
          float theOperand = 1;
          
          cout << "Operand: ";
          cin >> theOperand;
    }
    
    float Accumulate(const char theOperator,const float theOperand)
    {      
          static float myAccumulator = 0; // Initialized to 0 when the program starts
          
          switch(theOperator)
          {
                 case '+': myAccumulator = myAccumulator + theOperand; break;
                 
                 case '-': myAccumulator = myAccumulator - theOperand; break;
                 
                 case '*': myAccumulator = myAccumulator * theOperand; break;
                 
                 case '/': myAccumulator = myAccumulator / theOperand; break;
                 
                 default: cout << "Please enter +,-,* or /." << endl;
          };
          
          return myAccumulator;
    }
    
    int main(int argc, char* argv[])
    {
        SAMSErrorHandling::Initialize();
        
        do
        {    
              try
              {
                  char Operator = GetOperator();
                  float Operand = GetOperand();
              
                  cout << Accumulate(Operator,Operand) << endl;
              }
              catch(...)
              {
                  SAMSErrorHandling::HandleNotANumberError();
              };
        }
        while (SAMSPrompt::UserWantsToContinueYOrN ("More Division?"));
        
        return 0;
    }
    Thanks
    J2ke

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    your compiler should have produced warnings that functions GetOperator() and GetOperand() to not return a value. You should treat warnings as if they are errors because most of the time they are errors as they are in this case.
    Code:
    float GetOperand(void)
    {
          float theOperand;
          
          cout << "Operand: ";
          cin >> theOperand;
         return theOperand;
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Which teach yourself book are you working with? There are 3 SAMS ones...which one is it?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    It is working now! Thanks for the help.

    bumfluff: I am using SAMS Teach Yourself C++ in 10 minutes, 2nd edition.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    HMMMMM...I got that book and thought it was rubbish as it sticks to exactly the same program all the way through and after a while it just started to say right so lets do this now without an expanation of how it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  2. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  3. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  4. C++ newbie / linux not so newbie question
    By goldmonkey in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2003, 12:27 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM