I have to read text file each word at a time and then push that word to stack and then pop each word at a time to print in the display. I have tried the following code but after running the program, compiler just shows the blank screen with no error. NOTE: I am not allowed to implement the stack using a class or struct or with STL. The stack must be implemented using a fixed size array of words and an index integer for indicating the top of the stack
My text file is something like:
one two three four five
six seven and so on
output should be:
no os dna neves xis ...
main.cpp
using namespace std;
char word;
void push(char);
void pop();
void displaywords();
int count = 0;
const int arr_Size=50;
string stack[arr_Size];
int main()
{
//string word;
ifstream infile;
infile.open("data.txt");
if(!infile)
{
cerr << "An error occurred while opening the file.";
exit(1);
}
do
{
cin >> word;
if (infile.fail())
break;
cout << word;
push(word);
}while(infile.eof());
infile.close();
while(stack!=NULL) // trying to write code for stack is not null
{
displaywords();
pop();
}
return 0;
}
void push(char word)
{
count = count + 1;
stack[count] = word;
}
void displaywords()
{
cout << "PUSHED " << " " << stack[count] << " ." << endl;
}
void pop()
{
count = count - 1;
}