1

I am using priority queue from the C++ STL library and I want to keep pushing elements into the queue till the user presses Enter.

I've tried using getchar() to get the input of the elements but it gets saved as a series of 1's in the queue.

priority_queue<int> v1;
priority_queue<int, vector<int>, greater<int> > v2;
cout<<"Enter the elements of the queue. Press enter to stop.\n";
while(a = getchar()!= '\n')
{
    v1.push(a);
    v2.push(a);
}
while(!v1.empty())
{
    cout<<v1.top()<<" ";
    v1.pop();
}

I expected the output to be the min and max heap of the elements entered, but all it gives me is a series of 1's

Raisa A
  • 437
  • 7
  • 21

2 Answers2

3
while(a = getchar()!= '\n')

it gives me is a series of 1's

warning with the priority of the operators, you want

while((a = getchar())!= '\n')

currently you do like

while(a = (getchar()!= '\n'))

while the input char is not newline the test is true so a is set to 1


Considering your remark below you want to read lines then stop if that line is empty or just contains the newline or may be also if the line does not contains a literal int, else to extract the int to push it

For instance :

#include <iostream>
#include <queue>
#include <string>
#include <sstream>

using namespace std;

int main()
{
  priority_queue<int> v1;
  priority_queue<int, vector<int>, greater<int> > v2;
  cout<<"Enter the elements of the queue. Press enter to stop.\n";

  int a;
  string s;

  while (getline(cin, s) && (istringstream(s) >> a))
  {
    v1.push(a);
    v2.push(a);
  }
  while(!v1.empty())
  {
    cout<<v1.top()<<" ";
    v1.pop();
  }
  cout << endl;
}

Compilation and execution :

pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra c.cc
pi@raspberrypi:/tmp $ ./a.out
Enter the elements of the queue. Press enter to stop.
12
32

32 12 
pi@raspberrypi:/tmp $ 
bruno
  • 32,421
  • 7
  • 25
  • 37
  • I made the change in my code. But it gives me a series of random numbers now. – Raisa A Jun 27 '19 at 13:37
  • @RaisaArief random ? you mean you get the (probable) ASCII char codes of the char you enter – bruno Jun 27 '19 at 13:38
  • I wrote 12 as input and got 50 49 as output – Raisa A Jun 27 '19 at 13:41
  • @RaisaArief because 49 is the code of '1' and 50 the code of '2'. WHat did you expect to have ? the value 12 ? – bruno Jun 27 '19 at 13:42
  • So how do I display the value 12? I'm rather new to this so I may have trouble grasping the concepts easily. – Raisa A Jun 27 '19 at 13:53
  • 1
    Stop when it's EOF. Or stop using getchar if you don't want to get, well, chars. – Lightness Races in Orbit Jun 27 '19 at 13:57
  • @RaisaArief I edited my answer to add a proposal, look at it – bruno Jun 27 '19 at 13:58
  • I copied the entire code as is, but my output displays only the first element in the order that I input it. – Raisa A Jun 27 '19 at 14:20
  • @RaisaArief In my proposal i suppose only one number is enter per line. What do you want ? To enter all the numbers on the same line ? In that case move the _getline_ and the _istreamstring_ out of the _while_ and loop on the `>> a` (i am at the dentist now, not easy on phone ^^) – bruno Jun 27 '19 at 14:27
0

Yes, because a = getchar()!= '\n' is evaluated as a = (getchar() != '\n') gives you a bool value, true in this case, until you press enter.

A.Franzen
  • 725
  • 4
  • 10