Group: comp.lang.c++
From: "John Brawley"
Date: Saturday, February 16, 2008 4:07 PM
Subject: Re: out of inner if?


"Ian Collins" wrote in message
news:61p33gF1ui98vU17@mid.individual.net...
> John Brawley wrote:
> > 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
> }
> 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??


--
Peace
JB
jb@tetrahedraverse.com
Web: http://tetrahedraverse.com