itgeeksingh
Technical User
I wrote this program in windows and works fine in windows xp. But now my prof is trying it in UNIX and he is geting segmentation fault in option 2. here is the main.cpp file
#include <cstdlib>
#include <iostream>
#include "interval_tree.h"
using namespace std;
//the menu options...
int show_menu()
{
cout << "\n/**** Options ****/";
cout << "\n1. Create tree";
cout << "\n2. Insert interval";
cout << "\n3. Search interval";
cout << "\n4. Print";
cout << "\n0. Exit";
cout << "\n\nSelect any option: ";
int option;
fflush(stdin);
cin >> option;
return option;
}
//parse a given string in order to build an interval
IntInterval* parse_interval(char* data)
{
char* ptr = strchr(data,',');
int pos=(ptr-data);
if(pos > 0)
{
char* first = strtok (data,",");
char* second = strtok (NULL, ",");
return new IntInterval( atoi(first), atoi(second));
}
return NULL;
}
//draw the tree in the console
void serialize(TreeNode *_tree, int _level)
{
for(int i=0; i<_level; i++)
cout << "--";
_tree->get_interval()->Print();
cout << endl;
if( _tree->get_left()->get_interval() != NULL)
serialize(_tree->get_left(),_level+1);
else
if( _tree->get_right()->get_interval() != NULL)
{
for(int i=0; i<_level+1; i++)
cout << "--";
cout << "L\n";
}
if( _tree->get_right()->get_interval() != NULL)
serialize(_tree->get_right(),_level+1);
else
if( _tree->get_left()->get_interval() != NULL)
{
for(int i=0; i<_level+1; i++)
cout << "--";
cout << "R\n";
}
}
int main(int argc, char *argv[])
{
IntervalTree* _intTree = NULL;
system("color A");
//loop
while(true)
{
int option = show_menu();
switch(option)
{
case 0: //exit
return 0;
case 1: //create tree
{
_intTree = new IntervalTree();
puts("The Tree was created succesfully!\n");
}
break;
case 2: //insert node
if(_intTree != NULL)
{
printf("Type an Interval a,b: ");
fflush(stdin);
char data[10];
gets(data);
//inser the node
_intTree->Insert( parse_interval(data));
}
else
puts("No tree created!\n");
break;
case 3: //serch interval
if(_intTree != NULL)
{
printf("Type an interval a,b to search: ");
fflush(stdin);
char data[10];
gets(data);
Stack<void *> * _intervals = _intTree->IntervalSearch( parse_interval(data));
while(!_intervals->Empty())
{
IntInterval* _int = (IntInterval*)_intervals->Pop();
_int->Print();
printf("--");
}
printf("\n");
}
else
puts("No Tree created!\n");
break;
case 4: //print the tree
if(_intTree != NULL )
if( _intTree->get_root()->get_left()->get_interval() != NULL)
serialize(_intTree->get_root()->get_left(),0);
else
puts("Empty tree!!!");
else
puts("No Tree created!\n");
break;
}
system("pause");
system("cls");
}
}
I am new to unix. i tried to compile main.cpp using g++, it gives me following errors. Somebody could help in correcting code.
g++ main.cpp
main.cpp: In function `class IntInterval * parse_interval(char *)':
main.cpp:28: implicit declaration of function `int strchr(...)'
main.cpp:28: initialization to `char *' from `int' lacks a cast
main.cpp:32: implicit declaration of function `int strtok(...)'
main.cpp:32: initialization to `char *' from `int' lacks a cast
main.cpp:33: initialization to `char *' from `int' lacks a cast
#include <cstdlib>
#include <iostream>
#include "interval_tree.h"
using namespace std;
//the menu options...
int show_menu()
{
cout << "\n/**** Options ****/";
cout << "\n1. Create tree";
cout << "\n2. Insert interval";
cout << "\n3. Search interval";
cout << "\n4. Print";
cout << "\n0. Exit";
cout << "\n\nSelect any option: ";
int option;
fflush(stdin);
cin >> option;
return option;
}
//parse a given string in order to build an interval
IntInterval* parse_interval(char* data)
{
char* ptr = strchr(data,',');
int pos=(ptr-data);
if(pos > 0)
{
char* first = strtok (data,",");
char* second = strtok (NULL, ",");
return new IntInterval( atoi(first), atoi(second));
}
return NULL;
}
//draw the tree in the console
void serialize(TreeNode *_tree, int _level)
{
for(int i=0; i<_level; i++)
cout << "--";
_tree->get_interval()->Print();
cout << endl;
if( _tree->get_left()->get_interval() != NULL)
serialize(_tree->get_left(),_level+1);
else
if( _tree->get_right()->get_interval() != NULL)
{
for(int i=0; i<_level+1; i++)
cout << "--";
cout << "L\n";
}
if( _tree->get_right()->get_interval() != NULL)
serialize(_tree->get_right(),_level+1);
else
if( _tree->get_left()->get_interval() != NULL)
{
for(int i=0; i<_level+1; i++)
cout << "--";
cout << "R\n";
}
}
int main(int argc, char *argv[])
{
IntervalTree* _intTree = NULL;
system("color A");
//loop
while(true)
{
int option = show_menu();
switch(option)
{
case 0: //exit
return 0;
case 1: //create tree
{
_intTree = new IntervalTree();
puts("The Tree was created succesfully!\n");
}
break;
case 2: //insert node
if(_intTree != NULL)
{
printf("Type an Interval a,b: ");
fflush(stdin);
char data[10];
gets(data);
//inser the node
_intTree->Insert( parse_interval(data));
}
else
puts("No tree created!\n");
break;
case 3: //serch interval
if(_intTree != NULL)
{
printf("Type an interval a,b to search: ");
fflush(stdin);
char data[10];
gets(data);
Stack<void *> * _intervals = _intTree->IntervalSearch( parse_interval(data));
while(!_intervals->Empty())
{
IntInterval* _int = (IntInterval*)_intervals->Pop();
_int->Print();
printf("--");
}
printf("\n");
}
else
puts("No Tree created!\n");
break;
case 4: //print the tree
if(_intTree != NULL )
if( _intTree->get_root()->get_left()->get_interval() != NULL)
serialize(_intTree->get_root()->get_left(),0);
else
puts("Empty tree!!!");
else
puts("No Tree created!\n");
break;
}
system("pause");
system("cls");
}
}
I am new to unix. i tried to compile main.cpp using g++, it gives me following errors. Somebody could help in correcting code.
g++ main.cpp
main.cpp: In function `class IntInterval * parse_interval(char *)':
main.cpp:28: implicit declaration of function `int strchr(...)'
main.cpp:28: initialization to `char *' from `int' lacks a cast
main.cpp:32: implicit declaration of function `int strtok(...)'
main.cpp:32: initialization to `char *' from `int' lacks a cast
main.cpp:33: initialization to `char *' from `int' lacks a cast