The following code gets stuck in an infinite loop and i can't figure out where: the first code is the .cxx file. The second is the header and template for the template class that was created.
// FILE: stack1.h (part of the namespace main_savitch_7A)
// TEMPLATE CLASS PROVIDED: stack<Item>
//
// TEMPLATE PARAMETER, TYPEDEFS and MEMBER CONSTANTS for the stack<Item> class:
// The template parameter, Item, is the data type of the items in the stack,
// also defined as stack<Item>::value_type. It may be any of the C++ built-in
// types (int, char, etc.), or a class with a default constructor, a copy
// constructor, and an assignment operator. The definition
// stack<Item>::size_type is the data type of any variable that keeps track of
// how many items are in a stack. The static const CAPACITY is the
// maximum capacity of a stack for this first stack implementation.
// NOTE:
// Many compilers require the use of the new keyword typename before using
// the expressions stack<Item>::value_type and stack<Item>::size_type.
// Otherwise the compiler doesn't have enough information to realize that it
// is the name of a data type.
//
// CONSTRUCTOR for the stack<Item> template class:
// stack( )
// Postcondition: The stack has been initialized as an empty stack.
//
// MODIFICATION MEMBER FUNCTIONS for the stack<Item> class:
// void push(const Item& entry)
// Precondition: size( ) < CAPACITY.
// Postcondition: A new copy of entry has been pushed onto the stack.
//
// Item pop( )
// Precondition: size( ) > 0.
// Postcondition: The top item of the stack has been removed.
//
// CONSTANT MEMBER FUNCTIONS for the stack<Item> class:
// bool empty( ) const
// Postcondition: Return value is true if the stack is empty.
//
// size_type size( ) const
// Postcondition: Return value is the total number of items in the stack.
//
// Item top( )
// Precondition: size( ) > 0.
// Postcondition: The return value is the top item of the stack but the
// stack is unchanged. This differs slightly from the STL stack (where
// the top function returns a reference to the item on top of the stack).
//
// VALUE SEMANTICS for the stack<Item> class:
// Assignments and the copy constructor may be used with stack<Item>
// objects.
#ifndef MAIN_SAVITCH_STACK1_H
#define MAIN_SAVITCH_STACK1_H
#include <cstdlib> // Provides size_t
namespace main_savitch_7A
{
template <class Item>
class stack
{
public:
// TYPEDEFS AND MEMBER CONSTANT -- See Appendix E if this fails to compile.
typedef std::size_t size_type;
typedef Item value_type;
staticconst size_type CAPACITY = 100;
// CONSTRUCTOR
stack( ) {
used = 0;
}
// MODIFICATION MEMBER FUNCTIONS
void push(const Item& entry);
void pop( );
Item seek(size_type n = 1);
// CONSTANT MEMBER FUNCTIONS
bool empty( ) const {
return (used == 0);
}
size_type size( ) const {
return used;
}
Item top( ) const;
private:
Item data[CAPACITY]; // Partially filled array
size_type used; // How much of array is being used
};
}
#include "stack1.template" // Include the implementation.
#endif
// FILE: stack1.template
// TEMPLATE CLASS IMPLEMENTED: stack<Item> (see stack1.h for documentation)
// This file is included in the header file, and not compiled separately.
// INVARIANT for the stack class:
// 1. The number of items in the stack is in the member variable used.
// 2. The actual items of the stack are stored in a partially-filled
// array data[0]..data[used-1]. The stack elements appear from the
// bottom (at data[0]) to the top (at data[used-1]).
#include <cassert> // Provides assert
namespace main_savitch_7A
{
template <class Item>
consttypename stack<Item>::size_type stack<Item>::CAPACITY;
template <class Item>
void stack<Item>::push(const Item& entry)
// Library facilities used: cassert
{
assert(size( ) < CAPACITY);
data[used] = entry;
++used;
}
template <class Item>
void stack<Item>::pop( )
// Library facilities used: cassert
{
assert(!empty( ));
--used;
}
template <class Item>
Item stack<Item>::top( ) const
// Library facilities used: cassert
{
assert(!empty( ));
return data[used-1];
}
template <class Item>
Item stack<Item>::seek(size_type n)
{
assert(n < size());
return data[used-n-1];
}
}
There are only 3 while loops and 1 for loop in your program. Why don't you add a print statement inside each and see which one keeps printing? That will tell you where the infinite loop is.
In back_track:
Change it to return bool.
Change success = true; to returntrue;
Remove !success from the while condition
Add returnfalse; to the end.
Also, you have a variable called user_input which I cannot see defined anywhere.
And do you really want to start x at 1? (Maybe you do, but it is not the norm.)
What kind of name is var? Why not call it size?
You would not normally want assertions in the stack implementation (which are only available in a debug build and cause the program to abort). Exceptions are better.