out of inner if?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Brawley

    out of inner if?

    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....


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




  • Ian Collins

    #2
    Re: out of inner if?

    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.

    Comment

    • Pavel

      #3
      Re: out of inner if?

      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?
      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

      Comment

      • Ian Collins

        #4
        Re: out of inner if?

        John Brawley wrote:
        >>>
        >>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
        >}
        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.

        --
        Ian Collins.

        Comment

        Working...