Peg Assignment Help

I have been stuck on this for over a week trying to just get the basics of this to work. I'm having real trouble getting the constructor to work properly or even at all.

Here is my instructor provided header file which I am unable to edit

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
//  File:  Peg.h
///  Class Peg from Main and Savage
//
//	provides a "peg" that can hold a maximium of 64 disks 
//	of sizes 1 to MAX_INT in diameter.
//
//  CONSTRUCTOR:  Peg(x)
// 	POSTCONDITION the Peg has been initialized to have x disks of size x to size 1
//		on the peg
//
//  CONSTANT functions:
//
//  size_t count ();
//	POSTCONDITIONS: the number of disks on the Peg is returned
//
//  Disk top_Disk ();
//	PRECONDITIONS: the Peg is not empty
//	POSTCONDITIONS: the disk at the top of the Peg is returned
//
//
//  MODIFICATION functions:
//
//  void add (const Disk added_Disk);
//	PRECONDITIONS: the Peg is not full AND the added_Disk is smaller than the top disk
//	POSTCONDITIONS: the Peg has the disk added to the top of the peg
//
//  void remove ();
//	PRECONDITIONS: the Peg is not empty
//	POSTCONDITIONS: the top disk is removed from the Peg 
//
//  FRIENDS
//   	operator <<
//	POSTCONDITIONS: the disk on the peg are printed from the bottom to the top
//

#define MAX_DISKS  64
#include <cstdlib>
#include <cassert>
#include <iostream>

class Peg
{
  public:
    typedef std::size_t Disk;
        Peg(std::size_t init_disks=64);
    std::size_t count() const;
    Disk top_Disk() const;
    void add(const Disk added_Disk);
    void remove ();
       	
    friend std::ostream& operator<< (std::ostream&, const Peg&);

  private:
    std::size_t size;
    std::size_t disks[MAX_DISKS];
};


What I have failed to implement so far

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
// Programmer: Lothp
// Date: 1/26/14
// Assignment: Peg 
// Purpose: Creating classes for the Peg file


#include "Peg.h"
#include <cmath>

  // User enters how many pegs they wish to start with
  Peg::Peg(std::size_t init_disks)
  {
    //const std::size_t MAX_INT = 64;
	//const std::size_t MAX_DISKS = 64;
  
    //std::size_t size = init_disks;
    
	if (init_disks == 0 || init_disks > 64)
	{
	  disks[64];
	}
	else
	{
	  disks[init_disks];
	}
	
    for (int i = 0; i < init_disks; ++i)
    {
      disks[i] = init_disks - i;
	  disks[i].size = init_disks - i;
    }
  }

// Count  
  std::size_t Peg::count() const
  {
    
	std::cout << size << std::endl;
  
  }

// Top Disk
  Peg::Disk Peg::top_Disk() const
  {
    //int i;
    
    // Precondition: Peg cannot be empty
    if (size == 0)
	{
	  std::cout << "Peg is empty" << std::endl;
	}
	
	// POSTCONDITIONS: the disk at the top of the Peg is returned
	if (size != 0)
	{
	  std::cout << "hjhjh";
	}
  }

// Remove
  void Peg::remove()
  {
  
  
  }
  
  // friend std::ostream& operator<< (std::ostream&, const Peg&);
  std::ostream& operator<< (std::ostream& outstream, const Peg& peg)
  {
    for (int i = 0; i < peg.size; i++)
      {
        outstream << peg.disks[i] << std::endl;
        return outstream;
      }
  }



And my failed attempt to test my fails

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
#include <iostream>
#include "Peg.h"

using namespace std;


int main ()
{
  //ofstream outstream;
  ostream& operator<< (ostream& outstream, const Peg& peg);

  // Constructor
  Peg peg(26);

  // see pegs maybe
  cout << peg;  

  // Count function
  //Peg::count() const;
  
  // Top disk function
  //Peg::top_Disk(x) const;
  
  return 0;
}



Any help would be greatly appreciated
So far all it does is output a "26" or what ever is in the constructor of the test file "Peg peg(26)"
forgot to comment it out but line 30 of the implementation does not work either
So far all it does is output a "26" or what ever is in the constructor of the test file "Peg peg(26)"

It if compiles, you haven't presented us with the source code you're using. There is a syntax error in your Peg constructor. Variables of type std::size_t don't have members named size. You also don't set the class member size anywhere in the constructor.

I would expect the constructor's implementation to look like the following:

1
2
3
4
5
6
7
8
9
 // User enters how many pegs they wish to start with
  Peg::Peg(std::size_t init_disks) : size(init_disks)
  {
      if ( size > MAX_DISKS )
         size = MAX_DISKS ;

      for (unsigned i=0; i<size; ++i)
         disks[i] = size-i ; 
   }
Thank you kindly so far
For awhile I couldn't figure out how to set the size, I herp derped the whole time and was doing it wrong.
In the constructor how would I make it so that if this happened
Peg peg()

I need to make if this happens that it gets set to 64
Thought about adding another constructor to the header file with an empty parameter list, but I am not able to change the header file.

thought about doing
if (size > MAX_DISKS || NULL)
But I know NULL are not allowed here, what else could I use?
I need to make if this happens that it gets set to 64
Thought about adding another constructor to the header file with an empty parameter list, but I am not able to change the header file.

That's already taken care of by the default argument supplied in the method's declaration in Peg.h.

[Edit: Although, if you want a default constructed object you should drop the parentheses. Peg peg(); is a function named peg that returns a variable of type Peg. ]
Last edited on
You cire be genius
one last thing then I'll go back to trying it on my own
How can I display the size at a certain point within the array?
I know this won't work
disks[i].size

[Edit: nor cout << peg.size[1];
error: invalid types /size_t[int]/ for array subscript ]
Last edited on
Under the section // see pegs maybe, how would you use the << operator instead of cout and how would you see an element of the array?

Also under the //top disk function how would it know what the disk would be?

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

#include <iostream>
#include "Peg.h"

using namespace std;


int main ()
{
  //ofstream outstream;
  ostream& operator<< (ostream& outstream, const Peg& peg);

  // Constructor
 // Peg peg(26);
     Peg peg();
  // see pegs maybe
  cout << peg;  

  // Count function
  //Peg::count() const;
  
  // Top disk function
  //Peg::top_Disk(x) const;
  
  return 0;
}


Last edited on
Topic archived. No new replies allowed.