Infinite Loop while Reading from a File into Stack

Created a stack. While reading input from File I hit an infinite loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//StackDr.cpp
/*

Description: Test driver file
*/

#include <iostream>
#include <fstream>
#include "StackType.h"
#include<string>
#include<iomanip>
using namespace std;

int main()
{

  ifstream inFile;       // file containing operations
  ofstream outFile;      // file containing output
  string inFileName;     // input file external name
  string outFileName;    // output file external name
  string outputLabel;
  string command;        // operation to be executed

  ItemType item;
  StackType stack;
  int numCommands;


  // Prompt for file names, read file names, and prepare files
  cout <<setw(6) << "Programmed by Frantzdy Romain" << endl;
  cout << "Enter name of input command file; press return." << endl;
  cin  >> inFileName;
  inFile.open(inFileName.c_str());

  cout << "Enter name of output file; press return." << endl;
  cin  >> outFileName;
  outFile.open(outFileName.c_str());

  cout << "Enter name of test run; press return." << endl;
  cin  >> outputLabel;
  outFile << outputLabel << endl;
  numCommands = 0;
  inFile >> command;
  while (command != "Quit")
  {
    try
    {
      if (command == "Push")
      {
        inFile >> item;
        stack.Push(item);
      }
	  else if (command == "Pop"){
			inFile >> item;
			stack.Pop(item);
	  }
      else if (command == "Top")
      {
        item = stack.Top();
        outFile<< "Top element is " << item << endl;
      }
      else if (command == "IsEmpty")
        if (stack.isEmpty())
          outFile << "Stack is empty." << endl;
        else
          outFile << "Stack is not empty." << endl;

      else if (command == "IsFull")
        if (stack.isFull())
          outFile << "Stack is full." << endl;
      else outFile << "Stack is not full."  << endl;
    }
    catch (FullStack)
    {
      outFile << "FullStack exception thrown." << endl;
    }

    catch (EmptyStack)
    {
      outFile << "EmtpyStack exception thrown." << endl;
    }
    numCommands++;
    cout <<  " Command number " << numCommands << " completed."
         << endl;
    inFile >> command;
	//numCommands++;
  }

  cout << "Testing completed."  << endl;
  inFile.close();
  outFile.close();
  system("pause");
  return 0;
}




//header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//StackType.h
/*

Program description: test File.
*/
#pragma once
#include "ItemType.h"
class FullStack
{};
class EmptyStack
{};

class StackType
{
public:
	StackType(void);
	bool isEmpty() const; //function to check whether empty or not
	bool isFull() const; //check if full
	void Push(ItemType item);
	void Pop(ItemType& topItem);
	ItemType Top() const;

private:
	int top;
	ItemType items[MAX_ITEMS];
	
};


//implemention file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//StackType.cpp
/*

*/
#include "StackType.h"

StackType::StackType(void)
{
	top = 1; //initialize top location
}

bool StackType::isEmpty() const{
	return (top == -1); //empty when -1

}

bool StackType::isFull() const
{
	return (top == MAX_ITEMS -1); //is full when 5-4 which is the last(top) location  


}
void StackType::Push(ItemType newItem)
{
	//if stack is full throw an exception else increment top and set item at top location to newItem
	if(isFull())
		throw FullStack();
	top++; //increment top
	items[top] = newItem; //newitem will take posiion of items at top location


}

void StackType::Pop(ItemType& topItem)
{
	topItem = items[top];
	if(isEmpty())
	{
		throw EmptyStack();
		top--;
	}

	
}

ItemType StackType::Top() const
{
	if (isEmpty())
	{
	throw EmptyStack();
	return items[top]; //return the top of the stack
	
	}

}


//Include file for const MAX_ITEMS and ItemType
1
2
3
4
5
#include <iostream>
using namespace std;

const int MAX_ITEMS = 5;
typedef int ItemType;
Topic archived. No new replies allowed.