Error

/*
* File: main.cpp
* Author: ashish
*
* Created on 17 June, 2011, 3:06 AM
*/


/*
*
*
*/
//Standard C++ headers

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>

/* MySQL Connector/C++ specific headers */

#include <driver.h>
#include <connection.h>
#include <statement.h>
#include <prepared_statement.h>
#include <resultset.h>
#include <metadata.h>
#include <resultset_metadata.h>
#include <exception.h>
#include <warning.h>

#define DBHOST "tcp://127.0.0.1:3306"
#define USER "ashish"
#define PASSWORD "guwardoo"
#define DATABASE "test"

#define NUMOFFSET 100
#define COLNAME 200

using namespace std;
using namespace sql;

int words = 0;


//This is a structure defined for a word (linked list).
struct word
{
char value;
struct word *link;
};



/////////////////////////////////
////////////////////////////////


//Following is the defination of the file Tee
//This tree will store all the words on the file.

struct filetree
{
struct word *word;
int asciisum;
int frequency;
struct filetree *leftlink;
struct filetree *rightlink;
};



////////////////////////////
////////////////////////////


int ascii (struct word *q); // Programmed to calculate the ascii sum of the word.
void createword (struct word **start, char value); // Programmed to create a word.
void word_display(struct word *start); // Programmed to display a single word.
int word_comp(struct word *word1, struct word *word2); // Programmed to compare two words.
void tree_insert (struct filetree **q, struct word *word, int asciisum); // Programmed ot insert the word into the file tree.
void tree_display(struct filetree *start); // Programmed to display the file tree.



//****************************************************************************//
//This class reads a file form the disk.
class file
{
private:
FILE *fp;
public:
file (const char *name, const char *mode)
{
fp = fopen (name, mode);
if (fp==NULL)
{
printf("\nCannot open file: %s", name);
exit(1);
}
}

~file ()
{
fclose(fp);
}

FILE *getfilepointer()
{
return fp;
}
};
/*
*
*/

//static void feed_database (char word[50]);

/*
*
*/
int main(int argc, char** argv)
{
struct word *start = NULL;
struct filetree *tree = NULL;
int a;
char filename[67], ch;
printf("\nEnter the File Name..");
scanf("%s", filename);
file f (filename, "r");
while ((ch = fgetc(f.getfilepointer())) != EOF)
{
a = ((int)ch);

//Checking weather the letter is in desired limit or not.
//ascii value of charcter fecthed from file must be 33 <= letter <=126.
if((a>=97 && a <=122)||(a>=65 && a <= 90))
{
//We will have to create word here
//This letter will be included in the word being created.
createword(&start, ch);
}
else
{
//This will end up this word.
//This word will be stored in the tee.d
//Calculating ascii sum for this word.
//Test Code
int asciisum = 0;
asciisum = ascii(start);
cout<<"\n\n The ASCII sum [";
word_display(start);
cout<<"] is\t"<<asciisum;
tree_insert(&tree, start, asciisum);
start = NULL;
//Test Code Ends
}
}
cout<<"\n\n\n FILE TREE IS BEING DISPLAYED\n";
tree_display(tree);
cout<<"\n\n";
cout<<"Total Number of words present ["<<words<<"]\n\n\n";
return (EXIT_SUCCESS);
}

int ascii (struct word *q)
{
int ascii = 0;
struct word *start;
start = q;
while(start != NULL)
{
ascii += ((int) start->value);
start = start -> link;
}
return ascii;
}



//This function creates a linked list of a word according to the algo.
//CreateWord Fucntion
/*
* This function takes two arguments.
* 1. Address to the pointer containing start adress of word being created.
* 2. Value (New Character) to be inserted at the end of the current word.
*/
void createword (struct word **q, char value)
{
struct word *start;
struct word *temp;

start = *q;

temp = (struct word *) malloc (sizeof(struct word));

if(*q == NULL)
{
cout<<"\n\n\n NEW WORD STARTED\n";
}

cout<<"\n\n New character fetched from file\t"<<value;
cout<<"\n\n[ "<<value<<" ] is being inserted at memory location\t"<<temp;
temp->value = value;
temp->link = NULL;

if(*q == NULL)
{
*q = temp;
}
else
{
while(start->link != NULL)
{
start = start->link;
}
start->link = temp;
}

}

//This Function is programmed to display a word.
/*
* This function takes only one argument.
* 1. the start pointers value of the word to be displayd.
* */
void word_display(struct word *start)
{
while(start != NULL)
{
cout<<start->value;
start = start->link;
}
}

//This Functoin Compares two words.
/* This function takes two arguments/
* 1. Word 1
* 2. Word 2
*
* This function returns on value
*
* 1. returns 1 if words are same and
* 2. returns 0 of words are not same.
*/
int word_comp(struct word *word1, struct word *word2)
{
while(word1 != NULL || word2 != NULL)
{
if(word1->value != word2->value)
break;
word1 = word1->link;
word2 = word2->link;
}

if(word1 == NULL && word2 == NULL)
{
return 1;
}
else
{
return 0;
}

}

// Following function inserts a new node into FILE TREE
//this function intakes three arguments
/* 1. It takes the start address of the file tree.
* 2. It takes the start address of the word being insterted.
* 3. It takes the ascii sum of all the characters in the word.
*/
void tree_insert (struct filetree **q, struct word *word, int asciisum)
{
words ++;
cout<<"\n\n\n******************************************************************************\n\n\n";
cout<<"WE ARE NOW INSERTING [";
word_display(word);
cout<<"] INTO THE FILE TREE.\n\n\n";
struct filetree *temp; // Variable to insert new node into.
struct filetree *start; // Start address (variable) of the file tree.
start = *q;
temp = (struct filetree *) malloc (sizeof(struct filetree )); // Memory allocation to the temp.
temp->frequency = 1;
cout<<"\n\n Node being inserted at memory location\t["<<temp<<" ]";

temp->leftlink = NULL;
temp->rightlink = NULL;

temp->word = word;
temp->asciisum = asciisum;

if(*q == NULL)
{
*q = temp;
}

else
{
while((asciisum > start->asciisum && start->rightlink != NULL ) || (asciisum < start->asciisum && start->leftlink != NULL ))
{
if (asciisum > start->asciisum)

start = start->rightlink;
else

start = start->leftlink;
}

if(asciisum < start->asciisum)
{
start->leftlink = temp;
cout<<"\n\n"<<temp<<" attached to the leftlink of "<<start<<endl;
}
else if(asciisum > start->asciisum)
{
start->rightlink = temp;
cout<<"\n\n"<<temp<<" attached to the rightlink of "<<start<<endl;
}
else if(asciisum == start->asciisum)
{
int word_compare = word_comp(start->word, word);
start->frequency += word_compare;
if(word_compare == 0)
{
start->leftlink = temp;
cout<<"\n\n"<<temp<<" attached to the leftlink of "<<start<<endl;
}
}
}
}


//This Function displays the current tree.

/*
* This function takes one argument.
*
* 1. Start adress of the file tree.
*
*/
void tree_display (struct filetree *start)
{
if(start != NULL)
{
tree_display(start->leftlink);
word_display(start->word);
printf("\t %d \t %d \n\n", start->asciisum, start->frequency);
tree_display(start->rightlink);
}

}

This program above is working fine for small file but when the number of repeated words increase in file it gives wrong output please have a look.
oh i solved it
Topic archived. No new replies allowed.