0

I'm using insert() function to take input in which I'm trying a different methods to take input but it's now working

Here is the full code. Can anyone please help me how to take an input string

#include <iostream>

using namespace std;

class _string{
private:
    char *A;
    int size;
public:
    _string(){
        size = 10;
        A = new char[size];
    }
    _string(int sz){
        size = sz;
        A = new char[size];
    }
    ~_string(){
        delete []A;
    }

    void insert();
    void view();


};

void _string::insert(){
    cout<<"enter string: ";
    cin.getline(A,size);
}
void _string::view(){
    puts(A);
}

int main()
{
    _string *arr;
    int n;
    cout<<"Enter the size of string: "; cin>>n;

    arr = new _string(n);

    arr->insert();
    arr->view();
    
    return 0;
}

But when I'm using c function scanf("%s[^\n],A); its working ....

so please tell me how to use fgets(),getline(),cin.get() in my code

  • `_string` [is an illegal identifier](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). Don't know if that's part of your woes yet. – user4581301 Aug 29 '20 at 07:03
  • "It's now[sic] working" is not a proper description. ***How*** is it "no**t** working"? Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Aug 29 '20 at 07:09
  • TL;DR version. The carriage return sitting in the stream after `cin>>n;` is being picked up by `getline`'s scan for a line ending and terminating the `getline` instantly with an empty string. – user4581301 Aug 29 '20 at 07:11

0 Answers0