DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   No output (http://www.daniweb.com/forums/thread101662.html)

seyetotto Dec 18th, 2007 4:09 pm
Re: searching and inserting node in a binary search tree
 
Please I'm new to this forum and I need a help with my code.I have written it all and compiled but it doesnt seem to bring any output(I'm reading the input from a file on my system).Please bail me out, I need to turn it in soonest
#include<iostream>
#include<fstream>
#include <conio.h>

using namespace std;

struct node
  { 
    string firstName;    // firstName  letters
   
    string lastName;    //lastName letters
   
    string  telnumber;          // Telephone number
   
    string housenumber;      // fax number
   
    string city;            // city
   
    string state;            //state
   
    string zipcode;          //zipcode
   
    node *link;        // Pointer to next node
  };

node *head = NULL;


//Function to add a node to the list
//Sets the list head to newNode if list is empty
//Add the newNOde to the next position in the list if list is not empty
void add_node_at_end (node & newNode)
  { 
  node *temp, *temp2;  // Temporary pointers

    // fill the newNOde with the incoming data
    temp = new node;
   
    temp->firstName = newNode.firstName;
   
    temp->lastName = newNode.lastName;
     
    temp->telnumber = newNode.telnumber;
   
    temp->housenumber = newNode.housenumber;
   
    temp->city = newNode.city;
   
    temp->state = newNode.state;
   
    temp->zipcode = newNode.zipcode;
   
    temp->link = NULL;

    // Add this node to the list
    if (head == NULL)
        head = temp;
    else
      {
            temp2 = head;
            // if the list not empty!
    while (temp2->link != NULL)
          {  temp2 = temp2->link;
          // Move to next link in chain
          }
        temp2->link = temp;
   
      }
}

////////////////////////////////////////////
//Function ito displays the output file
////////////////////////////////////////////

void DisplayList(ofstream & outfile)
{
  node * temp = head;
 
do
  {  if (temp == NULL)
      cout << "End of list" << endl;
    else
      {  // Display details for what temp points to
          outfile << "firstName : " << temp->firstName << endl;
       
          outfile << "lastName : " << temp->lastName << endl;
         
          outfile << "telnumber : " << temp->telnumber << endl;
         
          outfile << "housenumber : " << temp->housenumber << endl;
         
          outfile << "city : " << temp->city << endl;
         
          outfile << "state : " << temp->state << endl;
         
          outfile << "zipcode : " << temp->zipcode << endl;
         
          outfile << endl;      // Blank line

          // Move to next node (if present)
          temp = temp->link;
      }
  }
while (temp != NULL);
 
  }


//Deleting node at head
  void delete_start_node()
  {
    node *temp;
    temp = head;
    head = head->link;
    delete temp;
  }
   
    //deleting at end
 
  void delete_end_node()
  {
    node *temp1, *temp2;  // Temporary pointers
   
    //Deleting a node at the end of the list
  if (head == NULL)
    cout << "The list is empty!" << endl;
  else
    {
      temp1 = head;
  if (temp1->link == NULL)   
      {
        delete temp1;
        head = NULL;
      }
  else
      {
        while (temp1->link != NULL)
      {
          temp2 = temp1;
          temp1 = temp1->link;
      }
        delete temp1;
        temp2->link = NULL;
      }
    }
 }
 
void add_node_at_start (node & newNode)
{
  node * temp = 0;  // Temporary pointers

    // Reserve space for new node and fill it with data
    temp = new node;
   
      temp->firstName = newNode.firstName;
   
      temp->lastName = newNode.lastName;
     
      temp->telnumber = newNode.telnumber;
   
      temp->housenumber = newNode.housenumber;
   
      temp->city = newNode.city;
   
      temp->state = newNode.state;
   
      temp->zipcode = newNode.zipcode;
   
   
      temp->link = head;
   
      head = temp;



void add_in_middle(node & newNode)
    {
          node * newNodePtr = 0;  // Temporary pointers

    // Reserve space for new node and fill it with data
    newNodePtr = new node;
   
     
  newNodePtr->firstName = newNode.firstName;
   
      newNodePtr->lastName = newNode.lastName;
     
      newNodePtr->telnumber = newNode.telnumber;
   
      newNodePtr->housenumber = newNode.housenumber;
   
      newNodePtr->city = newNode.city;
   
      newNodePtr->state = newNode.state;
   
      newNodePtr->zipcode = newNode.zipcode;
   
    newNodePtr->link= head->link;
   
    head->link =newNodePtr;
   
    }
         
void delete_in_middle ()
{
    node * temp;  // Temporary pointer
         
    // check to see if the list is empty
         
    if (head!= NULL)
    {                   
        // check to see if there is more than 1 node in the list
                   
        if (head->link != NULL)
        {                       
          temp= head->link->link ;
         
          delete  head->link;
         
          head->link = temp;
        }
    }
}
         
int main(void)
{   
  node inputNode;
     
  ifstream infile;  // input file
   
  infile.open("c:\\pbinput.txt");
 
  if (!infile)
  {
      cerr << "Unable to open file inputfile.txt";
 
      exit(1);  // call system to stop
  }
 
  ofstream outfile;
     
  outfile.open("outputfile.txt");
 
  if (!outfile)
  {
      cerr << "Unable to open file outputForPow.txt";
 
      exit(1);  // call system to stop
  }
 
  outfile << "This file contains the input and output for the Power function " << endl << endl;
 
 
  int i = 0;
 
    while (!infile.eof())
    {
        infile >> inputNode.lastName >> inputNode.firstName >> inputNode.telnumber >> inputNode.housenumber >> inputNode.city >> inputNode.state >> inputNode.zipcode ;
       
        outfile << "Last name: " << inputNode.lastName << " first name: " <<  inputNode.firstName << " tel number: " <<  inputNode.telnumber << " house number: " <<  inputNode.housenumber << " city: " << inputNode.city << " state : " <<  inputNode.state << " zip code: " <<  inputNode.zipcode  << endl;
 
        if ((i % 3) == 0)
        {           
            outfile << "Adding to end of list " << endl;
                       
            add_node_at_end (inputNode);
        }
        else if ((i %3) == 3)
        {
            outfile << "Adding to beginning of list " << endl;
             
            add_node_at_start (inputNode);
        }   
        else
        {
            outfile << "Adding to the middle of list " << endl;
           
            add_in_middle(inputNode);
        }
       
        i++;
       
       
    }
   
    outfile << "displaying list after reading input: " << endl;
   
    DisplayList(outfile);
    getch();
    outfile << "displaying list after deleting first node: " << endl;
   
    delete_start_node();
   
    DisplayList(outfile);
    getch();
    outfile << "displaying list after deleting last node: " << endl;
   
    delete_end_node();
   
    DisplayList(outfile);
    getch();
    outfile << "displaying list after deleting a node in the middle of the list: " << endl;
   
    delete_in_middle();
   
    DisplayList(outfile);   
    getch(); 
     
    infile.close();
   
    outfile.close();
   
    return(1);
}

seyetotto Dec 18th, 2007 4:15 pm
Re: searching and inserting node in a binary search tree
 
I'm sorry, I forgot to include that it is a phonebook and implemented in a singly linked list

dubeyprateek Dec 18th, 2007 5:07 pm
Re: No output
 
What does your input file contain? The program looks OK to me. However, it can not print anything if it does not have input file. lolz!

vmanes Dec 18th, 2007 5:46 pm
Re: No output
 
First, how does it compile without the <string> library included?

Once that's fixed, it does output, to the file, as you designed the program to do.

Your input, however, will not be correct, using the infile >> inputNode.lastName >> inputNode.firstName >> .... method. This gets one "word" at a time for each input, so street address and possibly city names will be broken up. If the input file is formatted one field per line (first name, last name, street address, etc), then you can use getline( ).

Once data is read in, you call many list functions. The program is mysteriously waiting for you to press a key to continue, with no indication that you must. It does seem to do what it's supposed to do.

Also, the line else if ((i %3) == 3) will never be true. What is the range of possible responses from the mod operator?

(added) You may be experiencing a hanging program with the input loop control you use:
while (!infile.eof())
{
        infile >> inputNode.lastName >> ....
eof will not be detected until you try to read after the last successful input, so input will hang, thus not completing the loop. Put the whole input line as the while( ) condition, and you should see something better occuring.
while (infile >> inputNode.lastName >> .........)
{
    //processing begins


All times are GMT -4. The time now is 12:59 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC