Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9032969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:06:35+00:00 2026-06-16T08:06:35+00:00

I am using dque to push some elements that I read from a file

  • 0

I am using dque to push some elements that I read from a file to the back of the deque but when I print them from the front I only get the last element

  fgets(line,100,file);
  qu.push_back(line);
  fgets(line,100,file);
  qu.push_back(line);
  fgets(line,100,file);
  qu.push_back(line);
  fgets(line,100,file);
  qu.push_back(line);

  while(!qu.empty())
            {
                puts(qu.front());
                qu.pop_front();
            }

sample input

a
b
c
d

output

d
d
d
d

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:06:36+00:00Added an answer on June 16, 2026 at 8:06 am

    You haven’t provided the declarations for line and qu, but I can guess they are

    char line[100];
    std::dequeue<char*> qu;
    

    This means qu stores pointers to an external (to qu) buffer with characters in it. In your program, you have just a single buffer (line), whose address gets pushed repeatedly into qu, but whose contents get overwritten with each fgets call.

    While you are learning C++, try to stay away from pointers as much as you can. So, instead of using char* for strings, use std::string:

    std::string line;
    std::deque<std::string> qu;
    
    getline(std::cin, line);
    qu.push_back(line);
    getline(std::cin, line);
    qu.push_back(line);
    getline(std::cin, line);
    qu.push_back(line);
    getline(std::cin, line);
    qu.push_back(line);
    
    while(!qu.empty())
    {
        std::cout << qu.front() << std::endl;
        qu.pop_front();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JNotify in project and it fulfill my all requirements, but some times it
Using JAXB in Java it is easy to generate from a xml schema file
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
Using knockoutjs mapping plugin is it possible to only make the child elements observables?
Using Trigger.io's barcode api in an application that only contains boilerplate HTML and the
using phpPgAdmin, I've tried to create a table in my database. But am getting
Using this as an example, how could I scale the graphic such that it's
Using ember 1.0.0-pre3 I have a little app that has this code: window.App =
Using a form I am adding some basic fields to my table. I also
Using python and the gi.repository module, I am trying to call Gdk.threads_add_idle , but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.