-
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
-
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;
}
-
Which teach yourself book are you working with? There are 3 SAMS ones...which one is it?
-
It is working now! Thanks for the help.
bumfluff: I am using SAMS Teach Yourself C++ in 10 minutes, 2nd edition.
-
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.