0

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;
}
David G
  • 94,763
  • 41
  • 167
  • 253
kbgb
  • 21
  • 4
  • 8
    *"sorry for the code mess"* Yeah, you should fix that instead of apologizing. Post an [SSCCE](http://www.sscce.org). – Baum mit Augen Apr 20 '15 at 18:37
  • Please don't use `char*` to point to constant data. And comparing a character with `NULL` is very fishy. `NULL` is for pointers. Also see http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope. – chris Apr 20 '15 at 18:38
  • you have an overload of `operator>>` but using `cin.getline` ... – user1810087 Apr 20 '15 at 18:39
  • What is `BUFFER`? You have a local variable `temp` and you're assigning it to `Str`, which I know won't lead to good things happening. Also, I think it is not a good idea at all to even have an overload of `operator >>` for your string class. An overload of `<<`, but not `>>`. – PaulMcKenzie Apr 20 '15 at 18:46

1 Answers1

1

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270