Q:

C++ ostream output wrong

I have a problem and I can't solve it.
note: I can't use any string/strlen/strcmp and such functions
My problem is when I'm trying to print obj after cin, when I don't put him value it prints garbage, and if I put value it change it by cin it print it well.
#ifndef __STRING_H__

#define __STRING_H__

#include <iostream>

using namespace std;

class String {
private:
    char* Str; //pointer to string 
    int Str_s; // size of string public:
    String(char* str = NULL);
    ~String();
    void const print() const; //check
    //~~~~~~~~~~~~~~~~~~
    //    operators
    //~~~~~~~~~~~~~~~~~
    String& operator=(const String& str);
    bool operator==(const String& s) const;
    bool operator!=(const String& s) const;
    void operator-=(const char c);
    void operator+=(const char c);
    char& operator[](const int index) const;
    //~~~~~~~~~~~~~~~~~~
    // i/o ~~ operators
    //~~~~~~~~~~~~~~~~~
    friend ostream& operator<<(ostream&, const String& s);
    friend istream& operator>>(istream&, String& s);
    //~~~~~~~~~~~~~~~~~~
    // arrange method
    //~~~~~~~~~~~~~~~~~
    void Letters(); // arrange all letter in sentence
    void Space(); //remove all unwanted spaces
    void Set_Str_s(int num) {
        this->Str_s = num;
    }
    int Get_Str_s() {
        return this->Str_s;
    }
    //~~~~~~~~~~~~~~~~~~
    //     methods
    //~~~~~~~~~~~~~~~~~
    char const* STR() {
        return Str;
    }
};

#endif

ostream& operator<<(ostream& output, const String& s) {
    //char *c = s.Str;
    //output << c << endl;
    for (int i = 0; i < s.Str_s; i++) {
        cout << s.Str[i];
    }
    return output;
}

istream& operator>>(istream& input, String& s) {
    if (s.Str == NULL) {
        s.Str_s = 0;
        char temp[BUFFER];
        int i = 0, j = 0;
        cin.getline(temp, BUFFER);
        while (temp[i] != NULL) {
            s.Str_s++;
            i++;
        }
        //s.Str = new char[s.Str_s];
        s.Str = temp;
    } else {
        input >> s.Str;
    }
    return input;
}

#include <iostream>
using namespace std;

#include "string_head.h"
#include "Definition_head.h"
#include "Menu_head.h"

int main() {
    char* c = "hi";
    char* h = "lilo";
    String s, m(c);
    cin >> s;
    cin >> m;
    cout << s;
    cout << endl;
    cout << m;
}

A:

The following line is a problem:
    s.Str = temp;

you are storing a pointer to a local array that will be deleted when the function returns. Use something like:
    s.Str = strdup(temp);

If your platform doesn't support strdup, you can implement it. It's not too hard.
The following line is also a problem:
    input >> s.Str;

If there isn't enough memory to hold the string being read, you will be writing into memory that you are not supposed to.

