Почему cin.get() странно себя ведет?

Код с проблемным cin.get()

 #include <iostream>
using namespace std;

struct STR{
private:
    char* text;
    size_t size;
public:

    void set_text(char* text){
        this -> text = text;
    }
    
    void get_text(){
        for(int i = 0; this->text[i] != '\0' ; ++i)
        cout << this->text[i];
        cout << endl;
    }
    
    void data(){
     cout << "Kol-symbol: " << this->size;   
    }
    
    STR(char* txt){
    this -> text = txt;
     size_t si = 0;
        for(int i = 0 ; this->text[i] != '\0' ; ++i){
            ++si;
        }
       this->size = si; 
    }
    
    STR(){
        char flag = '0';
        while(true){
            
            cout << "выделить место по умолчанию? (y or n): ";
            cin >> flag;
            
            if(flag == 'y'){
            char* new_text = new char[2];
            this->size = 1;
            this->text = new_text;
            break;
            }
            else{
                
            cout << "Создать текст? (y or n): ";
            cin >> flag;
            
                if(flag == 'y'){
                    char ch = 0;
                    int si = 20;
                    int sch = 0;
                    char* new_text2 = new char[si];
                    while( ch != '\n' && sch < si){
                        cout << "@" << endl;
                        cin.get(ch);
                        new_text2[sch] = ch;
                        ++sch;
                    }
                    cout << endl;
                    for(int i = 0; i < si ; i++){
                        cout << new_text2[i];
                    }
                    break;
                }
                else{
                    cout << "Выйти с программы? (y or n)";
                    cin >> flag;
                    if(flag == 'y'){
                        break;
                    }
                }
            }
        }
    }
};

int main(){
    STR text_;
    return 0;
}

Вот такой вот вывод : введите сюда описание изображения

А ожидалось что текст посимвольно запишется с потока в новый массив(если написать Hi!): введите сюда описание изображения

Почему так происходит ?


Ответы (0 шт):