Alright, so I've tried a couple things to get my bool to return what I want it to return and I can't get it to do what I want. If I put what I put below (count > 0 or count !=) it allows anything to return as true. If I put (count = 0) it doesn't allow anything but "+" to return true.
Disclaimer: I'm not trying to fool anyone, this is my homework. Just looking for a little help.
It's supposed to read in a mathematical function or commands C (Clear) or X(exit), as you can see in Operators. If it's one of those operators it's supposed to send it back to the main program as "Fun". If it's not, it's supposed to return false.
As you can see when I put Count != 0 in the Return False part, it does everything right with the correct operators, but it gives funky symbols or the minus sign if you put in something that's not on the list.
Why do you even need Count in there at all? Couldn't you just put a return false at the end of the function and return true if you get a valid symbol?
Psuedocode:
1 2 3 4 5 6 7
bool GetOperator (constchar Operators [], char & Fun) {
get character into Temp
loop through Operators
if temp == Operators[i]
returntrue;
returnfalse; //if we got here then it obviously wasn't good
}
I tried that, but it won't let me use any of the operators outside of +. This is so strange. I've tested the array to make sure that it's right. Let me just put up my whole program. The idea for this assignment is to use functions for reading in in the numbers, reading in the operators, and then the actual calculations. I know my style is terrible, I haven't really had a chance to work on that.
#include <iostream>
#include <conio.h>
usingnamespace std;
float A;
float B;
float S;
constint arraySize = 9;
constchar Operators [arraySize] = "+-*/CcXx";
float ReadA (float A) {
cout << "Please enter a number." << endl;
cin >> A;
return A;
}
float ReadB (float B) {
cout << "Please enter another number." << endl;
cin >> B;
return B;
}
float Add (float A, float B) {
return S= A + B;
}
float Sub (float A, float B) {
return S = A - B;
}
float Mul (float A, float B) {
return S = A * B;
}
float Div (float A, float B) {
return S = A / B;
}
bool GetOperator (constchar [], char &);
int main ()
{
char Fun;
bool Valid;
do {
cout << "Welcome to the Calculator." << endl;
A = ReadA (A);
do {
do {
cout << "The first number is " << A << endl;
cout << "Please enter a valid function" << endl;
Valid = GetOperator (Operators, Fun);
} while (!Valid);
cout << "You entered " << Fun << endl;
B = ReadB (B);
if (Fun == '+') {
S = Add(A, B);
cout << A << "+" << B << "=" << S << endl;
A = S;
}
elseif (Fun == '-') {
S = Sub(A, B);
cout << A << "-" << B << "=" << S << endl;
A = S;
}
elseif (Fun == '*') {
S = Mul(A, B);
cout << A << "*" << B << "=" << S << endl;
A = S;
}
elseif (Fun == '/') {
S = Div(A, B);
cout << A << "-" << B << "=" << S << endl;
A = S;
}
} while ((Fun != 'C')&&(Fun != 'X'));
} while (Fun != 'X');
return 0;
}
bool GetOperator (constchar Operators [], char & Fun)
{char Temp;
cin >> Temp;
int Count;
Count = 0;
for (int i = 0; i < arraySize; i++)
{
if (Operators [i] == Temp)
{
Fun = Temp;
Count = 1;
returntrue;
}
else
{ returnfalse;
}
}
}
The thing is, you don't want the else/return false into the for loop. Try going over the for loop yourself on paper to convince yourself why you will return false if the operation isn't '+'. Also look at my psuedocode again and compare it to your code.