0

This should display 5 lines of code that say "test", yet I am only getting 4 and it is never reaching zero, since the output never displays, "the test is over". What is wrong with this implementation? I am quite sure it's an error in my next() function.

Any help would be great.
Thanks in advance!

#include <iostream>
#include <cassert>
using namespace std;

class CountDown
{
  public: //Application Programmer Interface
	CountDown(int start); // it is set to start
	void next(); // subtracts one from it
	bool end()const; //
 private:
	int it;
};

CountDown::CountDown(int start)
{
    it = 0;
    it = start;
}

void CountDown::next()
{
    it = it - 1;
}

bool CountDown::end() const
{
    if (it <= 0)
    cout << "The countdown is now over" << endl;
}

int main()
{
	for( CountDown i = 5 ; ! i.end(); i.next())
		std::cerr << "test\n";

}
5
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by predator78
1

CountDown::end() doesn't return anything yet you declared it to return bool. That makes you a liar. ;)

0
#
for( CountDown i = 5 ; ! i.end(); i.next())
#
std::cerr << "test\n";

CountDown::end() function checks if it has reached 0 but not returns a boolean value. You must return true if it is zero in order to use it with the for loop above.

0

Lol gotta love geek humor, pretty sad I even laugh at myself when I find myself in a bad case of mislogicitus.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.