Aim of the below code is to take input strings of any length from cin (or for tha matter input stream)
istream& operator>>(istream& is, MyString& rhs)
{
    cout << __PRETTY_FUNCTION__ << endl;
    
    size_t inpChunkSize{256};
    size_t allocCap{256};
    size_t totReadBytes{0};
    char *tmpHoldingBuff{nullptr};
    char *input = new char[allocCap];
    while(!cin.eof())
    {
        cin.read(input + totReadBytes, inpChunkSize);
        totReadBytes += cin.gcount();
        // if 256 bytes have been used up, then new chunk has to be allocated. 
        if (cin.gcount() == inpChunkSize)
        {
            allocCap += inpChunkSize;
            tmpHoldingBuff = new char[allocCap];
            strncpy(tmpHoldingBuff, input, totReadBytes);
            
            delete [] input;
            
            input = tmpHoldingBuff;
        }
    }
    
    rhs = MyString(input);
    rhs.display();
    return is;
}
int main(int argc, char *argv[])
{
    
    MyString s1, s2;
    cin >> s1 >> s2;
    
    return 0;
}
I want s1 and s2 to be input just like we do for say int's.
But I see that once I input one string and do ^D, then input for s2 is never asked and it is initialised to null
What wrong is happening here ?
$ ./Misc/OLNonMembFuncs 
std::istream& operator>>(std::istream&, MyString&)
This is s1^D(This is s1)
std::istream& operator>>(std::istream&, MyString&)
()
Aucun commentaire:
Enregistrer un commentaire