out of inner if?

J

John Brawley

Please advise?
I've never fully grasped scope/namespaces, but have been able to work around
earlier problems.
I now have a program flow situation in which I have *no choice* but to do
this this way.
Suppose I have something like;

var1=0.0;

main() {
/*do something sane*/
if (/*condition*/) {
var1=7.65347; //somenumber
}
//use var1's new value
}

How do I get a changed variable back out of an if statement's { } block?
I do math inside the if( ){ now, where before, the math was in the main()
function. I've now no choice: I'm skipping user entry IFF there's a file to
be read, with the same info in it that the user would have entered, so I
have to conditionalize many of the operations that otherwise were in the
main(){. )

I'm missing something simple I'm sure, and I _thought_ if I declared the
variable (initialized or not) outside of the main() function (hence global;
every loop should 'see' it no matter how nested), I could change it and it
would come back out changed, but I've had this same problem several times
now.
The 400+ line program compiles and runs, but my _info_ is gone 'cause it's
being calculated in an if( ){ instead of in the main(){ .

I'm frazzled. Please if there's a short/sweet way to spit the var back out
into the main(), let me know?
Thanks....
 
I

Ian Collins

John said:
Please advise?
I've never fully grasped scope/namespaces, but have been able to work around
earlier problems.
I now have a program flow situation in which I have *no choice* but to do
this this way.
Suppose I have something like;

var1=0.0;

main() {
/*do something sane*/
if (/*condition*/) {
var1=7.65347; //somenumber
}
//use var1's new value
}

How do I get a changed variable back out of an if statement's { } block?

Declare the variable in main, but not in the if scope.

double var1;

if (/*condition*/) {
var1=7.65347; //somenumber
}
 
P

Pavel

John said:
Please advise?
I've never fully grasped scope/namespaces, but have been able to work around
earlier problems.
I now have a program flow situation in which I have *no choice* but to do
this this way.
Suppose I have something like;

var1=0.0;

main() {
/*do something sane*/
if (/*condition*/) {
var1=7.65347; //somenumber
}
//use var1's new value
}

How do I get a changed variable back out of an if statement's { } block?
I do math inside the if( ){ now, where before, the math was in the main()
function. I've now no choice: I'm skipping user entry IFF there's a file to
be read, with the same info in it that the user would have entered, so I
have to conditionalize many of the operations that otherwise were in the
main(){. )

I'm missing something simple I'm sure, and I _thought_ if I declared the
variable (initialized or not) outside of the main() function (hence global;
every loop should 'see' it no matter how nested), I could change it and it
would come back out changed, but I've had this same problem several times
now.
The 400+ line program compiles and runs, but my _info_ is gone 'cause it's
being calculated in an if( ){ instead of in the main(){ .

I'm frazzled. Please if there's a short/sweet way to spit the var back out
into the main(), let me know?
Thanks....


It would help if you gave us a little complete example which produces
the output you do not like and tell us what output you expect.
Currently, it is not very clear what the problem is.

-Pavel
 
J

John Brawley

Ian Collins said:
Declare the variable in main, but not in the if scope.

double var1;

if (/*condition*/) {
var1=7.65347; //somenumber
}
Ian Collins.

Nope; didn't do it....
Pavel suggests the offending code....
(embarassing, but) :
(cleaned a bit....)
(beware linewraps)

//this is in main() {
//loadyn is decided earlier; the if block does activate.

if (loadyn=='n') {
cout << "Number of pionts (pNum) ? :";
cin >> pNum; //isn't the problem
double pVol=(4.0/3.0)*M_PI*(pRad*pRad*pRad);
cout << "\nInfo:\n";
cout << pVol << " is piont volume\n";
double gVol=(pNum*pVol)*1/0.64;
cout << gVol << " is total pionts volume\n";
double cgRad=pow((gVol/(4.0/3.0)/M_PI),0.333333333)-pRad;
cout << "Estimated container radius (gRad; for RJP density) = " << cgRad <<
"\n";

//more like this, a couple more user inputs, then

}

//back in main(){

The problem:
All the text prints to the screen. All the variables supposedly calculated
there, print as zero (' 0 '). They all used to print (this was working code
before I stuck it into the if( ){ .
Declaring pVol, gVol, cgRad, (others cut from this snippet) outside the if
but inside the main(){ doesn't fix it.

I can not fathom why the text prints, but the results of the variable calcs
print as zero.
(Borland command line compiler, SciTE editor, if that's significant (I can't
imagine why it would be. The code was working fine for a year in main(){ .)
Help??
 
P

Pavel

John said:
Nope; didn't do it....
Pavel suggests the offending code....
(embarassing, but) :
(cleaned a bit....)
(beware linewraps)

//this is in main() {
//loadyn is decided earlier; the if block does activate.

if (loadyn=='n') {
cout << "Number of pionts (pNum) ? :";
cin >> pNum; //isn't the problem
double pVol=(4.0/3.0)*M_PI*(pRad*pRad*pRad);
cout << "\nInfo:\n";
cout << pVol << " is piont volume\n";
double gVol=(pNum*pVol)*1/0.64;
cout << gVol << " is total pionts volume\n";
double cgRad=pow((gVol/(4.0/3.0)/M_PI),0.333333333)-pRad;
cout << "Estimated container radius (gRad; for RJP density) = " << cgRad <<
"\n";

//more like this, a couple more user inputs, then

}

//back in main(){

The problem:
All the text prints to the screen. All the variables supposedly calculated
there, print as zero (' 0 '). They all used to print (this was working code
before I stuck it into the if( ){ .
Declaring pVol, gVol, cgRad, (others cut from this snippet) outside the if
but inside the main(){ doesn't fix it.

I can not fathom why the text prints, but the results of the variable calcs
print as zero.
(Borland command line compiler, SciTE editor, if that's significant (I can't
imagine why it would be. The code was working fine for a year in main(){ .)
Help??

Make sure pRad is not initialized to zero (maybe just print pRad before
using it) -- it might not be initialized into what you think. if pRad is
zero, everything you print should be zero. It does not seem it has
anything to do with `if' but who knows..

Hope this will help

-Pavel
 
I

Ian Collins

Please learn to trim and quote properly.
Nope; didn't do it....

Then you are doing something silly that you are not telling. Post a
minimal example that compiles and shows your problem.
 
J

John Brawley

calculated there, print as zero (' 0 '). They all used to print (this was
working code before I stuck it into the if( ){ .(I can't imagine why it would be. The code was working fine for a year in
main(){ .)
Make sure pRad is not initialized to zero (maybe just print pRad
before using it) -- it might not be initialized into what you think. if pRad
zero, everything you print should be zero. It does not seem it has
anything to do with `if' but who knows..

Hope this will help
-Pavel

*AAAAaaaaarrrgh...!*
(Hadda be done; tension reliever...)
Yes, thanks, Pavel, that was exactly it: I had moved pRad to a logical
place for it (yeah, right....not very).
Everything's fine now, settling self down, taking a break, maybe some
TV.....
Thank you _very_ much.

Ian, obviously I was doing something silly, that I didn't realize I was
(hence couldn't have told), and I'll sure try to find in the FAQ what's
proper trimming and quoting.

Thank you both.
End of (unnecessary, but vital, thread...)

Peace
JB
(e-mail address removed)
Web: http://tetrahedraverse.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,160
Latest member
CollinStri
Top