| rjp52193 (6) | |
| Alright, so I'm wondering how I can check whether one variable is ASSIGNED to another variable (as in, statement is true if Var A is assigned to var B). However, I cannot simply say if(varA==varB) because in my program, varA could also be equal to varC, which has an if statement as well and would ruin everything. Any help? | |
|
|
|
| Framework (3116) | |
|
What you're asking is quite strange, because it doesn't make much sense. Can you refine your question more? Wazzak | |
|
|
|
| clanmjc (661) | |
| What are are you comparing, the values, the addresses? Please clarify, post some code. | |
|
|
|
| Disch (8337) | |||||
variables do not hold assignment information. They only hold a value.
If you want a variable that contains another variable (and not just a value), then maybe you want a pointer... but as Framework said this is very strange:
| |||||
|
|
|||||
| rjp52193 (6) | |
|
Thanks guys I'll try to post a simplified version of what I'm doing. char x; float varA=0, varB=0, varC, amount; cout<<"Enter a or b:"; cin>>x; if(x=='a') varC=varA; else if(x=='b') varC=varB; cout<<"Enter the amount to add or subtract:"; cin>>amount; *** if(varC==varA) varC+=amount; ***else if(varC==varB) varC-=amount; The lines marked with asterisks are where I'm having a problem, because both statements will be true since both A and B are set to zero initially. (The first "if" is used, the "else if" gets overridden.) Also, the value of x gets changed in other spots of my program so I can't use if(x=='a') etc. again. And I have to have both values set to zero initially. So any help? | |
|
|
|
| clanmjc (661) | |
|
Well a quick simple solution would be... instead of if(varC==varA) do if(x=='a') and the else if (x=='b') and proceed as you've done.You already have x which appears to be your "condition", reuse it.Also, I know this is a small program, but I'd suggest using meaningful names for all variables whenever you can, if not for anything other than making it habit. | |
|
Last edited on
|
|
| vlad from moscow (3108) | |
|
Introduce a logical variable and do not bother:) bool AsA = false; if(x=='a') varC=varA, AsA = true; else if(x=='b') varC=varB; if ( AsA ) varC+=amount; else varC-=amount; | |
|
Last edited on
|
|
| rjp52193 (6) | |
| My value of x gets changed in other parts of the code that I didn't include, so is there any other alternative? | |
|
|
|
| clanmjc (661) | |
| Yes, use a flag as vlad has suggested. | |
|
|
|
| rjp52193 (6) | |
| Oh I get it, thanks for your help! | |
|
|
|
| Disch (8337) | |||
Err.... I wouldn't use a flag. I would use a pointer. Going to pitch this at you again:
| |||
|
|
|||
| clanmjc (661) | |
| @Disch - I was going to suggest something similiar except for how if a or b += or -=. | |
|
|
|
| Disch (8337) | |
| Ah I missed that bit. Okay that make sense. | |
|
|
|