• NOW LIVE! Into the Woods--new character species, eerie monsters, and haunting villains to populate the woodlands of your D&D games.

C++ Help

Macbeth

First Post
For a project in my CS class, I have created the following code to create a binary tree, and use that tree to encrypt information, using the scheme:
0 - move one node left
1 - move one node right
2 - take the information from the current node.

Now I know that the code probably has all kinds of logic errors, but at this point it won't compile! I get a "PCH creation point" error every time. in the past this has usually been becuase I missed a semicolon, but damn if I know where that is. So I'm asking for somebody to pleas tell me where this error is coming from! I'll fix the (likely plentifil) logic errors on my own, i just want to know why the damn thing won't compile:

CryptoTree.h:
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

using namespace std;


class Node {
public:
  char data;
  Node *left;
  Node *right;

  Node (char inp) {
    data = inp;
    left  = NULL;
    right = NULL;
  }
};


/*//////////////////////////////*/


class CryptoTree {

protected:
  Node *head;


  Node *nodeSearch (char info) {
    Node *temp = head;
    Node *decide = head;

    while (decide != NULL) {
      temp = decide;
      if (info < temp->data)  {
        decide = decide->left;
      } else {
        decide = decide->right;
      }
    }
  

  int search (Node *position, char info) {
    if (position != NULL) {
      if (search (position->left, info) == 1 || position->data == info || search (position->right, info) == 1){
	return(1);
      }
    }
    return(0);
  }

  string outSearch(Node *pos, char info, string out) {
    string leftstring = out + "0";
    string rightstring = out + "1";

    if ( pos != NULL ){
      if(pos->data == info){
		  return(out);
	  } else {
		  out = outSearch(pos->left, info, leftstring);
		  out = outSearch(pos->right, info, rightstring);
	  }
	  
	}
	return(out);
  } 


  int treeFull(){
	  int i;
    for ( i = 48; i < 91; i++) {
		if( search(head, ((char)i)) == 0){
	return(0);
      }
    }
    return(1);
  }
	
          

  void keyPrint(Node *target, string code) {
	  
	  string leftcode = code + "0";
	  string rightcode = code + "1";
	  
	  if(target->left != NULL){
		  keyPrint(target->left, leftcode);
	  }
	  cout << code << "2 - " << target->data << endl;
	  if(target->right != NULL){
		  keyPrint(target->right, rightcode);
	  }

	  return;
  }

public:
  CryptoTree () {
	srand(static_cast<unsigned>(time(0)));
	char rand;
	int irand;

	while(treeFull == 0){
		irand = (48 + ((int) 43 * rand() / (RAND_MAX + 1.0)));
		rand = (char)irand;
		insert(rand);
	}
    
  } // constructor

  void insert (char inp) {
    Node *temp = new Node (inp);

    if (head == NULL) {
      head = temp;
    } else {
      Node *seek = nodeSearch (temp->data);
   
      if (temp->data < seek->data) {
        seek->left = temp;
      } else {
        seek->right = temp;
      }
    }
	return;
  } // insert


  void returnKey(){
	  string init;
	  init = " ";
	  cout << "Encryption Key:" << endl;
	  printKey(head, init);
	  return;
  }


  string encrypt(string input){
    char targ;
    string starter = "";
    int max = input.length();
	int j;
    
	for( j = 0; j < max; j++){  
		targ = input[j];
		starter = starter + outSearch(head, targ, starter);
	}
    return(starter);
  }

  string decrypt(string input){
	  int max;
	  int k;
	  node *trav;
	  string out;
	  trav = head;
	  max = input.length();
	  out = ""; 

	  for(k = 0; k < max; k++){
		  if(input[k] == 2){
			  out = out + (trav->data);
		  }else if(input[k] == 0){
			  trav = trav->left;
		  }else{
			  trav = trav->right;
		  }
	  }

	  return(out);
  }


};

cryptoTest.cpp:
Code:
#include "Crypto.h"


int main() {
	CryptoTree Trial;

	Trial.returnKey();


	return(0);
}

I know the lack of commenting is horrible, I was goign to comment it once I had working code. I'll try commenting it now, and post an easier to read version in a while, if nobody can read the code as is.
 

log in or register to remove this ad


node *trav;

Don't you want this Node capitalized, or is there something I'm missing? It's been a bit since I did any C++, and I didn't read this too carefully.
 

That was one thing, I had also copied and pasted some stuff from another file, and missed some important parts of nodesearch by accident. Fixed it now.

Now I beging fighting the logic errors...
 

Good luck with that. I spent quite a while last week sorting logic errors out of our AI project - it was supposed to play Othello, and used a doubly-linked tree of future moves for a basic minmax search. Boy, there was some ugly stuff in there. ;)

--Impeesa--
 
Last edited:

I got it all sorted out. Not the prettiest code, but it works. Then I go to my CS class, and the teacher extended the assignment to Saturday. :\
 

Into the Woods

Remove ads

Top