user3019400
user3019400

Reputation: 13

How to print the contents of a Queue

I can't figure out what I am doing wrong here. I am trying to print out my entire queue (which holds weather history) and it won't work. When I try and debug it, it shows me that it is holding the values in the queue correctly, but not assigning it correctly. How can i output the contents of my queue correctly? Here is my code:

Temp.cpp

ostream &operator<<( ostream &out, Temperature &p)//Overloaded Method to output the data
{    
fstream myEngFile5;
string tempLine5;
string tempLine6;
myEngFile5.open("English.txt");
myEngFile5.seekg(335);
getline(myEngFile5, tempLine5);
out<<tempLine5<<p.value; //The Temperature is __
myEngFile5.seekg(356);
getline(myEngFile5, tempLine6);
out<<tempLine6<<"\n"; //F
return out;
}

Temp.h

int getTemp();
int getTemp2();

Temperature()
{
value=0;
}

Temperature(int v)
{
value=v;
}
friend ostream &operator<<(ostream &out,Temperature &p);

Source.cpp

queue<int>temp;//decalred in the begining
.
.
.
for(int i=0; i<temp.size(); i++){
int qSize=temp.size();
Temperature p2(temp.front());
while( !temp.empty()){
cout<<p2;
temp.pop();}
}

Upvotes: 1

Views: 8857

Answers (1)

Dweeberly
Dweeberly

Reputation: 4777

Is this what you are looking for (untested)?:

while (!temp.empty()) {
    Temperature p2(temp.front());
    cout << p2;
    temp.pop();
    }

It seems like you might be better off using queue<Temperature> and constructing your Temperature objects when you fill your queue.

Upvotes: 1

Related Questions