-1

This is a practice that we did in our robotics club a while ago. We were supposed to create to classes, one that would assign user input to a variable (cin) and another one that would print it out (cout). We needed to use pointers to acheive this, and this is what I came up with.

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

using namespace std;

class Input
{
public:
    string* textString = new string();

    Input(string uInput)
    {
        textString = &uInput;
    }

    void userInput() {
        cout << "Enter some text - ";
        cin >> *textString;
    }
private:
    // Nothing yet
};


class Output
{
public:
    string* inputText = new string();

    Output(string uInput)
    {
        inputText = &uInput;
    }

    void userOutput() {
        cout << *inputText << endl;
    }
private:
    // Nothing yet
};

int main()
{
    string userInput = "EMPTY";
    cout << &userInput << endl;

    Input i = Input(userInput);
    i.userInput();

    Output o = Output(userInput);
    o.userOutput();

    return 0;
}

However, it doesn't seem to work. When I run this in Visual Studio 2018, and enter a value, it waits a couple of seconds and prints "Press any key to continue..." and ends the program. Nothing appears in the compiler either. Could somebody with more C++ knowledge help me understand what is wrong with my code? Thanks!

George B
  • 918
  • 3
  • 15
  • 24
  • 2
    There are so many wrong things here, it's hard to decide where to start. Memory leaks. Undefined behavior due to addresses to temporary objects. There's definitely a very fundamental misunderstanding here how C++ works. The best thing for you to do is to spend some time [reading a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). There isn't just one problem here, but a couple of them. Oh, and there are no "global variables" here; so also there's a terminology issue. – Sam Varshavchik Dec 07 '18 at 01:39
  • What is the expected outcome? That `o.inputText` should have the content entered by the user? – Kingsley Dec 07 '18 at 02:11
  • To Sam - I understand that my code is poorly written, because I am new to C++. That is why I am asking this question - because I don’t know why my code doesn’t work, and internet sources haven’t helped me. – George B Dec 07 '18 at 02:13
  • To Kingsley - Yes, that was what I wanted. – George B Dec 07 '18 at 02:14

1 Answers1

1

The code is not creating the i and o objects correctly. I guess you could have said Input *i = new Input(userInput); - that would work, but requires further changes too.

I changed your Input::userInput() to take a pointer to a string. This same sort of pointer-to-argument layout would work for modifying both base-types and objects.

I'm really not fond of using cin and cout, personally I'd use fgets(), and then put the value from that into your string.

#include <string>
#include <iostream>


class Input
{
    private:
    std::string textString;

    public:
    Input ( std::string uInput )
    {
        textString = uInput;
    }

    void userInput( std::string *intoString )
    {
        std::cout << "Enter some text - ";
        std::getline( std::cin, textString );
        *intoString = textString;
    }
};


class Output
{
    private:
    std::string inputText;

    public:
    Output( std::string uInput )
    {
        inputText = uInput;
    }

    void userOutput()
    {
        std::cout << inputText << std::endl;
    }
};

int main(  )
{
    std::string userInput = "EMPTY";
    std::cout << userInput << std::endl;

    Input i( userInput );
    i.userInput( &userInput );

    Output o( userInput );
    o.userOutput();

    return 0;
}

It's not clear to me exactly how the Input object should work. The local textString seems redundant, but I tried to use it anyway.

Kingsley
  • 14,398
  • 5
  • 31
  • 53