@SGM
I haven't heard from you for a couple of days, maybe you are busy at work or something.
I hope that I haven't scared you off with my last comment about not writing any new code?
If that is the case, don't worry, we are not far off getting a really good solution.
So our design is essentially this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void UseCalculator() {
//UseCalculator function
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Does simple calculations using 2 floats and an operator
//Author:
//State: Incomplete design only
//problems:
//to do list:
//Last edited:
//////////////////////////////////////////////////////////////////////////////////////////////////////
//get a number
//check that it is valid
//get an operator
//check that it is valid
//get a number
//check that it is valid
//Calculate the answer
} //end of function UseCalculator
|
And this translates into having these functions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
//variable declarations
//variable float FirstNumber;
//variable float SecondNumber;
//variable char Operator;
/get a number
GetFloat();
//check that it is valid
IsValidFloat(FirstNumber);
//get an operator
GetOperator();
//check that it is valid
IsOperator(Operator);
//get a number
GetFloat();
//check that it is valid
IsValidFloat(SecondNumber);
//Calculate the answer
CalcAnswer(FirstNumber, Operator, SecondNumber )
|
Going to the next stage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
void UseCalculator() {
//UseCalculator function
////////////////////////////////////////////////////////////////////////////////////////////////////////
// Does simple calculations using 2 floats and an operator
//Author: TheIdeasMan
//State: Incomplete function skeletons
//problems: it s all going well
//to do list: function definitions
//Last edited: 28/06/2012
//////////////////////////////////////////////////////////////////////////////////////////////////////
//variable declarations & initialisation
//variable float FirstNumber;
float FirstNumber =0.0;
//variable float SecondNumber;
float SecondNumber = 0.0;
//variable char Operator;
char Operator = 'a'; //purposely set it to something invalid
//variable bool ValidFloat;
bool ValidFloat = false; //purposely set it to something invalid
;
//variable bool ValidOperator;
bool ValidOperator = false ; //purposely set it to something invalid
//get a number
FirstNumber = GetFloat();
//check that it is valid
ValidFloat = IsValidFloat(FirstNumber);
//get an operator
Operator = GetOperator();
//check that it is valid
ValidOperator = IsOperator(Operator);
//get a number
SecondNumber = GetFloat();
//check that it is valid
ValidFloat = IsValidFloat(SecondNumber);
//Calculate the answer
Answer = CalcAnswer( FirstNumber, SecondNumber, Operator);
}; //end of function UseCalculator
//function definitions outside main
float GetFloat(){
float TheFloat =0.0; //local variable
return TheFloat
};
bool IsValidFloat(float TheNumber){
//return a bool
};
bool IsOperator(char Operator){
//we have already done this one
//return a bool
}
float CalcAnswer( const float FirstNumber, const float SecondNumber, const char Operator) {
float answer = 0.0;
// switch statement depending on what the operator is
//make sure to return answer;
};
|
Right, now for some info on the IsValidFloat function:
There are ways in C++ to check for valid input. cin.ignore is one of them, for example. They are a little bit more involved.
However I think we should keep it simple, and the rest of the code is C, so I am going to explain a C approach.
The C counterparts to cout and cin are functions called printf and scanf. The f means formatted input/output. There are others fprintf and fscanf which deals with files, and sscanf which gets formatted input and returns character array (aka a string, but it's not the same thing exactly as a C++ string)
The good thing about scanf is that it does a bit of error checking which is why we are going to use it
Return Value
On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned. |
If you google "C++ scanf" you will find the full explanation of this function, with all the format parameters. We are going to use the %f which stands for float. BTW %f also works for the double type.
The example on the web page is a bit simplistic because it doesn't check the return value of scanf.
We are going to use it like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
float GetFloat(){
int ScanfReturnVal = 0;
float TheFloat = 0.0;
while(ScanfReturnVal = 0) { //Bad input, it's not a float
printf("Enter a floating point number.\n");
ScanfReturnVal = scanf(%f, &TheFloat); //& is the address of operator if TheFloat is actually
// a float then ScanfReturnVal will be 1. and the
//loop will terminate
}
return TheFloat;
}
|
Now in our design we had GetFloat and IsValidFloat, but the simple code above shows how we have got the input and validated it all in one go, so we don't really need IsValidFloat anymore.
Where you would need a validation function, would be when the float had to be in a range say 10.0 < float <= 20.0.
We already have the IsOperator function, so you should be able to use this write GetOperator in a similar fashion to GetFloat.
There should be enough clues now for you to write CalcAnswer using a switch.
Were nearly there!! I am interested to see how you go, and look forward to your reply