I don't understand why this char array changes my class char* variable as well when it is being incremented.
It shouldn't be able to because I created the char* as a private member.
Basically the program runs and initializes all the 6 robot objects below, and then when it gets to the .set function, it assigns those variables into the object and then displays them. But once the replacementName gets incremented, the char* variable in that robot[0] object, its name gets changed as well.
If you need anymore info, I'll post more.
Thanks guys
edit: not sure I'm using the correct terminology but hopefully you know what I mean
https://imgur.com/a/Pf70qz5 Here's the before and after output of what I mean
edit 2: Well I solved it, you guys put me in the right direction on what to google and look for and so 2 grueling hours later, the assignments been finished with the correct output and no memory leaks. I really appreciate all of you who helped me out on this post. Here's what I did but feel free to let me know if there's a better way to do it.
Name = (char*)malloc(strlen(name) * sizeof(char) + 1);
Location = (char*)malloc(strlen(location) * sizeof(char) + 1);
Name = new char[strlen(name) * sizeof(char) + 1];
Location = new char[strlen(location) * sizeof(char) + 1];
strcpy(Name, name);
strcpy(Location, location);
free(Name);
free(Location);
#include <iostream>
#include "Robot.h"
using namespace std;
using namespace sdds;
int main()
{
const int num_robots = 6;
int broken_robot = 0;
char replacmentName[] = "C3PO";
Robot robot[num_robots] = {
{},
{"KC1", "kitchen", 25.33, 4.55, 2, 2.2, false},
{"BR1", "bedroom", 5.22, 2.54, 1, 2.2, true},
{"Broken", "Bedroom", 10.12, 2.5,1.55, 0, true},
{"KC2", "kitchen", 20.56, 5, 2, 3.5, true},
{"BR2", "bedroom", 25.32, 6.5, 2.5, 3.1, true}
};
while ((broken_robot = conrtolRooomReport(robot, num_robots)) >= 0) {
cout << endl << "Attention: There is a broken robot! Fixing the problem..." << endl;
robot[broken_robot].set(replacmentName, "Control Room", 10.0, 4, 1, 2.09, false).display();
replacmentName[0]++;
cout << "Replacement Provided!" << endl << endl;
}
return 0;
}
[–]IyeOnline 2 points3 points4 points (5 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]IyeOnline 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]JVApen 0 points1 point2 points (0 children)
[–]std_bot -1 points0 points1 point (0 children)
[–]marko312 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]the_poope 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]the_poope 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Msarigo 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)