Bool Functions, am I right?

Heres what I have I'm just trying to make sure I'm on the right path before i submit, thanks

Question
Write two type bool member functions called atMax and atMin that return a value of true when the value of a counter object has reached its maximum value or its minimum value (zero), respectively. Don't forget the ::operator.


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
  
for max
public:
bool atMax(int no)
{
if (number.isMax(no) != int::npos)
{
return true;
}
else
{
return false;
}
}



for min
public:
bool atMin(int no)
{
if (number.isMin(no) != int::npos)
{
return true;
}
else
{
return false;
}
bump
This doesn't even look like valid C++... What is the purpose of lines 2 and 18?

But now, if everything else is written correctly, then no, these functions will not work properly. You'll be stuck in endless recursion.

A quick example:
1
2
3
4
5
6
7
bool atMax() {
   return (this.counter == _MAX_VALUE_);
}

bool atMin() {
   return (this.counter == 0);
}


I don't know what the purpose of passing a value to yours is, but from what I understand from your question is that counter is already a part of the class, you simply need to use the "this" keyword, or simply omit it since it's implied, to access it. You then need to compare it to whatever the maximum value it is allowed to be, or 0, respectively. Then return true if it is true.
Thank you for the response. Please forgive my ignorance but I put lines 2 and 18 in there so it would loop everytime the value reaches either the maximum or minimum (0). So if I'm understanding you right having the for loops in there would make it a infinite loop? And about the example I implemented it the way I believe it should be, I'm just not positive if it should be (this.true == _MAX_VALUE_) or (true == _MAX_VALUE_)

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
public:
bool atMax(int no)
{
if (number.isMax(no) != int::npos)
{
return (this.true == _MAX_VALUE_);
}
else
{
return false;
}
}



public:
bool atMin(int no)
{
if (number.isMin(no) != int::npos)
{
return  (this.false == 0);
}
else
{
return false;
}
According to the instructions you have supplied, it looks like you already have a class that's been defined, possibly as a counter? It's very hard to understand what you're trying to accomplish since I don't know the entire context of your program.

From the instructions, it says that atMin and atMax are supposed to be part of the said class and it's supposed to check the counter object to check to see if it has reached it's min/max value. Can you supply at least this function so I can help guide you in the right direction?

Edit: Going off of your previous post, I'm assuming there is no code with this either. I'm also assuming we're talking about some pseudo Counter class. What really confuses me is on your other post, you're having problems with converting a for loop to a while loop, but yet in this one, you're trying to solve a problem that involves classes and members. If they're related, then the other one needs to be changed.

But, back to this one. I'm not sure what kind of teacher you have, but this question is severely taken out of context and is almost impossible to know what the "correct" answer should be. My suggestion is that you look at the text book of where this question came from, or wherever else you can turn for help. I'll also provide you with my best guess as to how these functions should operate.

1
2
3
4
5
6
7
8
9
10
11
12
bool Counter::atMax() {
   // This line will first evaluate value and maxVal
   // If they're the same, then the function returns true
   return (this.value == this.maxVal);
}

bool Counter::atMin() {
   // This line will evaluate the opposite of value
   // If it's non-zero, the function will return false
   // If it's zero, the function will return true
   return !this.value;
}
Last edited on
I definitely appreciate the assistance, I'm taking the class online and the questions are ones were supposed to work as practice for later projects? I haven't very good luck getting in touch with her either.
Topic archived. No new replies allowed.