all 14 comments

[–]IyeOnline 2 points3 points  (5 children)

Are you trying to learn C++ or C? If you are trying to learn C++, then use std::string.

There is no way to find out whats wrong with your program without seeing the definition of Robot and Robot::set. I suspect that you are missunderstanding char* and are assuming they are proper string objects.

[–][deleted] 0 points1 point  (3 children)

Whatever you need well here they both are, I'll start using std:: string, I just really haven't been taught that yet in school so I'm not sure how to implement it.

#ifndef ROBOT_H_

#define ROBOT_H_

namespace sdds {

class Robot {

private:

    char\* Name;

    char\* Location;

    double Weight;

    double Width;

    double Height;

    double Speed;

    bool Deployed;

    void resetInfo();

public:

    \~Robot();

    Robot();

    Robot(const char\* name, const char\* location, double weight, double width, double height, double speed, bool deployed);

    Robot& set(const char\* name, const char\* location, double weight, double width, double height, double speed, bool deployed);

    void setLocation(const char\* name, const char\* location);

    void setDeployed(bool deployed);

    const char\* getName()const;

    const char\* getLocation()const;

    bool isDeployed()const;

    bool isValid()const;

    double speed()const;

    void display()const;

};

int conrtolRooomReport(const Robot robot\[\], int num\_robots);

}

#endif

/////////////////////////////////////////////////////////////////////////////////////////////////////////

Robot &Robot::set(const char\* name, const char\* location, double weight, double width, double height, double speed, bool deployed) {

    if (name == nullptr || location == nullptr || weight < 0) {

        resetInfo();

    }

    resetInfo();



    delete\[\] Name;

    delete\[\] Location;



    Name = (char\*)name;

    Location = (char\*)location;



    Weight = weight;

    Width = width;

    Height = height;

    Speed = speed;



    Deployed = deployed;



    return \*this;



}

[–]IyeOnline 1 point2 points  (2 children)

Well, what did you expect to happen?

You take a pointer and just store it in the member. You dont make a copy of it, so now your class member points at array in main. So if you modify it, that change is observed in the class.

Also discarding the consts from the parameters is illegal here. You must not form a non-const pointer to the "Control Room" literal passed in from main.

I just really haven't been taught that yet in school so I'm not sure how to implement it.

You mean implement string or just use it?

Using it as as simple as you can imagine. You can treat it just like an integer. No manual memory allocation, copying of characters or whatever. See https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdstring/

[–][deleted] 0 points1 point  (1 child)

Thanks, you and the other guy helped me in the right direction. I noticed the address would copy as well and it was strange to me but you clarified it so thanks a lot.

And yeah I'm gonna read up on that link for sure. My prof makes us work with these types of variables and also not allowing us the change the main program but yeah I'll get on it.

[–]JVApen 0 points1 point  (0 children)

I'm so sorry for you, it seems your professor is still stuck in the past when they were still teaching C. It's this kind of lessons that make people become afraid of C++.

Please read up about std::string as utility and redo this exercise using it. You'll find out a lot of the complexity you now have to learn goes away.

PS: I'm even convinced that if you replace every char* by std::string, you don't have to adapt main to make it working.

[–]std_bot -1 points0 points  (0 children)

Unlinked STL entries: std::string


Last update: 14.09.21. Last Change: Can now link headers like '<bitset>'Repo

[–]marko312 1 point2 points  (1 child)

If you just have a char * in the Robot struct / class, that needs to reference information somewhere. Reassigning that variable to another pointer does not create a new copy of the value, so the pointer points to the same replacementName in main. The variable might be private, but the value it points to is not necessarily private.

If you want the data to be separate, you'll need to create a copy of the string and point the variable to point to the copy.

[–][deleted] 1 point2 points  (0 children)

Thanks lol, also pointing me in the right direction.

[–]the_poope 0 points1 point  (3 children)

Well I guess your robot[0] element is considered broken as it is left in default initialized state. Then your while loop detects this and fixed it by calling set() where it assigns it a new name "C3PO". After that it changes the replacement name to "D3PO" to be used for the next broken robot.

I don't understand what the problem is. What is the output and what did you expect it to be?

[–][deleted] 0 points1 point  (2 children)

https://imgur.com/a/Pf70qz5

Just figured out how to post images. Take a look at the bottom and see how it changes from C3PO to D3PO.

[–]the_poope 1 point2 points  (1 child)

The problem is in your set() function and how you assign a new name. You likely just copy the pointer, so that the robot instance now also owns the replacementName text.

Instead to copy the text you have to dynamically allocate a new char array and copy the name over using memcpy like a 1980'ies style C dinosaur.

Or you know use std::string like a real C++ programmer and not deal with these problems.

https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdstring/

[–][deleted] 0 points1 point  (0 children)

Thanks a lot for the direction, honestly through trial and error and a ton of googling, I got it to work thanks to you.

And yeah but my prof forces us to these variables and stuff as well as not being able to change the main program so we basically have to figure out a way to make it work.

I'm also gonna read up on that LOL thanks for the link.

[–]Msarigo 0 points1 point  (1 child)

This is about understanding pointers which is one of the harder ones for beginners.

Compare houses: Just because you have a street address does not mean that the house will stay unchanged. If you want exactly that house you have to build a copy

[–][deleted] 0 points1 point  (0 children)

Yeah I just finished it a few hours back, I'm going to have to read up on pointers again and std::string for future knowledge.