Need Help With Single Peg Hanoi Tower

So I'm working on a program that uses a single peg and has disks on it with different diameters, much like the Tower of Hanoi, however it has only a single peg, not multiple. I have gotten some of the code however, after days of working I hit a roadblock. I have commented in the program what I'm stuck on for easier use. Any feedback and help would be greatly appreciated. I was also provided with the header class as I will give below. Also, more specifically, the problem I have been running into is in my implementation class, I can't get the correct output/logic with the stuff below my Counting Function.

Last edited on
Remove every cin and cout from your member functions. The operator<< and main() are the only ones that should talk to the user.

Do not define a macro; use a named const instead. You can have it as public member of the Peg.

Be consistent. You have the "64" appear in multiple places even though you have a named value for that purpose.

Be consistent. If you have type Disk, then use it where values have that type.

// I know the other commands go here, such as outputting the size of the disks
// and the count of the disks,
// but i can't figure out how to implement it

Ask yourself what is a Disk and where does it store its size?
That's what I have always been confused about, if i do an if statement in the implementation class then how do I can that statement in the main class? Like take my first if statement in the implementation class, the inputting initial disks and if its greater than 64 just make it 64, how would I do the cin and cout for that statement in the main()? Sorry I've always been really bad at classes.
A function is a function.
Calling a member function of a type requires an object of that type.

1
2
3
4
5
6
7
8
9
10
11
12
#include <vector>
#include <iostream>
using Peg = std::vector<double>;

int main() {
  std::size_t foo = 0;
  std::cout << "Hello\n";
  std::cin >> foo;
  Peg leg( foo );
  std::cout << leg.size() << '\n';
  return 0;
}
Last edited on
Ok I need help with one more thing, and thank you keskiverto, you've helped me along a lot. However I'm running into one more problem. My updated implementation class is as follows:

Last edited on
You have it return nothing sensible now.

The a and b are uninitialized integers. Totally unknown values. The function promises to return a Peg::Disk. A copy of the Disk that is at the top of the peg.

If a function promises to return a Disk, a Disk it must return. There is no option to not return a Disk.

Some functions opt to throw an exception when they cannot do what they should. Uncaught exceptions "crash" the program.


Other functions return a bogus Disk, but the caller cannot possibly know that. Or could it? Can you imagine a Disk from which you (the caller) could say that it will never occur as "normal value"?

Look at:
http://www.cplusplus.com/reference/string/string/find/
The find returns the position (a number), where it found the searched text, but if there are no matches it still returns a "position", a known numeric value that the caller can test and see that it is not a real position.

Use that std::string::find as inspiration.
Topic archived. No new replies allowed.