Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with my C++ code I wrote a proble for the following question Using y

ID: 675490 • Letter: I

Question

I need help with my C++ code I wrote a proble for the following question

Using your priority queue class, implement a program that transforms its input as follows:

Read a sequence of characters one-by-one.

If the character is a letter, insert it into the queue.

If the character is an exclamation point, remove the minimum (alphabetically first) character from the queue and print it to the output.

For example, given the following input:

The program will produce this output

I'm trying to have the data from a text file instead of inputing the data. the text name is prog1_2.txt . I tried to code it but I keept getting it wrong. Any help with this would be great.

My code is below.

#include <functional>
#include <queue>
#include <vector>
#include <iostream>
#include <string>

using namespace std;


int main() {
   string input;
   cout<< "blah"<<endl;
   cin >> input;
  
   priority_queue<int, vector<int>, greater<int> > q;
   for (char n : input)
   {
       if (n == '!')
       {
          int k = q.top();
          q.pop();
          cout<< (char)k;
       }
       else
       {
           q.push((int)n);
       }
      
   }
   cout<<endl;
  
}

Explanation / Answer

#include <functional>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;


int main() {
  
   priority_queue<int, vector<int>, greater<int> > q;
   string filename;
   cout << "Please enter the data file path :";
   cin >> filename;
   ifstream file(filename.c_str());
   char ch;
    while (file >> ch)
   {
        if (ch == '!')
        {
             int k = q.top();
             q.pop();
             cout<< (char)k;
        }
        else
        {
            q.push((int)ch);
        }
    }
   cout<<endl;
    file.close();
   return 0;
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote