3

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;
}
muzzi
  • 372
  • 2
  • 13

3 Answers3

1

That's because you are trying to read from cin. Change the cin in the do block to infile.

L. F.
  • 19,445
  • 8
  • 48
  • 82
1

Your code has quite a few problems. One obvious one is that the reading loop has while(infile.eof()) as its condition. That's almost certainly not what you want. while(!infile.eof()) is probably what you were thinking of, but that doesn't really work correctly/dependably either.

You're also opening infile but when you read, you try to read from cin instead of infile.

You also attempt to use while(stack!=NULL), with the apparent intent of reading until the stack is empty--but stack is an array. It'll never compare as equal to NULL.

Since you're using C++, I'd use a standard container (e.g., std::vector or std::deque, with or without the std::stack adapter) instead. Something on this general order should be a bit closer:

std::vector<std::string> strings;
std::infile("some file.txt");
std::string word;

while (infile >> word)
    strings.push_back(word);

while (!strings.empty()) {
    std::cout << strings.back();
    strings.pop_back();
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 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 – muzzi Jul 31 '18 at 01:28
  • 1
    @muzzi: Well, then go ahead and do that--though if it were me, I'd still create a `stack` class of my own, so the rest of the code would just deal with a stack at an abstract level (push, pop. top). Oh, and based on the edit to the question, you really want to read one character at a time, and put single characters on the stack, not whole words. – Jerry Coffin Jul 31 '18 at 01:30
1

std::cin reads from stdin. You're not going to get anything out of your streambuffer - it's waiting for user input.

jdunlop
  • 181
  • 1
  • 9