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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:45:34+00:00 2026-05-31T00:45:34+00:00

I am trying to write my own getline function and it keeps on segfaulting.

  • 0

I am trying to write my own getline function and it keeps on segfaulting. How do I fix it and how does get line work officially if mine is not functional? I am writing this to learn how to code better.

#include"MyString.h"





MyString::MyString( )  //constructor
{
    size=0;
    capacity=1;
    data=new char[capacity];

}
MyString::MyString(char * n)  //copy constructor
{
    size=strlen(n);
    capacity=strlen(n)+1;
    data=new char[capacity];
    strcpy(data,n);
}
MyString::MyString(const MyString &right)  //
{
    size=strlen(right.data);
    capacity=strlen(right.data)+1;
    data=new char [capacity];
    strcpy(data,right.data);

}
MyString::~MyString( )
{
    delete [] data;
}
MyString  MyString::operator = (const MyString& s)
{

    if(this!=&s)
    {
        MyString temp=data;
        delete [] data;
        size=strlen(s.data);
        capacity=size+1;
        data= new char [capacity];
        strcpy(data,s.data);
    }
}
MyString&  MyString::append(const MyString& s)
{
    if(this!=&s)
    {
        strcat(data,s.data);
    }


}
MyString&  MyString::erase()
{

}
MyString  MyString::operator + (const MyString& s)const
{
    return strcat(data,s.data);
}
bool  MyString::operator == (const MyString& s)
{
    return strcmp(data,s.data)==0;
}
bool  MyString::operator <  (const MyString& s)
{
    return strcmp(data,s.data)<0;
}
bool  MyString::operator >  (const MyString& s)
{
    return strcmp(data,s.data)>0;
}
bool  MyString::operator <= (const MyString& s)
{
    return strcmp(data,s.data)<=0;
}
bool  MyString::operator >= (const MyString& s)
{
    return strcmp(data,s.data)>=0;
}
bool  MyString::operator != (const MyString& s)
{
    return strcmp(data,s.data)!=0;
}
void  MyString::operator += (const MyString& s)
{
    append(s.data);
}
char&  MyString::operator [ ] (int n)
{
    return data[n];
}
void  MyString::getline(istream& in)
{
    char c;
    erase();
    ifstream input;
    while(in.get(c)&&c!='\n')
    {
        data[size]=c;
        size++;

        if(size+1<=capacity)
        {
          capacity*=2;
          char*p=new char[capacity];
          strcpy(p,data);
          delete [] data;
          data=p;
        }
        data[size]=c;
        size++;
        data[size]='\0';
    }

}
int  MyString::length( ) const
{
    return strlen(data);
}
void MyString::grow()
{
 capacity=strlen(data)+1;
MyString temp;
temp=data;
delete [] data;
capacity*=2;
data= new char[capacity];
}

ostream& operator<<(ostream& out, MyString& s)
{

    out<<s.data;
    return out;


}



// int  MyString::getCapacity(){return capacity;}
  • 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-05-31T00:45:35+00:00Added an answer on May 31, 2026 at 12:45 am

    It should look something like this:

    void MyString::getline(istream& in)
    {
        erase();
        for (char c; in.get(c) && c != '\n'; )
            append(c);
    }
    

    Now you just need to properly implement a method called append that appends a single character. If you have trouble with that, then ask another question. You may think I’m being facetious here, but I am not. You need to limit the places that you reallocate and stop repeating yourself in your code. A getline function is not the place for that sort of activity (reallocations, I mean).

    • 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 my own function like with no luck. ...is not a
I'm trying to write my own function to block events. Here is what I
I'm trying to write my own Mergesort function in the same fashion as C's
I am trying to write my own function for scaling up an input image
I'm trying to write my own modal popup window using a jQuery function. However,
I am trying to write my own Command Line wrapper like 'Console2' in C#.
I'm trying to write my own toy My Toy Language -> MSIL compiler in
I am trying to write our own RIA services provider to expose data from
I have been trying to write my own diff3 wrap script for SVN and
I have been trying to write a small app with its own option windows.

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.