-4

I'm writing a code for my classes, and I'm facing a problem. I've never before worked with strings in C++, and I've got no idea how to separate one string into two others. Also the program compiles, but the output is wrong, and I'm suspecting the constructors. Can somebody please take a look and give me an anaswer to what should I change for the code to work properly? I'd be most grateful :)

Here's the code: main (it was given by the teacher)

   int main()
{
    oop::Data a;
    oop::Data b;

  {
        MACRO1;//co tu ma się dziać?
    Data b;     
  }

    const oop::Data Data_element = "Test-first"; // tworzony jest obiekt const o nazwie Data_element klasy Data, do którego wpisywany jest teks

    const oop::Data c(Data_element, "Test-second"); // wywołanie konstruktora

    a.full_data("Pierwszy Drugi");
    b.first() = b.second("Tekst testowy"); //co tu ma się dziać?

    std::cout
        << "a = " << a << std::endl;
    std::cout           
        << "b = " << b.full_data() << std::endl;
    std::cout
        << "c = " << c.first() << ", " << c.second() << std::endl;


    const oop::Data d = a.to_upper(); //zamienia na WIELKIE litery
    oop::Data e = d.to_lower(); //zamienia na mnałe litery

    std::cout
        << "d = " << d << std::endl;
    std::cout
        << "e = " << e << std::endl;

}

/* Output:
a = ["Pierwszy", "Drugi"]
b = ["Tekst testowy", "Tekst testowy"]
c = Test-first, Test-second
d = ["PIERWSZY", "DRUGI"]
e = ["pierwszy", "drugi"]
*/

.h file:

#define MACRO1 using namespace oop;
//Makro !

#include <string>
#include <iostream>


namespace oop{

    class Data
    {
    public:
        Data(const char*);//Konstruktor ma przyjac const char* a nie string bo " text " to const char*
        Data(const Data&,const char*);
        Data();
        Data(const Data&);//dla linijki 35
        ~Data();


        void first_data(char* data);
        std::string first() const;
        const char* second()const;//to sie przyda
        std::string second(std::string data) const;
        //const char* full_data(const std::string data);
        void full_data(const std::string data);
        const char* full_data();
        Data to_upper(void) const;//const dodajesz tutaj zawsze jak nie zmieniasz obiektu zeby metoda dzialala na const obiekcie
        Data to_lower(void) const;

        friend std::ostream & operator <<(std::ostream & wypisz, const Data& data);


    private:
        const char* _text1;
        const char* _text2;

    };

    inline std::ostream & operator <<(std::ostream & wypisz, const Data& data)
    {return (wypisz<<"[\"" << data._text1 << " \", \" " << data._text2 << "\" ]");}

}
#endif

and functions in .cpp

    #include <iostream>
#include "lab07.h"
#include <string>

//konstruktor*******************************************************************************************************
    oop::Data::Data(const char* c)
    {_text1=c;}

    oop::Data::Data(const Data& object,  const char* c)
    {
        _text1 = object._text1;
        _text2 = c;
    }

    oop::Data::Data(){} //tu chyba nic nie jest potrzebne
    oop::Data::Data(const Data& object)
    {
        _text1 = object._text1;
        _text2 = object._text2;
    }

    oop::Data::~Data(){} //tu chyba nic nie jest potrzebne

//funkcje*******************************************************************************************************
    void oop::Data::first_data(char* data){}

    std::string oop::Data::first()const
    {return std::string(" ");}

    std::string oop::Data::second(std::string data)const
    {return std::string(" ");}

   /* const char* oop::Data::full_data(const std::string data)
    {return " ";}*/
    void oop::Data::full_data(const std::string data)
    {
        int size = data.size();
        int space = data.find(' ');
        //_text1 = data.substr(0,space);
       // _text2 = data.substr(space,size);



    return ;}

    const char* oop::Data::full_data()
    {return " ";}

    oop::Data oop::Data::to_upper(void)const
    {return *this;}

    oop::Data oop::Data::to_lower(void)const
    {return *this;}

    const char* oop::Data::second()const
    {return " ";}
b4hand
  • 9,550
  • 4
  • 44
  • 49
ninigi
  • 143
  • 1
  • 3
  • 14
  • Could you please change the commends into English ? – Meghdeep Ray May 28 '15 at 06:05
  • What output does it give, and what do you expect? – b4hand May 28 '15 at 06:12
  • @b4hand my psychic powers predict: /* Output: a = ["Pierwszy", "Drugi"] b = ["Tekst testowy", "Tekst testowy"] c = Test-first, Test-second d = ["PIERWSZY", "DRUGI"] e = ["pierwszy", "drugi"] */ – user4581301 May 28 '15 at 06:13
  • 1
    You should worry about the complete lack of implementation of your *funkcje* before you start suspecting the constructors or splitting strings. (Are you *really* surprised that doing nothing or returning `" "` produces the wrong result?) Before you start separating strings into more strings, get the rest of the code working. Also, if you're allowed `std::string`, do it consistently. – molbdnilo May 28 '15 at 06:50

1 Answers1

0

Whole bunch of ways to do this. Here are two:

Quick way: stringstream

stringstream ss(data);
string tstr;
ss >> tstr;  // every call to >> reads one bunch of characters up to the first whitespace.

Then make a suitably sized char array, copy tstr into the char array, and assign the char array to _text1. Repeat for _text2. One gotcha: a string like "the quick brown fox" will become "the" and "quick" the rest of the string will be lost.

The hard way: find and substr

string tstr;
size_t loc = data.find(' ');
if (loc != data.npos)
{
    tstr = data.substr(0, loc);
    //Copy tstr into _text1
    tstr = data.substr(loc + 1, data.npos);
    //Copy tstr into _text2
}

This one is a bit better for the "the quick brown fox" case. It will produce "the" and "quick brown fox".

user4581301
  • 33,082
  • 7
  • 33
  • 54