Incorrect Output when Printing a Stack

This is a discussion on Incorrect Output when Printing a Stack within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm getting an error when I try to print out a stack of objects. I want to have a ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Incorrect Output when Printing a Stack

    Hello,

    I'm getting an error when I try to print out a stack of objects. I want to have a stack that simulates the "Running Queue" in an operating system. I want to have one Process (represented by a number) and the IDLE process. Here is my code when I populate the stack and the two queues:

    Code:
      ifstream infile("in_file.txt");
      ifstream event("events.txt");
      
      list<string> vec;
      list<string> tempPro;
      
      stack<string> run;
      queue<string> ready;
      queue<string> wait;
      
      if (!infile || !event) {
        cerr << "error: unable to open input file!\n";
        return -1;
      }
    
    string word;
      string idle = "IDLE";
      
      run.push(idle);
      
      while (infile >> word) {
          vec.push_back(word);
          string tempEl;
          if (word == "running") {
                tempEl = tempPro.front();
                tempPro.pop_front();
                run.push(tempEl);
          }
          else if (word == "ready") {
                tempEl = tempPro.front();
                tempPro.pop_front();
                ready.push(tempEl);
          }
          else if (word == "waiting") {
                tempEl = tempPro.front();
                tempPro.pop_front();
                wait.push(tempEl);
          }
          else tempPro.push_front(word);
          
          // print the contents
          if (word == "running" || word == "ready" || word == "waiting") {  
              int runSize = run.size();
              int readySize = ready.size();
              int waitSize = wait.size();
          
              cout << "Running: ";
              while (runSize != 0) {
                  string running = run.top();
                  cout << running << " ";
                  run.pop();
                  run.push(running);
                  runSize--;
              }
              cout << endl;
              cout << "Ready: ";
              while (readySize != 0) {
                  string readyToGo = ready.front();
                  cout << readyToGo << " ";
                  ready.pop();
                  ready.push(readyToGo);
                  readySize--;
              }
              cout << endl;
              cout << "Waiting: ";
              while (waitSize != 0) {
                  string waiting = wait.front();
                  cout << waiting << " ";
                  wait.pop();
                  wait.push(waiting);
                  waitSize--;
              }
              cout << endl;
              cout << endl;
          }
      }

    The parts in bold are the parts that concern the stack. When I print the results, I get two things in the run stack, the process number twice. For some reason, it won't print the string "IDLE". However, when I manipulate the contents later in my code so that there is only one string in the stack, the word IDLE is displayed. Can someone tell me why two numbers print instead of a number and the word IDLE when I try to print the stack with two strings in it? Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    stack<string> run;
    ...
    int runSize = run.size();
    ...
    while (runSize != 0) {
        string running = run.top();
        cout << running << " ";
        run.pop();
        run.push(running);
        runSize--;
    }
    Since run is a stack (Last-In-First-Out), the above code will only end up printing the top element runSize times. The number gets popped but then gets put back on top in the next push statement. If you store a number (on the top) and then the word "IDLE" in the stack, then since the number is on the top, it gets printed twice (two elements in the stack). You may want a different container for this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ah, of course! I should have noticed that. Thanks, everything's working great now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String incorrect for output command target
    By DarkAlex in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2008, 09:32 PM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. printing the output its urgent
    By yviswanadham in forum C Programming
    Replies: 6
    Last Post: 12-26-2004, 09:27 AM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. printing output from prog
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 08:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21