need help getting member values from an object pointer in a class function | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

need help getting member values from an object pointer in a class function

title pretty much describes my problem. i can't get the member values of class objects in that class function. i'll post the parts in question to show what it currently looks like. i've altered it every which way a beginner like me can think to do. and can't seem to get the value. ========================================================= class character //only included parts relevant to question { public: void Strike(character *target) /*originally &target and i used target.member instead of target->member */ { genericint = (this->striking + randomint(0, 10)) - (target->dodge + randomint(0, 10)); //the above values always = 0-10. if (genericint > 0) { genericfloat = (this->striking / 10) * (this->strength/2); target->chealth -= genericfloat; genericint = genericfloat; genericstring = this->name; cout << genericstring << " Strikes " << target->name << "! dealing " << genericint << " damage." << endl; //results in " Strikes ! dealing damage." same problem on both couts. genericfloat = (target->chealth/target->health) * 100; genericint = genericfloat; genericint = genericfloat; cout << target->name << " has " << genericint << "% health left." << endl; } else { cout << target->name << " Avoids " << this->name << "'s attack" << endl; } } } int main() { character C1("John", 55, 0, 5, 5); //irrelevant details character C2("Cathy", 23, 1, 4, 2);//irrelevant details character *c1 = &C1; character *c2 = &C2; c2->Strike(c1); return 0; }

30th Jun 2021, 3:36 AM
AndXr
6 Réponses
+ 3
AndXr are you talking about line 179 `this->striking / 10` It is not always 0, sometimes it's 1. It is 0 when striking < 10. This is because `striking` is an integer, and when an integer is divided by an integer, the result is always an integer, i.e. the decimal part is ignored. So for example, if striking=9, striking/10 will result in 0.9 and as the decimal part ignored, the final result would be 0.
30th Jun 2021, 9:53 AM
XXX
XXX - avatar
+ 2
Looks like your `character` class is not storing the `name` property correctly. Can't say anything from the code you've given here. Can you maybe post the whole code or atleast the constructor of your `character` class? Also, instead of pasting the code in the question description like you've done here, save it in a C++ code in the code playgroynd and link it here. That makes it easier to read, gets rid of the character limit problem and makes it easier to reference line numbers and compiler errors/warnings.
30th Jun 2021, 4:33 AM
XXX
XXX - avatar
+ 2
https://code.sololearn.com/ca8a22A8A15a here. I tried to post a msg earlier. but it seems to have bugged. I fixed all of the problems except 1. and thats "striking/10 " always = 0 as a float value. the missing name was from an oversight. when i changed from cin to directly adding the value on object creation. now i just have to figure out how to make a wait or delay function lol.
30th Jun 2021, 7:17 AM
AndXr
+ 2
AndXr Also, if you don't mind me saying, your code is currently very unreadable. Having variables like `generic...` as class members doesn't make sense, especially since you're using them only in the Strike() method. Try keeping variables as local as possible. Declare, initialize and use the variables `generic...` inside the Strike() method only. These variables are also named badly. I mean, if someone sees `genericint`, they won't know what type of value it holds. Whereas a variable like `strength` or `damage` makes it clear what it holds. Don't hesitate from making new variables if you have to. I would make a cleaner version of your code as example, but I don't understand what you're trying to do at many of the places.
30th Jun 2021, 9:54 AM
XXX
XXX - avatar
+ 2
Well. Thank you for the advice. The generic variables are so I can freely use them for temporary value storage without making unecessary new ones. I feel like that is the More optimized option. And atleast to me. It tells me that variable is generic, and freely changeable, and not important to any other function. The actual script I have is alot longer than that bit I put there, where it is used in more situations. I'm mainly only using it to teach myself how to actually use the language. As I haven't done much practice with alot of the different syntax yet. So I'm not up to making simple, readable or optimized code yet lol. I'm slowly re-writing, changing and simplifying as I go. And about the int part. I thought that if the variable I was editin the value of (being a float) would allow the int values to math as floats. Considering I'm only referencing their values for the math, and not trying to store that value on them. Atleast, that's my experience with modding games with various different languages.
30th Jun 2021, 1:18 PM
AndXr
+ 2
AndXr "I thought that if the .... math as floats" It would, but after `this->striking / 10` has been evaluated. The resulting 0 (or 1) will be converted to float. You can get the exsct result of the division by changing 10 to 10.0f or by explicitly casting this->striking to float. All the following alternatives would do what I described above `this->striking / 10.0f` `(float) this->striking / 10` `(float) this->striking / 10.0f`
30th Jun 2021, 1:55 PM
XXX
XXX - avatar