initialized value to new value

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....
Do you mean operation like this?
1
2
3
4
5
6
7
8
9
class MyClass
{
    public:
    int property;
    MyClass(int value)
    {
        property = value;
    }
};


In main you use it like this:
1
2
MyClass myObject(3); // initialize in contructor
myObject.property = 4; // change afterwards 
Last edited on
No I mean like this
1
2
3
4
5
6
Resistor:: Resistor (void)
{
	R_nominalValue=1000; ///// set value
	R_tolerance= .20;
	
}

Within class to change it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool Resistor:: setResistance(double R_NewNomValue)
{
	if (R_NewNomValue<0||R_NewNomValue>1000000)
	{
		cout<< "Invalid imput, please try again";
		return false;
	}
	else
	{
		R_NewNomValue>=0||R_NewNomValue<=1000000;

		cout << "Your new Resistance is:" << R_NewNomValue<<"Ohms";
		cout<< endl;
		return true;
	}
	R_nominalValue= R_NewNomValue; ////// will this change it??
}


The reason I ask is my visual studio is acting up and I can get it to run on an online compiler but it doesn't let me do any cin operations....

Studio is giving me a Cannot find or open the PDB file.... but it runs online?
Any way not my problem right now....
Line 10 is not needed:
R_NewNomValue>=0||R_NewNomValue<=1000000;

return true; 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.
Last edited on
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....
Sorry, you lost me there. I don't know whether you misunderstood me, or the other way around. Anyway, this is what I meant:
1
2
3
4
5
6
7
8
9
10
11
12
bool Resistor::setResistance(double R_NewNomValue)
{
    if (R_NewNomValue<0||R_NewNomValue>1000000)
    {
        cout<< "Invalid input, please try again";
        return false;
    }
    
    R_nominalValue = R_NewNomValue;   
    cout << "Your new Resistance is:" << R_nominalValue << "Ohms" << endl;
    return true;
}
You are such a life save, I totally misunderstood you, Also thank you very much.... But now that you have stated that do I have this worded right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

bool Resistor::SetTolerance(double R_Newtolerance)
{
	if (R_Newtolerance= 1,2,5,10)
	{
		cout<<" Your New Tolerance is :"<<R_Newtolerance;
		return true;
	}
	else
	{
		cout<<"Invalid tolerance amount, Please try again";
		return false;
	}
	R_tolerance=R_Newtolerance;


Or should it be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

bool Resistor::SetTolerance(double R_Newtolerance)
{
	if (R_Newtolerance= 1,2,5,10)
	{
                              R_tolerance=R_Newtolerance;

		cout<<" Your New Tolerance is :"<<R_tolerance;
		return true;
	}
	else
	{
		cout<<"Invalid tolerance amount, Please try again";
		return false;
	}
	R
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/

See also the && and || operators on the same page
Last edited on
Okay, I'm going to try this one last time.... If I understood what it said on that site you had me read up on... It would look something like this....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
	if (R_Newtolerance== '1' || R_Newtolerance== '2'|| R_Newtolerance== '5' || R_Newtolerance== '10')
	{
		R_tolerance=R_Newtolerance;

cout<<" Your New Tolerance is :"<<R_tolerance<<"%";
		return true;
	}
	else
	{
		cout<<"Invalid tolerance amount, Please try again";
		return false;
	}
	
}
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.
Last edited on
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
Last edited on
Did you check the compiler messages?

and did you try this:
1
2
    Resistor R;
    R.SetTolerance('5');

Output:
 Your New Tolerance is :53%

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
Last edited on
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<<"%";
        return true;
    }


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.
Last edited on
Ohhh I didn't catch that. Thank you. I don't understand why I didn't get a compiler error? What are you using?
Last edited on
Well, I use a couple of different compilers, one is MinGW32 4.7.2 which gave the message:
[Warning] multi-character character constant [-Wmultichar]
because of this : '10'

That didn't actually stop it from compiling and running, as it was only a warning. However, it was a pretty good clue that something was amiss.
Topic archived. No new replies allowed.