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 7783087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:44:26+00:00 2026-06-01T19:44:26+00:00

I have a program takes inputs from 2 files in c++. Then identify Is

  • 0

I have a program takes inputs from 2 files in c++. Then identify Is the second input file is topological sort? but somehow if i use the list.empty() in a while loop statement, it gives me a segmentation fault, but the for loop doesnt give me any error; however, the for loop only loop once, since i might need to go throught twice.

#include <list>
#include <iostream>
#include <vector>

using namespace std ;

list<unsigned> output;

list<unsigned> &
testSort ( istream & idata , istream & sdata )
{
 unsigned n,x1,x2;
 vector< list<unsigned> > successor(n);
 vector<unsigned> count(n,0);
 vector<bool> marks(n,false);

 idata >>n;

 for(int i=0;i<n;i++) {
idata>>x1>>x2;
count[x2]++;
successor[x1].push_back(x2);
if(idata.eof()) break;
 }

 for(int i=0;i<n;i++) {
 sdata>>x1;
  if(count[x1]==0) {
   marks[x1]=true;

   //for(int j=0;j<successor[x1].size();++j) {
    while(!successor[x1].empty()) { 
count[successor[x1].front()]--; 
successor[x1].pop_front();      
  }
 }
  else {
    for(int i=0;i<n;i++)
    {
     if(marks[i]==false)
     output.push_back(i);
    }
   break;
  }  

 }
  return output;
}
  • 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-01T19:44:27+00:00Added an answer on June 1, 2026 at 7:44 pm
    unsigned n,x1,x2;
    vector< list<unsigned> > successor(n);
    

    This is a clear error — you’re using n to specify the size of successor, but n hasn’t been initialized yet, so it contains garbage (and the same is true with count and marks since you’ve specified their size as n as well). In other words, at this point, we have no clue about the size of successor.

    You have a couple of choices. You could move your idata >> n; before you define successor, count, and marks, or you could define them without a size, and then use resize to specify their size after you read n from idata.

    I’ll leave off my usual rant about std::list beyond pointing out that I rarely find it an optimal choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a perl program that takes input and output file arguments, and I'd
I have a python program which takes input from stdin. Now, I've to write
I have a program that takes a list of names from a file and
I have to create a program that takes an input file of unsorted numbers
I am writing a program that takes in an input file from the user.
  I have a file copy program that takes from one file and pastes in
I have a program that loads a file (anywhere from 10MB to 5GB) a
Currently I have a program that takes as input a folder that contains .MPP
I have a Python program which is going to take text files as input.
I have a program that's expected to take as input a few options to

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.