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

  • Home
  • SEARCH
  • 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 7837713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:48:45+00:00 2026-06-02T14:48:45+00:00

Im trying to write a program that takes a txt file as input, and

  • 0

Im trying to write a program that takes a txt file as input, and uses ques from the words in the text file to generate a .h file (and later hopefully) a .cpp file. This code compiles fine, its just that it segfault 11’s once it gets the input file. Can somebody help me out?

#include <iostream>
#include <fstream>
#include "12337756_Library.cpp"
#include <string>
#include <vector>

using namespace std;

int main()
{
    bool a;
    string filename;
    string line;
    vector<string> Attr;

    cout << "Enter input file name:";
    getline(cin, filename);

    ifstream fin(filename.c_str());

    if (fin)
    {
        while(!fin.eof())
        {
            getline(fin, line);
            createNewFile(line);
            Attr.push_back(line); 
        }
        if(Attr[1]=="Movie.h")
        {
            bool x,y;
            //x=createNewFile(Attr[0]);
            y=createNewFile(Attr[1]);
            if(x)
            {
                ofstream fout(Attr[1].c_str());
                fout << "#ifndef HEADER_H_" << endl << "#define HEADER_H_" << endl;
                fout << "#include <iostream>" << endl << "#include <vector>" << endl;
                fout << "using namespace std;" << endl;
                fout << "enum Movie_Rating {G,PG,PG13,R,NC17,NR} ;" << endl;
                fout << endl << endl << endl;
                fout << "class Movie" << endl << "{"<< endl;
                fout << "public: " << endl;
                fout << "// ------------------------------------------------------" << endl;
                fout << "// ----- Constructors -----------------------------------" << endl;
                fout << "// ------------------------------------------------------" << endl;
                fout << endl << "Movie();" << endl << "Movie(const string& title);" << endl;
                fout << "Movie(const string& title, const string& director, Movie_Rating rating,unsigned int year,const string& path,const string& actor); " << endl;
                fout << endl;
                fout << "// ------------------------------------------------------" << endl;
                fout << "// ----- Destructor -------------------------------------" << endl;
                fout << "// ------------------------------------------------------" << endl;
                fout << endl;
                fout << "~Movie();" << endl << endl;
                fout << "// ------------------------------------------------------" << endl;
                fout << "// ----- Inspectors -------------------------------------" << endl;
                fout << "// ------------------------------------------------------" << endl;
                fout << endl;
                fout << "string getTitle() const;" << endl;
                fout << "string getDirector() const;" << endl;
                fout << "Movie_Rating getRating() const ;" << endl;
                fout << "unsigned int getYear() const ;" << endl;
                fout << "string getURL() const ;" << endl;
                fout << "string getActor(unsigned int i) const ;" << endl;
                fout << "int getActorNumber() const ;" << endl;
                fout << endl;
                fout << "// ------------------------------------------------------" << endl;
                fout << "// ----- Mutators ---------------------------------------" << endl;
                fout << "// ------------------------------------------------------" << endl;
                fout << endl;
                fout << "void setTitle(const string& title);" << endl;
                fout << "void setDirector(const string& director) ;" << endl;
                fout << "void setRating(Movie_Rating rating)  ;" << endl;
                fout << "void setYear(unsigned int year)  ;" << endl;
                fout << "void setURL(const string& path)  ;" << endl;
                fout << "void setActor(const string& actor);" << endl;
                fout << endl;
                fout << "//-----------------------------------------------------------" << endl;
                fout << "//------- Facilitators --------------------------------------" << endl;
                fout << "//-----------------------------------------------------------" << endl;
                fout << "void output(ostream & out);" << endl;
                fout <<"// ----------------------------------------------------------" << endl;
                fout <<"// ----------------------------------------------------------" << endl;
                int size = Attr.size();

                while(size!= 1)
                {
                    fout << Attr[size] << endl;
                    size--;
                }
                fout << "};" << endl;
            }
        }
    }
}
  • 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-02T14:48:47+00:00Added an answer on June 2, 2026 at 2:48 pm

    You only pushed back one std::string so your vector only contains 1 element. To access that one element use Attr[0] not Attr[1] (there are several places you do that in the code). Remember in C++ indexes start at 0 and not 1.

    Also, in the following code size() returns 1, so the while loop is never entered.

    int size = Attr.size();
    
    while(size!= 1)
    {
        fout << Attr[size] << endl;
        size--;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that reads text from external file (string string
I'm trying to write a program that takes a large file (of any type)
I am trying to write a C++ program that takes the following inputs from
I am trying to write a program that takes an input of of n
I'm trying to write a program that takes input of - hexadecimals, octals, and
I'm trying to write a program that takes in a plaintext file as it's
I'm trying to write a program that uses sockets to connect with other instances
I'm trying to complete an exercise to write a program that takes the following
I'm trying to write a program that cleans data, using Matlab. This program takes
I'm trying to write a program that takes a Sudoku puzzle and solves it.

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.