Trouble with my bool

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const int arraySize = 9;
const char Operators [arraySize] = "+-*/CcXx";

bool GetOperator (const char 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;
						return true;
					}
				if (Count > 0)
					{
						return false;
				    }
	
		}

Last edited on
I don't see anything wrong with that (except that your indentation is a bit odd in some places). Maybe you want a else if?

What exactly is this function supposed to do?
Last edited on
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.
Last edited on
http://img.photobucket.com/albums/v24/Scorpion559/calc1.jpg

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.

http://img.photobucket.com/albums/v24/Scorpion559/calc2.jpg

On this one I put Count < 1 and it won't take anything besides the first slot on my Array the +.
Ah, I got it.

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 (const char Operators [], char & Fun) {
    get character into Temp
    loop through Operators
        if temp == Operators[i]
            return true;
    return false; //if we got here then it obviously wasn't good
}
1
2
3
4
else 
{	
return false;
}


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.

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <conio.h>


using namespace std;

float A;
float B;
float S;
const int arraySize = 9;
const char 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 (const char [], 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;
		}
		else if (Fun == '-') {
			S = Sub(A, B);
			cout << A << "-" << B << "=" << S << endl;
			A = S;
		}
		else if (Fun == '*') {
			S = Mul(A, B);
			cout << A << "*" << B << "=" << S << endl;
			A = S;
		}
		else if (Fun == '/') {
			S = Div(A, B);
			cout << A << "-" << B << "=" << S << endl;
			A = S;
		}

		 

	} while ((Fun != 'C')&&(Fun != 'X'));
	
	} while (Fun != 'X');
return 0;
}

bool GetOperator (const char 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;
						return true;
					}
				else 
				{	return false;
				}
	
		}
}
Last edited on
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.
Thanks for your help. I don't know why I thought that needed to be in the loop. That makes total sense.
Topic archived. No new replies allowed.