Here is a chunk of my program... without the class and header file...

I am wanting to take that little graph thats in the middle of the code and have the user input a starting point and an ending point...

the class is a stack class with all the functions pop, push, peak...

it is already created...

the problem is where i go from one point to the other... someone help me if you feel like it :)

right now my while loop is infinite.. and i cant think how to take my struct and have it point to the starting block then on to the next few til it is at the end...

struct graph {

	char from;
	char to;
	bool searched;
	
}edges[13];

int main () {

    char start;
    char end;
    char point;
    char nextpoint;
    char visited[12];
    int depth;
    int x;
    search_stack wait;
    search_stack path;
    
    cout << endl;
    cout <<     "        A     B" << endl;
    cout <<    "       /    / | " << endl;
    cout <<   "      C    D  E" << endl;
    cout <<  "    / |  /   /" << endl;
    cout << "   F   G   H  I" << endl;
    cout << "   | / | / | /" << endl;
    cout << "   J   K   L" << endl;
    
    edges[0].from = 'a' ; edges[0].to = 'c'; edges[0].searched = false;
    edges[1].from = 'b' ; edges[1].to = 'd'; edges[1].searched = false;
    edges[2].from = 'b' ; edges[2].to = 'e'; edges[2].searched = false;
    edges[3].from = 'c' ; edges[3].to = 'f'; edges[3].searched = false;
    edges[4].from = 'c' ; edges[4].to = 'g'; edges[4].searched = false;
    edges[5].from = 'd' ; edges[5].to = 'g'; edges[5].searched = false;
    edges[6].from = 'e' ; edges[6].to = 'h'; edges[6].searched = false;
    edges[7].from = 'f' ; edges[7].to = 'j'; edges[7].searched = false;
    edges[8].from = 'g' ; edges[8].to = 'j'; edges[8].searched = false;
    edges[9].from = 'g' ; edges[9].to = 'k'; edges[9].searched = false;
    edges[10].from = 'h' ; edges[10].to = 'k'; edges[10].searched = false;
    edges[11].from = 'h' ; edges[11].to = 'l'; edges[11].searched = false;
    edges[12].from = 'l' ; edges[12].to = 'i'; edges[12].searched = false;
    
    cout << endl << "Please input the point you would like to begin from. ";
    cin >> start;
    cout << endl << "At what point would you like to end? ";
    cin >> end;
    
    point = start;
    depth = 0;
    
    if (point == end) {
    
    	cout << "The only point visited was: " << point << endl << endl;
    
    }
    
    while (point != end) {
        
          
    	wait.push(point, depth);
    	cout << "The following points are waiting: " << wait.peek() << endl;
    	wait.pop();
        path.push(point, depth);
    	depth++;
    	for (x = 0; x < 13; x++){
            if (point = edges[x].from) {
                      edges[x].searched = true;
                      point = edges[x].to;
            }
        }
        
                   
        if (point == end) {
                  
                  
                  cout << "The path taken is: ";
               /*  for (int x = depth; x >= 0; x--){
                      visited[x] = path.peek();
                      cout << visited[x];
                      path.pop();
                  }  */
                  break;
        }
    }
    
    cin >> start;
    return 0;
    
}

Recommended Answers

All 4 Replies

How to slow potential responses...

Here is a chunk of my program... without the class and header file...

Lacking enough information to keep me from being able to compile it and benefit from the compiler's diagnostic messages.

Do you intend for this to be a comparison rather than an assignment?

if (point = edges[x].from) {

alright here is some more of the code...

and the reason why i did

if (point = edges[x].from) {

was for the fact of... how do you know what point is unless you run a comparision.. the only thing is.. there are some points that have multiple other paths? i am lost on how you add them all to a stack and then pop the one you need back to another stack

here is you some code.

#include <iostream>
#include "jnh_search_stack_class.cpp"
using namespace std;

struct graph {

	char from;
	char to;
	bool searched;
	
}edges[13];

int main () {

    char start;
    char end;
    char point;
    char nextpoint;
    char visited[12];
    int depth;
    int x;
    search_stack wait;
    search_stack path;
    
    cout << endl;
    cout <<     "        A     B" << endl;
    cout <<    "       /    / | " << endl;
    cout <<   "      C    D  E" << endl;
    cout <<  "    / |  /   /" << endl;
    cout << "   F   G   H  I" << endl;
    cout << "   | / | / | /" << endl;
    cout << "   J   K   L" << endl;
    
    edges[0].from = 'a' ; edges[0].to = 'c'; edges[0].searched = false;
    edges[1].from = 'b' ; edges[1].to = 'd'; edges[1].searched = false;
    edges[2].from = 'b' ; edges[2].to = 'e'; edges[2].searched = false;
    edges[3].from = 'c' ; edges[3].to = 'f'; edges[3].searched = false;
    edges[4].from = 'c' ; edges[4].to = 'g'; edges[4].searched = false;
    edges[5].from = 'd' ; edges[5].to = 'g'; edges[5].searched = false;
    edges[6].from = 'e' ; edges[6].to = 'h'; edges[6].searched = false;
    edges[7].from = 'f' ; edges[7].to = 'j'; edges[7].searched = false;
    edges[8].from = 'g' ; edges[8].to = 'j'; edges[8].searched = false;
    edges[9].from = 'g' ; edges[9].to = 'k'; edges[9].searched = false;
    edges[10].from = 'h' ; edges[10].to = 'k'; edges[10].searched = false;
    edges[11].from = 'h' ; edges[11].to = 'l'; edges[11].searched = false;
    edges[12].from = 'l' ; edges[12].to = 'i'; edges[12].searched = false;
    
    cout << endl << "Please input the point you would like to begin from. ";
    cin >> start;
    cout << endl << "At what point would you like to end? ";
    cin >> end;
    
    point = start;
    depth = 0;
    
    if (point == end) {
    
    	cout << "The only point visited was: " << point << endl << endl;
    
    }
    
    while (point != end) {
        
          
    	wait.push(point, depth);
    	cout << "The following points are waiting: " << wait.peek() << endl;
    	wait.pop();
        path.push(point, depth);
    	depth++;
    	for (x = 0; x < 13; x++){
            if (point = edges[x].from) {
                      edges[x].searched = true;
                      point = edges[x].to;
            }
        }
        
                   
        if (point == end) {
                  
                  
                  cout << "The path taken is: ";
               /*  for (int x = depth; x >= 0; x--){
                      visited[x] = path.peek();
                      cout << visited[x];
                      path.pop();
                  }  */
                  break;
        }
    }
    
    cin >> start;
    return 0;
    
}
/**********************************************************************
*                                                                     *
*                                                                     *
*                                                                     *
*    Contents: Class definition for a stack neccessary for searching  *
*              a graph of points defined be characters and including  *
*              the depth of the search.                               *
*                                                                     *
*    Defines:                                                         *
*         search_stack_rec:   This is a type definition for a record  *
*                             to be used in the search stack class.   *
*                             It contains the point name as a         *
*                             character and the current search depth  *
*                             as an integer value.  It also contains  *
*                             a pointer to the next element in the    *
*                             stack.                                  *
*                                                                     *
**********************************************************************/
#ifndef   JNH_SEARCH_STACK_CLASS_H_
#define   JNH_SEARCH_STACK_CLASS_H_

//Data type used by the search stack class.  Available to user
//accessing the class.

typedef struct search_stack_rec {
     
     char                point;    //The name of the point.
     int                 depth;    //The depth of the search.
     search_stack_rec    *next;    //The next element in the stack.
     
} search_stack_rec;

//This is the class definition.  For the outline of the functions,
//consult the file "jnh_search_stack_class.cpp".

class search_stack {
     
     private:
          //search_stack_top always points to the top of the stack
          //unless its value is NULL.
          
          search_stack_rec    *search_stack_top;
     
     public:
          
          search_stack();
          void                push(char new_point, int new_depth);
          void                pop();
          search_stack_rec    *peek();
          ~search_stack();
          
};

#endif
/**********************************************************************
*                                                                     *
*                                                                     *
*                                                                     *
*    Contents: Class definition for a stack neccessary for searching  *
*              a graph of points defined be characters and including  *
*              the depth of the search.                               *
*                                                                     *
*    Functions:     search_stack()                                    *
*                   void push(char new_point, int new_depth)          *
*                   void pop()                                        *
*                   search_stack_rec *peek()                          *
*                   ~search_stack()                                   *
*                                                                     *
**********************************************************************/
#include <iostream>
#include "jnh_search_stack_class.h"

using namespace std;

/**********************************************************************
*                                                                     *
*    Function: search_stack()                                         *
*                                                                     *
*    Local variables:    none                                         *
*                                                                     *
*    Purpose:  This is a constructor for the search_stack class.  It  *
*              initializes the top of the stack to NULL               *
*                                                                     *
**********************************************************************/

search_stack::search_stack() {
     //Initialize the top pointer to NULL.
     search_stack_top = NULL;
}

/**********************************************************************
*                                                                     *
*    Function: void push(char new_point, int new_depth)               *
*                                                                     *
*    Local variables:    char                new_point                *
*                        int                 new_depth                *
*                        search_stack_rec    *new_rec                 *
*                                                                     *
*    Purpose:  This function accepts the new values for the record to *
*              be pushed onto the search stack.  It then creates the  *
*              space for the record, fills the record with values and *
*              then it inserts the record as the new top of the.      *
*              search stack.                                          *
*                                                                     *
**********************************************************************/
void search_stack::push(char new_point, int new_depth) {
     search_stack_rec    *new_rec;

     //Create the space for the new record.
     new_rec = new search_stack_rec;

     //Assign the values to the new record.
     new_rec->point = new_point;
     new_rec->depth = new_depth;
     new_rec->next = NULL;

     //Insert the new record as the new top of the stack.
     new_rec->next = search_stack_top;
     search_stack_top = new_rec;
}

/**********************************************************************
*                                                                     *
*    Function:void pop()                                              *
*                                                                     *
*    Local variables:    search_stack_rec    *delete_rec              *
*                                                                     *
*    Purpose:  This function removes the top record from the stack    *
*              and frees the allocated memory for it.                 *
*                                                                     *
**********************************************************************/

void search_stack::pop() {
     search_stack_rec    *delete_rec;

     //If the stack is not empty.
     if(search_stack_top != NULL) {
          //Remove the top element.
          delete_rec = search_stack_top;
          search_stack_top = search_stack_top->next;
          //Free the allocated memory.
          delete delete_rec;
     }
}

/**********************************************************************
*                                                                     *
*    Function: search_stack_rec *peek()                               *
*                                                                     *
*    Local variables:    none                                         *
*                                                                     *
*    Purpose:  This function will simply return a pointer to the      *
*              current top of the stack so the value can be read.     *
*                                                                     *
**********************************************************************/

search_stack_rec *search_stack::peek() {
     //Return a pointer to the current top of the stack.
     return search_stack_top;
}

/**********************************************************************
*                                                                     *
*    Function: ~search_stack()                                        *
*                                                                     *
*    Local variables:    search_stack_rec    *delete_rec              *
*                                                                     *
*    Purpose:  This function is a destructor for the search stack     *
*              class.  Its purpose is to check if there are records   *
*              left on the stack when the class object is destoyed.   *
*              If there are, it will go through the stack and clean   *
*              all memory before it exits.                            *
*                                                                     *
**********************************************************************/

search_stack::~search_stack() {
     search_stack_rec    *delete_rec;

     //If records remain on the stack
     if(search_stack_top != NULL) {
          cout << endl << "Warning on exit: Search stack still full." << endl;
          cout << "Emptying search stack before exit." << endl;

          //Go through the entire stack list
          while(search_stack_top != NULL) {
               delete_rec = search_stack_top;
               search_stack_top = search_stack_top->next;
               //Free all allocated blocks of memory.
               delete delete_rec;
               cout << "Element deleted." << endl;
          }
          cout << "Search stack is empty.  Exiting." << endl;
     }
}

and the reason why i did

if (point = edges[x].from) {

was for the fact of... how do you know what point is unless you run a comparision..

Typically a comparison is written like this.

if (point == edges[x].from) {

As you have it written, using an assignment, the if will be true whenever edges[x].from is nonzero.

[edit]This is not related to your problem, but you're not really doing the following, are you?

#include "jnh_search_stack_class.cpp"

If so, wander through [thread=21015]this thread[/thread].

got it working.

thanks dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.