I am trying to change an initialized value that I have to a new value, How do I do this? It's a value that I declared within a class constructor, but now I need the user to be able to change it.
I tried using new but and when I declared it under public: it give me this
expected unqualified-id before 'new' so I'm not quite sure how to change this value....
Line 10 is not needed: R_NewNomValue>=0||R_NewNomValue<=1000000;
returntrue; at line 14 will prevent the value from changing. Move this line after line 16.
Also, ideally the message cout << "Your new Resistance is:" would output the value of R_nominalValue after the change, rather than as at present where the input parameter is printed.
I understand what you are saying but I already have a R_nominalValue and I need to change the gett value because it is used in other parts of my code for math stuff....
Beware of the return statement which does two things: it specifies what value will be passed back to the calling function, and it also immediately exits from the function, so that no statements after the return will be executed. The first version says, in effect "yes, success" but then quits early without modifying anything.
The second version is heading somewhat in the right direction (apart from a typo on the last line).
But this line is very wrong. if (R_Newtolerance= 1,2,5,10)
the = operator is used to assign a new value to something. You need to use == in order to test for equality. In addition, you need to test each value individually, the comma operator is something else entirely.
See Comma operator here for more information. http://www.cplusplus.com/doc/tutorial/operators/
Yes, that looks good - full marks for figuring out the if statement.
(Though the final verdict would be given by testing to see whether it works - the eye can sometimes overlook something).
Edit:My mistake, its wrong I'm afraid. I think you should compile and test the code, one of the benefits of a compiler is that it highlights obvious problems.
Nope if I hit 1,2,5 or 10 they al return a false and it doesn't ask them again to input it just goes to the next part of my test?? That and if you type an invalid input for the resistance part I wrote it doesn't return a false... LoL I'm a mess
Yes I did... No compiler messages, just moves to the next step after returning false error message that I have as cout. not quite sure what your line two is. I understand it is a pointer to my Resister class object SetTolerance but what does the ('5') mean
not quite sure what your line two is. I understand it is a pointer to my Resister class object SetTolerance but what does the ('5') mean
There are no pointers. I declared a resistor object R and then called its SetTolerance() function.
the ('5') was one of the options that were specified as being valid in the code here:
1 2 3 4 5 6 7
if (R_Newtolerance== '1' || R_Newtolerance== '2'|| R_Newtolerance== '5' || R_Newtolerance== '10')
{
R_tolerance=R_Newtolerance;
cout<<" Your New Tolerance is :"<<R_tolerance<<"%";
returntrue;
}
When i compiled that code it gave me a warning message about in particular, this part: R_Newtolerance== '10'
In any event, the issue with that code in general is that a value of type double is being compared with a character literal. It is as out of place as for example if (3.14 == 'A') where a letter of the alphabet is compared with a floating-point number.