Mutator functions not working, not allowing user input Programming Software Development by emilyhedgecock … void setDescription (string chardescription); // A mutator function for description }; //////////////////////////////////////////////////////////////////////////////// /// constructor …characters::getDescription() { return description; } /// Mutator function for description /// void characters::setDescription (string… Mutator/setter for an array Programming Software Development by Johnathon332 How does one simply make a mutator or setter for an array of strings? I have made a inspector or accessor but cant seem to figure out how to make a mutator. much appreciated. Re: Mutator/setter for an array Programming Software Development by mike_2000_17 … accessor: int operator[](int i) const { return arr[i]; }; // element mutator: int& operator[](int i) { return arr[i]; }; }; [/CODE] And… Combined accessor/mutator? Programming Software Development by daviddoria … delete my mutators now? ie. [code] class foo { int A; //mutator void set_A(const int a) {A=a;} //accessor int get_A…); //set } [/code] vs [code] class foo { int A; //accessor AND mutator int& a() {return A}; } int main() { foo MyFoo; int… Re: Combined accessor/mutator? Programming Software Development by daviddoria I don't understand how you would not need mutators... [code] class Scanner { Point Location; Scanner(Point &Loc) : Location(Loc) {} }; int main() { Scanner MyScanner(Point(0,1,0)); //now how do I move the scanner to a new location without a mutator setLocation() ? return 0; } [/code] What do you think of that example? Dave How to create a mutator method Programming Software Development by cdominguez13 …;, " + fname; return name; } } Now I need to create a mutator method that lets the client change the gpa. It must… Re: How to create a mutator method Programming Software Development by moutanna Your mutator sould look like: [CODE] public void setGpa(double studentGpa) { if((studentGpa>=0.0) && (studentGpa<=4.0) { //gpa is in the range 0.0 to 4.0 (inclusive). gpa=studentGpa; }else{ //display an error message System.out.println("Value out of range!"); //end the program System.exit(0); } } [/CODE] Hope it helps. Re: Mutator functions not working, not allowing user input Programming Software Development by n1cko Hi Em! Lines 114,119,124,129 - should they be character2.<function> ? Nick Re: Mutator functions not working, not allowing user input Programming Software Development by emilyhedgecock yes they should well spotted, but it still goes crazy when i swap the order about, i put the character 2 bits quickly on the end to see what affect it had - thanks for your input Nick! Re: Mutator functions not working, not allowing user input Programming Software Development by mike_2000_17 This is a classic problem that comes up almost every day on this forum. The reason for this problem is that when you do a getline, it will get the string up to the first newline (enter stroke) and will delete that newline from the input buffer. But, when you get a numeric variable (int or char or double) it will get the first valid value (ignoring … Re: Mutator functions not working, not allowing user input Programming Software Development by emilyhedgecock i just made the corrections. it does the character perfectly but then it goes "Please enter character name: Please enter character description" rather than doing the 2 separately. Re: Mutator functions not working, not allowing user input Programming Software Development by emilyhedgecock Mike Yes thats it!! Thank you soo much - will definitely keep note of this for future reference! Quite surprised its not mentioned in any of the tutorials i have been reading - i think i did something of with a similar theory when doing C Thanks again! Emily Re: Mutator/setter for an array Programming Software Development by Lerner [code]struct StringArray { char sArray[10][10]; void setString(char *s, int pos) {strcpy(sArray[pos], s);} };[/code] setString should change/set/mutate sArray by assigning s to element pos in sArray. You need to use strcpy() to "assign" one C style string to another. You would need to be sure there is enough room in … Re: Combined accessor/mutator? Programming Software Development by daviddoria and actually, why not just [code] class foo { int A; }; int main() { foo MyFoo; int b = MyFoo.A; //get MyFoo.A = 4; //set } [/code] ?? Re: Combined accessor/mutator? Programming Software Development by Murtan Well...that last example won't work because classes default to private. If you put a public: in front of the int A it would work. However both of the last examples (directly accessing the member or a reference to the member) are violating the principle of data hiding. Anyone using the class now has to know about how it is implemented. (Granted … Re: Combined accessor/mutator? Programming Software Development by daviddoria Ok - but for simple cases (just like this one, where all it is doing is storing a number), it's not bad practice to just make it public? Often times I just have a huge list of properties: [code] int NumPoints; double ThetaMin, ThetaMax, PhiMin, PhiMax, PhiStep, ThetaStep; [/code] And the list goes on and on.... then I have to write accessors and… Re: Combined accessor/mutator? Programming Software Development by Narue >but for simple cases (just like this one, where all it is doing is >storing a number), it's not bad practice to just make it public? It depends on whom you ask this question. Some people will tell you that public data is absolutely evil. Others will tell you that it's okay sometimes, but you should avoid it. In the usual case, … Re: Combined accessor/mutator? Programming Software Development by Murtan This is heading into what I think the key difference is in a lot of designs. Are you using C++ and classes and polymorphism or are you writing object oriented programs. There is a distinct difference between a program with classes and a program written around objects. The difficulty comes in coming up with the right object and the right … Re: Combined accessor/mutator? Programming Software Development by daviddoria I guess I don't know the difference between using class and writing object oriented programs? I thought an object oriented program was one which used classes (aka objects)? What I have is a Scanner class and a Scan class. I do something like this [code] Scanner MyScanner; Scan MyScan = MyScanner.TakeScan(); [/code] Is that enough to tell you … Re: Combined accessor/mutator? Programming Software Development by Narue >I thought an object oriented program was one which used classes (aka objects)? I don't think anyone agrees on terminology, but I use classes regularly (they're awesome!) and I wouldn't say I write object oriented programs. I'd break it up like this: [B]Object Assisted:[/B] Basically a structured approach with the odd object here and there to … Re: Combined accessor/mutator? Programming Software Development by Rashakil Fol First of all, Narue's definitions are BS. (What the fuck is a "full" inheritance hierarchy?) Second, mutators are almost always a sign of design problems, unless your problem provably requires mutators, but getters aren't. Well, if you have mutators that aren't provably necessary, it's evidence that there's a "better" … Re: Combined accessor/mutator? Programming Software Development by Rashakil Fol I would construct a new Scanner with a different location. Re: Combined accessor/mutator? Programming Software Development by daviddoria but the constructor really looks like [code] Scanner::Scanner(int NumPoints, int MinTheta, int MaxTheta, int ThetaStep, int MinPhi, int MaxPhi, int PhiStep, Point Location, Transformation T, double PointSpeed) [/code] It seems absolutely crazy to me to construct a new Scanner when all that needs to be changed is the location, no? Re: Combined accessor/mutator? Programming Software Development by Rashakil Fol First of all, it would be, at the very least, [icode]Scanner::Scanner(int NumPoints, RangeStep& Theta, RangeStep& Phi, Point Location, Transformation T, double PointSpeed)[/icode], or even something more clumpy. I suspect I'd only have 2 or 3 parameters to the constructor, the rest part of helper classes. Second, I'd probably pass some … Re: Combined accessor/mutator? Programming Software Development by Murtan It's all based on your object model. If you write the model so that you need to set the location, then you need a setLocation. I'm sure I could come up with (or you could come up with) several examples that require either public access or mutators. For example, your Point class (or struct) very likely has public or mutable members. In this … Re: Combined accessor/mutator? Programming Software Development by daviddoria Thanks for the discussion guys - gives me something to think about. [QUOTE] First of all, it would be, at the very least, Scanner::Scanner(int NumPoints, RangeStep& Theta, RangeStep& Phi, Point Location, Transformation T, double PointSpeed) , or even something more clumpy. I suspect I'd only have 2 or 3 parameters to the constructor, the … Creating an array where each element is a class ?:S Programming Software Development by emilyhedgecock … { healthPoints=healthPoints-subtractHealthPoints; } /// Mutator function for addExperiencePoints bool playingCharacterClass::addExperiencePoints(int …{ attackPoints=attackPoints+newAttackPoints; } //////////////////////////////////////////////////////////////////////////////// // mutator - Add an item to inventory bool itemClass… Re: Creating an array where each element is a class ?:S Programming Software Development by emilyhedgecock …attackpoints /////////////////////////////// bool addAttackPoints(int newAttackPoints); // a mutator function to subtract attack points bool subtractHealthPoints(int subtractHealthPoints…; public: bool addExperiencePoints(int points);// a mutator function to add experience points bool setPoints(int… Re: Creating an array where each element is a class ?:S Programming Software Development by Stefano Mtangoo …attackpoints /////////////////////////////// bool addAttackPoints(int newAttackPoints); // a mutator function to subtract attack points bool subtractHealthPoints(int subtractHealthPoints…; public: bool addExperiencePoints(int points);// a mutator function to add experience points bool setPoints(int… Re: Creating an array where each element is a class ?:S Programming Software Development by Stefano Mtangoo …attackpoints /////////////////////////////// bool addAttackPoints(int newAttackPoints); // a mutator function to subtract attack points bool subtractHealthPoints(int subtractHealthPoints…; public: bool addExperiencePoints(int points);// a mutator function to add experience points bool setPoints(int…