Program sie czasami zawiesza czasami nie

0

Witam problem z poniższym kodem jest taki że zawsze wyświetla to co powinien jednak czasami po wyświetleniu tekstu wyświetla się komunikat Program 2.exe przestał działać a czasami program wykonuje się bez problemów. Mój OS to Windows 7 32 bit poniżej zamieszczam cały kod metody dopisane przeze mnie metody to przeciążanie operatora dodawania, metoda stringup i metoda stringlow reszta kodu pochodzi z książki Szkoła Programowania Język C++ Wydanie V

//string2.h
#include <iostream>
using std::ostream;
using std::istream;

#ifndef STRING2_H_
#define STRING2_H_

class String
{
    private:
        char * str;                  //wskaŸnik ci¹gu
        int len;                     //d³ugoœæ ci¹gu
        static int num_strings;      //liczba obiektów klasy
        static const int CINLIM = 80;//limit d³ugoœci ci¹gu na wejœciu
    public:
        //konstruktory i pozosta³e metody klasy
        String(const char * s);      //konstruktor
        String();                    //konstruktor domyœlny
        String(const String &);      //konstruktor kopiuj¹cy
        ~String();                   //destruktor
        int lenght () const {return len;}
        void stringlow();
        void stringup();
        //metody przeci¹¿aj¹ce operatory
        String & operator=(const String &);
        char & operator[] (int i);
        const char & operator [] (int i) const;
        String & operator+(const String &);
        //funkcje zaprzyjaŸnione przeci¹¿aj¹ce operatory
        friend bool operator<(const String & st, const String & st2);
        friend bool operator>(const String & st1, const String & st2);
        friend bool operator==(const String & st, const String & st2);
        friend ostream & operator<<(ostream & os, const String & st);
        friend istream & operator>>(istream & is, String & st);
        //metoda statyczna
        static int HowMany();
};
#endif 
//string2.cpp
#include <cstring>
#include <cctype>
#include "string2.h"
using std::cin;
using std::cout;

//inicjalizacja statycznej skladowej klasy
int String::num_strings = 0;

//metoda statyczna
int String::HowMany()
    {
        return num_strings;
    }

//metody klasy
String::String(const char * s)      //konstruuje obiekt String z ciagu C
    {
        len = std::strlen(s);       //ustawienie dlugosci ciagu
        str = new char[len + 1];    //przydzial pamieci
        std::strcpy(str, s);        //incijalizacja wskaznika ciagu
        num_strings++;              //aktualizacja licznika obiektow
    }

String::String()                    //konstruktor domyslny
    {
        len = 4;
        str = new char[1];
        str[0] = '\0';              //domyslny ciag obiektow klasy
        num_strings++;
    }

String::String(const String & st)
    {
        num_strings++;              //aktualizacja skladowej statycznej
        len = st.len;               //ta sama dlugosc ciagu
        str = new char [len + 1];   //przydzial pamieci
        std::strcpy(str, st.str);   //skopiowanie ciagu
    }

String::~String()                   //destruktor(niezbedny)
    {
        --num_strings;               //koniecznie
        delete [] str;              //koniecznie
    }

void String::stringlow()
    {
        for(int i = 0; i<strlen(str); i++)
            {
                str[i] = tolower(str[i]);
            }
    }

void String::stringup()
    {
        len = strlen(str);
        for(int i = 0; i<len; i++)
            {
                str[i] = toupper(str[i]);
            }
        str[len + 1] = '\0';
    }

//metody przeciazajace operatory
//przypisywanie obiektu klasy String do innego obiektu tej klasy
String & String::operator=(const String & st)
    {
        if(this == &st)
            return *this;
        delete [] str;
        len = st.len;
        str = new char [len + 1];
        strcpy(str, st.str);
        return *this;
    }

//przypisywanie ciagu C do obiektu klasy String
String & String::operator=(const char * s)
    {
        delete [] str;
        len = std::strlen(s);
        str = new char [len + 1];
        std::strcpy(str, s);
        return *this;
    }

String & String::operator+(const String & st)
    {
        len = strlen(st.str);
        int temp_len = strlen(str);
        char * temp;
        temp = str;
        len = temp_len + len;
        str = new char [len + 1];
        str = strncat(temp, st.str, len+1);
        str[len + 1] = '\0';
        return * this;
    }

//pelny dostep do znakow ciagu(dla obiektow zwyklych)
char & String::operator[](int i)
    {
        return str[i];
    }

//dostep (do odczytu) do znakow ciagu(dla obiektow const)
const char & String::operator[] (int i) const
    {
        return str[i];
    }

//zaprzyjaznione funkcje przeciazajace operatory
bool operator<(const String & st1, const String & st2)
    {
        return (std::strcmp(st1.str, st2.str) < 0);
    }

bool operator>(const String &st1, const String &st2)
    {
        return st2.str < st1.str;
    }

bool operator==(const String &st1, const String &st2)
    {
        return (std::strcmp(st1.str, st2.str) == 0);
    }

//wyprowadzenie ciagu na wyjscie
ostream & operator<<(ostream & os, const String & st)
    {
        os << st.str;
        return os;
    }

//wczytywanie ciagu z wejscia(uproszczone)
istream & operator>>(istream & is, String & st)
    {
        char temp[String::CINLIM];
        is.get(temp, String::CINLIM);
        if (is)
        st = temp;
        while(is && is.get() != '\n')
            continue;
        return is;
    }
 
//pe12_2.cpp
#include <iostream>
#include "string2.h"

using namespace std;

int main()
{
    String s1(" uczy sie C++.");
    String s2 = "Podaj swoje imie: ";
    String s3;
    cout << s2;     //przeciazony operator <<
    cin >> s3;      //przeciazony operator >>
    s2 = s3 + s1;   //przeciazony operator = i +
    s2.stringup();  //zamiana liter malych na duze
    cout << s2;
}
 
0

Wiesz,co to takiego debugger i do czego służy?

0

wiem co to jest ale nie wiem jak użyć debuggera w C::B bo jeśli po prostu wejdę w zakładkę debug i wybiorę start to pokazują mi się dwa okna jedno puste z konsolą a drugie z nr adresem i funkcją ale nie wiem co to znaczy

1 użytkowników online, w tym zalogowanych: 0, gości: 1