Solved

Five times a Fstream

Posted on 1997-12-08
7
314 Views
Last Modified: 2013-12-14
I am trying to pull in five sets of values via fstream, can you give me any ideas what is wrong?

the input file looks like:
   555 1
   222 2

   etc ...

   From_Truck is an object of the fstream class.

      while(!From_Truck.eof())
      {
         for (int k=0; k < 5 && !Stacker.StackEmpty(); k++)
         {
            From_Truck >> z >> ID;
            Stacker.StackAdd(z);
            Get_Input_From_Stack(z, ID);
         }
      }
0
Comment
Question by:hawhite
  • 4
  • 3
7 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 1175330
What is happening - how do you know something _IS_ wrong?  Does the code compile?

Are you sure there is a trailing newline at the end of your data file?

Are you sure the problem is not in StackAdd() or Get_Input_From_Stack() [which you've not told us anything about]

Could it be the Stacker.StackEmpty() is true before you start, so the for loop never executes ???  Assuming these routines do what they sound like (from their names) this seems likely to be the problem


0
 

Author Comment

by:hawhite
ID: 1175331
It compiles, and works to a point.  StackEmpty is a function that just checks to see if the linked list is empty.
//stack empty
  if( first == 0)
     return 0
   else
      return 1;

Get Input From Stack just deletes from a queue if the queue is full.  

//Get Input from stack
      int NewVal = z;

      Stacker.StackDelete(NewVal);

      if(Bins[ID].QueueEmpty())
         Bins[ID].QueueAdd(NewVal);
      else
      {
         Bins[ID].QueueDelete(z);
         Bins[ID].QueueAdd(NewVal);
      }

The loop pulls in the right information, but in the last array of queues it adds and extra number

Bin1
====
999
999
999
999
111
222
333
444
555

Bin2
====
111
222
333
444
555

Bin3
====
111
222
333
444
555

Bin4
====
111
222
333
444
555

Bin5
====
111
222
333
444
555
555

It also add to much to the first bin.


HELP!!!
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1175332
If this is your StackEmpty function, then it is wrong.  It is returning false (0) when empty and true (1) when not empty.

> //stack empty
> if( first == 0)
>  return 0;
> else
>  return 1;

It is still very difficult to work out what on earth your program is trying to do .. it LOOKS like you are reading from a file, pushing a value ont a stack, then popping it straight off again and adding it to a queue ?????

Without more code and a description of what it is SUPPOSED to do, I cannot tell WHAT is going wrong.

Can you e-mail me some code to look at ???

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:hawhite
ID: 1175333
It is moving things from a stack to a queue.  I will gladly send you a copy of my code.  Just tell me how to get you e-mail address, or I could just paste it on here.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1175334
I'm not supposed to tell you, but...

mailto:Roger_Onslow@compsys.com.au

If the code is short enough, post it here too so other experts can get a look in.

0
 

Author Comment

by:hawhite
ID: 1175335
//linkstack.h
/*====================================
   
   Library Information
   ===================
   
   This is the library for a linkedlist
   stack. It contains the functions
   necesary for the proper use of a
   singly linkedlist with one external
   pointer.
   
====================================*/

#ifndef LINKSTACK_H
#define LINKSTACK_H
#include <iostream.h>
#include <assert.h>
#include "data.h"

class LinkedStack
{
   public:
      LinkedStack(void);
      ~LinkedStack(void);
      Node * Delete(int);
      void Print();
      Node * Find_ith(int i);
      Node * StackDelete(int& retvale);      
      int StackEmpty();
      void StackAdd(const int& y);
      Node * Search(const int& Account);
      int Find_length(int& Counter);
     
   private:
      Node * top;
      int Counter;

};
#endif


//linkstack.cxx
/*====================================
   Library Information
   ===================
   
   This is the library for a linkedlist
   stack. It contains the functions
   necesary for the proper use of a
   singly linkedlist with one external
   pointer.
   
====================================*/

#include "linkstack.h"

LinkedStack::LinkedStack(void)
{
   top = 0;
}

LinkedStack::~LinkedStack(void)
{
   Node
      *TempPtr = top;

   while (TempPtr != 0)
   {
      top = top -> next;
      delete TempPtr;
      TempPtr = top;
   }
}

int LinkedStack::StackEmpty()
{
   if(top == 0)
      return 0;
   else
      return 1;
}
Node * LinkedStack::Delete(int Account)
{
   Node * q = top;
     
   Node * p;
   
   if(top == 0)
      return p;

   while(q && (q ->next) -> data != Account)
      q = q -> next;
     
   p = q -> next;

   q -> next = q -> next;

   delete p;
}

Node *LinkedStack::Find_ith(int i)
{
   int count = 0;
   
   Node *p = top;
   
   while(p && (count < i))
   {
      count ++;
     
      p = p -> next;
   }
   
   return p;  
}
Node *LinkedStack::Search(const int& item)
{
   Node * q = top;

   while(q != 0 && (q -> data != item))
      q = q -> next;
     
   return q;
}

void LinkedStack::StackAdd(const int& y)
{
   top = new Node(y, top);
}

Node * LinkedStack::StackDelete(int& retvalue)
{
   if(top == 0)
   {
      return 0;
   }

   Node * delnode = top;

   retvalue = top -> data;

   top = top -> next;

   return delnode;

   //return& retvalue;
}
int LinkedStack::Find_length(int& Counter)
{
   Counter = 0;
   
   Node * p;
   
   p = top;
   
   while(p != 0)
   {
      Counter ++;
     
      p = p -> next;
   }
   return Counter;
}

void LinkedStack::Print()
{
   Node *p = top;

   while(p)
   {
      cout << p -> data << endl;
      p = p -> next;
   }
}


//data.h
#ifndef DATA_H
#define DATA_H

class LinkedList;

class Node
{
   friend class LinkedList;
   
   public:
      Node(int element = 0, Node * p = 0);

      int element;
     
      int data;
         
      Node * next;
};
#endif


//data.cxx
/*====================================
   Library Information
   ===================
   
   This is the library for a node in a
   linkedlist.  It contains a balance
   and a pointer field.
   
====================================*/
#include "data.h"

Node::Node(int element, Node * p)
{
   data = element;

   next = p;
}

// linkqueue.h
//same as linkstack except for the public definitions of:
      int * QueueDelete(int& retvale);
      int QueueEmpty();
      void QueueAdd(const int& y);

//it has two pointers front and rear

//linkqueue.cxx
int LinkedQueue::QueueEmpty()
{
   if(front == 0)
      return 0;
   else
      return 1;
}

void LinkedQueue::QueueAdd(const int& y)
{
   if(front == 0)
      front = rear = new Node(y,0);
   else
      rear = rear -> next = new Node(y,0);  
}

int * LinkedQueue::QueueDelete(int& retvalue)
{
   if(front == 0)
   {
      return 0;
   }

   Node * x = front;

   retvalue = front -> data;

   front = x -> next;

   delete x;

   return& retvalue;
}

//intermediary.h
/*************************************************
   Description
   ===========

   This is the prototype of a Class that will
   construct a bridge to the two classes for the
   stack and the queue.

*************************************************/
#ifndef INTERMEDIARY_H
#define INTERMEDIARY_H
#include <iostream.h>
#include "linkstack.h"
#include "linkqueue.h"

class Intermediary
{
   public:
      Intermediary();
      void Get_Input_To_Stack();
      void Get_Input_From_Stack(int& z, int Value);
      void Print();
      void Check(int& z, int ID);

   private:
      LinkedQueue Bins[5];
      LinkedStack Stacker;

      int NUM,
          Value,
          i,
          z;
   
      char File[20];
};
#endif

//intermediary.cxx

/*************************************************
   Description
   ===========

   This is the implementation of a Class that will
   construct a bridge to the two classes for the
   stack and the queue.

*************************************************/
#include <iostream.h>
#include <fstream.h>
#include "intermediary.h"


   Intermediary::Intermediary()
   {
   }
   
   void Intermediary::Get_Input_To_Stack()
   {
      cout <<"\nPlease enter the truck (filename) you wish to unload (use): ";
   
      cin >> File;

      fstream
         From_Truck;

      From_Truck.open(File, ios::in);

      int ID;
     
      while(!From_Truck.eof())
      {      
         for (int k=0; k < 5 && !Stacker.StackEmpty(); k++)
         {
            From_Truck >> z >> ID;
            Stacker.StackAdd(z);
            Get_Input_From_Stack(z, ID);
         }
      }

   From_Truck.close();
   }  

   void Intermediary::Get_Input_From_Stack(int& z, int ID)
   {
      int NewVal = z;

      Stacker.StackDelete(NewVal);

      if(Bins[ID].QueueEmpty())
         Bins[ID].QueueAdd(NewVal);
      else
      {
         Bins[ID].QueueDelete(z);
         Bins[ID].QueueAdd(NewVal);
      }
   }

   void Intermediary::Print()
   {
      int q;

      for(q = 0; q < 5; q ++)
      {
         if(q ==0) cout <<"\nBin1" <<"\n====" << endl;
         else if(q ==1) cout <<"\nBin2" <<"\n====" << endl;
         else if(q ==2) cout <<"\nBin3" <<"\n====" << endl;
         else if(q ==3) cout <<"\nBin4" <<"\n====" << endl;
         else cout <<"\nBin5" <<"\n====" << endl;
         Bins[q].Print();
      }
   }


// driver.cxx

/*====================================
   Driver
   ======
   This is a driver for a program that
   will move objects from a truck (file)
   and insert these items into a stack
   (stacker) when the stack.  The stack
   will then be emptied in to a series
   of queues (bins);

====================================*/

#include <iostream.h>
#include "intermediary.h"

int main(void)
{
   Intermediary
      I;

   I.Get_Input_To_Stack();

   I.Print();

   return 0;
}
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1175336
I'll check it out ASAP.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question