why can't I assign the value of one variable to another? [duplicate]Scope of variables in if statementsAre parentheses around the result significant in a return statement?What are some uses of decltype(auto)?What are the differences between a pointer variable and a reference variable in C++?Why can't variables be declared in a switch statement?Why can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhy is it faster to process a sorted array than an unsorted array?Can code that is valid in both C and C++ produce different behavior when compiled in each language?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations

Offered a new position but unknown about salary?

Can I say: "When was your train leaving?" if the train leaves in the future?

Wireless headphones interfere with Wi-Fi signal on laptop

Did galley captains put corks in the mouths of slave rowers to keep them quiet?

is it correct to say "When it started to rain, I was in the open air."

Is Valonqar prophecy unfulfilled?

Why can't I share a one use code with anyone else?

Is there any good reason to write "it is easy to see"?

Why weren't the bells paid heed to in S8E5?

Extract the characters before last colon

Why did the metro bus stop at each railway crossing, despite no warning indicating a train was coming?

Do not cross the line!

Promotion comes with unexpected 24/7/365 on-call

How to cope with regret and shame about not fully utilizing opportunities during PhD?

Why did the soldiers of the North disobey Jon?

Is there an academic word that means "to split hairs over"?

How can I add a .pem private key fingerprint entry to known_hosts before connecting with ssh?

Why didn't the Avengers use this object earlier?

Was the dragon prowess intentionally downplayed in S08E04?

Mark command as obsolete

What information exactly does an instruction cache store?

Why does SSL Labs now consider CBC suites weak?

Help understanding this line - usage of くれる

Alias for root of a polynomial



why can't I assign the value of one variable to another? [duplicate]


Scope of variables in if statementsAre parentheses around the result significant in a return statement?What are some uses of decltype(auto)?What are the differences between a pointer variable and a reference variable in C++?Why can't variables be declared in a switch statement?Why can templates only be implemented in the header file?Why is “using namespace std” considered bad practice?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Why are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhy is it faster to process a sorted array than an unsorted array?Can code that is valid in both C and C++ produce different behavior when compiled in each language?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








14
















This question already has an answer here:



  • Scope of variables in if statements

    4 answers



I have two variables and I want to work with the bigger and the smaller one differently.



My approach:



a = 1;
b = 2;

if (a >= b)
int c = a;
int d = b;

else
int c = b;
int d = a;



I obtained an error of unused variable and when I try to use c and d later, it says




c could not be resolved




Is there a way to solve this?










share|improve this question















marked as duplicate by πάντα ῥεῖ, S.M., jpmc26, ComFreek, Dukeling May 3 at 15:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • en.cppreference.com/w/cpp/language/scope This provides some good guidance on how where you declare stuff in general affects where/how you can then use it at later points in your program

    – abcalphabet
    May 3 at 14:22







  • 3





    Duplicate of Scope of variables in if statements

    – jpmc26
    May 3 at 15:24


















14
















This question already has an answer here:



  • Scope of variables in if statements

    4 answers



I have two variables and I want to work with the bigger and the smaller one differently.



My approach:



a = 1;
b = 2;

if (a >= b)
int c = a;
int d = b;

else
int c = b;
int d = a;



I obtained an error of unused variable and when I try to use c and d later, it says




c could not be resolved




Is there a way to solve this?










share|improve this question















marked as duplicate by πάντα ῥεῖ, S.M., jpmc26, ComFreek, Dukeling May 3 at 15:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















  • en.cppreference.com/w/cpp/language/scope This provides some good guidance on how where you declare stuff in general affects where/how you can then use it at later points in your program

    – abcalphabet
    May 3 at 14:22







  • 3





    Duplicate of Scope of variables in if statements

    – jpmc26
    May 3 at 15:24














14












14








14


1







This question already has an answer here:



  • Scope of variables in if statements

    4 answers



I have two variables and I want to work with the bigger and the smaller one differently.



My approach:



a = 1;
b = 2;

if (a >= b)
int c = a;
int d = b;

else
int c = b;
int d = a;



I obtained an error of unused variable and when I try to use c and d later, it says




c could not be resolved




Is there a way to solve this?










share|improve this question

















This question already has an answer here:



  • Scope of variables in if statements

    4 answers



I have two variables and I want to work with the bigger and the smaller one differently.



My approach:



a = 1;
b = 2;

if (a >= b)
int c = a;
int d = b;

else
int c = b;
int d = a;



I obtained an error of unused variable and when I try to use c and d later, it says




c could not be resolved




Is there a way to solve this?





This question already has an answer here:



  • Scope of variables in if statements

    4 answers







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 3 at 13:18









Stack Danny

2,173932




2,173932










asked May 3 at 12:48









Laura PinguichaLaura Pinguicha

895




895




marked as duplicate by πάντα ῥεῖ, S.M., jpmc26, ComFreek, Dukeling May 3 at 15:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by πάντα ῥεῖ, S.M., jpmc26, ComFreek, Dukeling May 3 at 15:47


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • en.cppreference.com/w/cpp/language/scope This provides some good guidance on how where you declare stuff in general affects where/how you can then use it at later points in your program

    – abcalphabet
    May 3 at 14:22







  • 3





    Duplicate of Scope of variables in if statements

    – jpmc26
    May 3 at 15:24


















  • en.cppreference.com/w/cpp/language/scope This provides some good guidance on how where you declare stuff in general affects where/how you can then use it at later points in your program

    – abcalphabet
    May 3 at 14:22







  • 3





    Duplicate of Scope of variables in if statements

    – jpmc26
    May 3 at 15:24

















en.cppreference.com/w/cpp/language/scope This provides some good guidance on how where you declare stuff in general affects where/how you can then use it at later points in your program

– abcalphabet
May 3 at 14:22






en.cppreference.com/w/cpp/language/scope This provides some good guidance on how where you declare stuff in general affects where/how you can then use it at later points in your program

– abcalphabet
May 3 at 14:22





3




3





Duplicate of Scope of variables in if statements

– jpmc26
May 3 at 15:24






Duplicate of Scope of variables in if statements

– jpmc26
May 3 at 15:24













4 Answers
4






active

oldest

votes


















29














In both cases c and d are scoped to the braces in the if else block, so you can't access them outside that.



You need something of the form



int c, d;
if (a >= b)
c = a;
d = b;
else
c = b;
d = a;






share|improve this answer
































    21














    As other have pointed out the issue here is where you declare the variables. You cannot use them outside of the scope they are declared in so you get an error.



    If you can use C++17 then you can fix the code by using std::minmax and a structured binding like



    int main()

    int a = 5, b = 10;
    auto [min, max] = std::minmax(b, a);
    std::cout << min << " " << max;



    which is really nice because now you don't have variables that are uninitialized for any amount of time.






    share|improve this answer


















    • 2





      It's really really nice.

      – Bathsheba
      May 3 at 13:07






    • 2





      @Bathsheba Yep. I love it because it very clearly expresses what you are trying to do. From a maintainability standpoint, that's gold.

      – NathanOliver
      May 3 at 13:09


















    6














    It's because you are declaring the variables in an if statement.



    Logically you may be of the belief that the else in a way guarantees that the variables will be assigned if the declaration is in both the if and also in the else block.



    The correct way to do it is to just declare the variables before the if block, otherwise the variables use will be restricted to the scope from which it was declared.



    Also, you can do this without the need for an if and else by using ternary operations:



     int a = 1;
    int b = 2;

    int c = a >= b ? a : b;
    int d = b < a ? b : a;


    With this type of syntax, you can save yourself the hassle of writing if and else blocks for simple variable assignments. The variable after the ? is the result if the condition is true, and the variable after the : is the result if the condition is false.






    share|improve this answer




















    • 1





      The problem is not setting variables in an if statement, the problem is declaring them in there.

      – Angew
      May 3 at 12:52






    • 1





      I didn't downvoted, but this is certainly because of the lack of explanations

      – Cid
      May 3 at 12:52






    • 2





      It has nothing to do where the variables are "set". It is where where they are declared. This answer really need some fleshing out.

      – NathanOliver
      May 3 at 12:52







    • 1





      @Angew understood. Of course this is what I meant. I will edit.

      – Elroy Jetson
      May 3 at 12:53






    • 1





      The problem with the ternary approach is that you essentially repeat the condition check.

      – Bathsheba
      May 3 at 13:09


















    4














    That's a scope problem, you are creating the variables inside a scope and they can't be accessed outside



    if (a >= b)
    int c = a; // c and d belongs to that if scope
    int d = b;

    else
    int c = b; // c and d belongs to that else scope
    int d = a;



    Change your code to this :



    a = 1;
    b = 2;
    int c;
    int d;

    if (a >= b)
    c = a;
    d = b;

    else
    c = b;
    d = a;

    // You can now call c and d there



    A way to shorten that code would be to store the boolean value of a >= b and use it in a ternary expression to set c and d



    In example :



    a = 1;
    b = 2;
    bool IsAGreaterOrEqualToB = (a >= b);
    int c = ((IsAGreaterOrEqualToB) ? (a) : (b));
    int d = ((IsAGreaterOrEqualToB) ? (b) : (a));





    share|improve this answer




















    • 2





      You've got quite a few brackets there. How come? Also I'd add in some const but otherwise yes this is good. Prefer init over assignment!

      – Lightness Races in Orbit
      May 3 at 13:02











    • @LightnessRacesinOrbit this is an old habit I have. Like when I return something, I am used to write : return (MyVar);

      – Cid
      May 3 at 13:04






    • 2





      Okay, but why is that? It just adds noise! ? (a) : (b) is harder to read than ? a : b :(

      – Lightness Races in Orbit
      May 3 at 13:05











    • @Cid: That is occasionally harmful in C++ by the way.

      – Bathsheba
      May 3 at 13:06











    • @LightnessRacesinOrbit that's just an habit, it helped me to separate the differents parts (especially when i wrote some nested ternary) Feel free to edit my answer if you think this may be more readable, answers are made for others

      – Cid
      May 3 at 13:10

















    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    29














    In both cases c and d are scoped to the braces in the if else block, so you can't access them outside that.



    You need something of the form



    int c, d;
    if (a >= b)
    c = a;
    d = b;
    else
    c = b;
    d = a;






    share|improve this answer





























      29














      In both cases c and d are scoped to the braces in the if else block, so you can't access them outside that.



      You need something of the form



      int c, d;
      if (a >= b)
      c = a;
      d = b;
      else
      c = b;
      d = a;






      share|improve this answer



























        29












        29








        29







        In both cases c and d are scoped to the braces in the if else block, so you can't access them outside that.



        You need something of the form



        int c, d;
        if (a >= b)
        c = a;
        d = b;
        else
        c = b;
        d = a;






        share|improve this answer















        In both cases c and d are scoped to the braces in the if else block, so you can't access them outside that.



        You need something of the form



        int c, d;
        if (a >= b)
        c = a;
        d = b;
        else
        c = b;
        d = a;







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 3 at 13:04

























        answered May 3 at 12:49









        BathshebaBathsheba

        184k27261388




        184k27261388























            21














            As other have pointed out the issue here is where you declare the variables. You cannot use them outside of the scope they are declared in so you get an error.



            If you can use C++17 then you can fix the code by using std::minmax and a structured binding like



            int main()

            int a = 5, b = 10;
            auto [min, max] = std::minmax(b, a);
            std::cout << min << " " << max;



            which is really nice because now you don't have variables that are uninitialized for any amount of time.






            share|improve this answer


















            • 2





              It's really really nice.

              – Bathsheba
              May 3 at 13:07






            • 2





              @Bathsheba Yep. I love it because it very clearly expresses what you are trying to do. From a maintainability standpoint, that's gold.

              – NathanOliver
              May 3 at 13:09















            21














            As other have pointed out the issue here is where you declare the variables. You cannot use them outside of the scope they are declared in so you get an error.



            If you can use C++17 then you can fix the code by using std::minmax and a structured binding like



            int main()

            int a = 5, b = 10;
            auto [min, max] = std::minmax(b, a);
            std::cout << min << " " << max;



            which is really nice because now you don't have variables that are uninitialized for any amount of time.






            share|improve this answer


















            • 2





              It's really really nice.

              – Bathsheba
              May 3 at 13:07






            • 2





              @Bathsheba Yep. I love it because it very clearly expresses what you are trying to do. From a maintainability standpoint, that's gold.

              – NathanOliver
              May 3 at 13:09













            21












            21








            21







            As other have pointed out the issue here is where you declare the variables. You cannot use them outside of the scope they are declared in so you get an error.



            If you can use C++17 then you can fix the code by using std::minmax and a structured binding like



            int main()

            int a = 5, b = 10;
            auto [min, max] = std::minmax(b, a);
            std::cout << min << " " << max;



            which is really nice because now you don't have variables that are uninitialized for any amount of time.






            share|improve this answer













            As other have pointed out the issue here is where you declare the variables. You cannot use them outside of the scope they are declared in so you get an error.



            If you can use C++17 then you can fix the code by using std::minmax and a structured binding like



            int main()

            int a = 5, b = 10;
            auto [min, max] = std::minmax(b, a);
            std::cout << min << " " << max;



            which is really nice because now you don't have variables that are uninitialized for any amount of time.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered May 3 at 12:59









            NathanOliverNathanOliver

            102k17145227




            102k17145227







            • 2





              It's really really nice.

              – Bathsheba
              May 3 at 13:07






            • 2





              @Bathsheba Yep. I love it because it very clearly expresses what you are trying to do. From a maintainability standpoint, that's gold.

              – NathanOliver
              May 3 at 13:09












            • 2





              It's really really nice.

              – Bathsheba
              May 3 at 13:07






            • 2





              @Bathsheba Yep. I love it because it very clearly expresses what you are trying to do. From a maintainability standpoint, that's gold.

              – NathanOliver
              May 3 at 13:09







            2




            2





            It's really really nice.

            – Bathsheba
            May 3 at 13:07





            It's really really nice.

            – Bathsheba
            May 3 at 13:07




            2




            2





            @Bathsheba Yep. I love it because it very clearly expresses what you are trying to do. From a maintainability standpoint, that's gold.

            – NathanOliver
            May 3 at 13:09





            @Bathsheba Yep. I love it because it very clearly expresses what you are trying to do. From a maintainability standpoint, that's gold.

            – NathanOliver
            May 3 at 13:09











            6














            It's because you are declaring the variables in an if statement.



            Logically you may be of the belief that the else in a way guarantees that the variables will be assigned if the declaration is in both the if and also in the else block.



            The correct way to do it is to just declare the variables before the if block, otherwise the variables use will be restricted to the scope from which it was declared.



            Also, you can do this without the need for an if and else by using ternary operations:



             int a = 1;
            int b = 2;

            int c = a >= b ? a : b;
            int d = b < a ? b : a;


            With this type of syntax, you can save yourself the hassle of writing if and else blocks for simple variable assignments. The variable after the ? is the result if the condition is true, and the variable after the : is the result if the condition is false.






            share|improve this answer




















            • 1





              The problem is not setting variables in an if statement, the problem is declaring them in there.

              – Angew
              May 3 at 12:52






            • 1





              I didn't downvoted, but this is certainly because of the lack of explanations

              – Cid
              May 3 at 12:52






            • 2





              It has nothing to do where the variables are "set". It is where where they are declared. This answer really need some fleshing out.

              – NathanOliver
              May 3 at 12:52







            • 1





              @Angew understood. Of course this is what I meant. I will edit.

              – Elroy Jetson
              May 3 at 12:53






            • 1





              The problem with the ternary approach is that you essentially repeat the condition check.

              – Bathsheba
              May 3 at 13:09















            6














            It's because you are declaring the variables in an if statement.



            Logically you may be of the belief that the else in a way guarantees that the variables will be assigned if the declaration is in both the if and also in the else block.



            The correct way to do it is to just declare the variables before the if block, otherwise the variables use will be restricted to the scope from which it was declared.



            Also, you can do this without the need for an if and else by using ternary operations:



             int a = 1;
            int b = 2;

            int c = a >= b ? a : b;
            int d = b < a ? b : a;


            With this type of syntax, you can save yourself the hassle of writing if and else blocks for simple variable assignments. The variable after the ? is the result if the condition is true, and the variable after the : is the result if the condition is false.






            share|improve this answer




















            • 1





              The problem is not setting variables in an if statement, the problem is declaring them in there.

              – Angew
              May 3 at 12:52






            • 1





              I didn't downvoted, but this is certainly because of the lack of explanations

              – Cid
              May 3 at 12:52






            • 2





              It has nothing to do where the variables are "set". It is where where they are declared. This answer really need some fleshing out.

              – NathanOliver
              May 3 at 12:52







            • 1





              @Angew understood. Of course this is what I meant. I will edit.

              – Elroy Jetson
              May 3 at 12:53






            • 1





              The problem with the ternary approach is that you essentially repeat the condition check.

              – Bathsheba
              May 3 at 13:09













            6












            6








            6







            It's because you are declaring the variables in an if statement.



            Logically you may be of the belief that the else in a way guarantees that the variables will be assigned if the declaration is in both the if and also in the else block.



            The correct way to do it is to just declare the variables before the if block, otherwise the variables use will be restricted to the scope from which it was declared.



            Also, you can do this without the need for an if and else by using ternary operations:



             int a = 1;
            int b = 2;

            int c = a >= b ? a : b;
            int d = b < a ? b : a;


            With this type of syntax, you can save yourself the hassle of writing if and else blocks for simple variable assignments. The variable after the ? is the result if the condition is true, and the variable after the : is the result if the condition is false.






            share|improve this answer















            It's because you are declaring the variables in an if statement.



            Logically you may be of the belief that the else in a way guarantees that the variables will be assigned if the declaration is in both the if and also in the else block.



            The correct way to do it is to just declare the variables before the if block, otherwise the variables use will be restricted to the scope from which it was declared.



            Also, you can do this without the need for an if and else by using ternary operations:



             int a = 1;
            int b = 2;

            int c = a >= b ? a : b;
            int d = b < a ? b : a;


            With this type of syntax, you can save yourself the hassle of writing if and else blocks for simple variable assignments. The variable after the ? is the result if the condition is true, and the variable after the : is the result if the condition is false.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 3 at 13:32

























            answered May 3 at 12:49









            Elroy JetsonElroy Jetson

            662517




            662517







            • 1





              The problem is not setting variables in an if statement, the problem is declaring them in there.

              – Angew
              May 3 at 12:52






            • 1





              I didn't downvoted, but this is certainly because of the lack of explanations

              – Cid
              May 3 at 12:52






            • 2





              It has nothing to do where the variables are "set". It is where where they are declared. This answer really need some fleshing out.

              – NathanOliver
              May 3 at 12:52







            • 1





              @Angew understood. Of course this is what I meant. I will edit.

              – Elroy Jetson
              May 3 at 12:53






            • 1





              The problem with the ternary approach is that you essentially repeat the condition check.

              – Bathsheba
              May 3 at 13:09












            • 1





              The problem is not setting variables in an if statement, the problem is declaring them in there.

              – Angew
              May 3 at 12:52






            • 1





              I didn't downvoted, but this is certainly because of the lack of explanations

              – Cid
              May 3 at 12:52






            • 2





              It has nothing to do where the variables are "set". It is where where they are declared. This answer really need some fleshing out.

              – NathanOliver
              May 3 at 12:52







            • 1





              @Angew understood. Of course this is what I meant. I will edit.

              – Elroy Jetson
              May 3 at 12:53






            • 1





              The problem with the ternary approach is that you essentially repeat the condition check.

              – Bathsheba
              May 3 at 13:09







            1




            1





            The problem is not setting variables in an if statement, the problem is declaring them in there.

            – Angew
            May 3 at 12:52





            The problem is not setting variables in an if statement, the problem is declaring them in there.

            – Angew
            May 3 at 12:52




            1




            1





            I didn't downvoted, but this is certainly because of the lack of explanations

            – Cid
            May 3 at 12:52





            I didn't downvoted, but this is certainly because of the lack of explanations

            – Cid
            May 3 at 12:52




            2




            2





            It has nothing to do where the variables are "set". It is where where they are declared. This answer really need some fleshing out.

            – NathanOliver
            May 3 at 12:52






            It has nothing to do where the variables are "set". It is where where they are declared. This answer really need some fleshing out.

            – NathanOliver
            May 3 at 12:52





            1




            1





            @Angew understood. Of course this is what I meant. I will edit.

            – Elroy Jetson
            May 3 at 12:53





            @Angew understood. Of course this is what I meant. I will edit.

            – Elroy Jetson
            May 3 at 12:53




            1




            1





            The problem with the ternary approach is that you essentially repeat the condition check.

            – Bathsheba
            May 3 at 13:09





            The problem with the ternary approach is that you essentially repeat the condition check.

            – Bathsheba
            May 3 at 13:09











            4














            That's a scope problem, you are creating the variables inside a scope and they can't be accessed outside



            if (a >= b)
            int c = a; // c and d belongs to that if scope
            int d = b;

            else
            int c = b; // c and d belongs to that else scope
            int d = a;



            Change your code to this :



            a = 1;
            b = 2;
            int c;
            int d;

            if (a >= b)
            c = a;
            d = b;

            else
            c = b;
            d = a;

            // You can now call c and d there



            A way to shorten that code would be to store the boolean value of a >= b and use it in a ternary expression to set c and d



            In example :



            a = 1;
            b = 2;
            bool IsAGreaterOrEqualToB = (a >= b);
            int c = ((IsAGreaterOrEqualToB) ? (a) : (b));
            int d = ((IsAGreaterOrEqualToB) ? (b) : (a));





            share|improve this answer




















            • 2





              You've got quite a few brackets there. How come? Also I'd add in some const but otherwise yes this is good. Prefer init over assignment!

              – Lightness Races in Orbit
              May 3 at 13:02











            • @LightnessRacesinOrbit this is an old habit I have. Like when I return something, I am used to write : return (MyVar);

              – Cid
              May 3 at 13:04






            • 2





              Okay, but why is that? It just adds noise! ? (a) : (b) is harder to read than ? a : b :(

              – Lightness Races in Orbit
              May 3 at 13:05











            • @Cid: That is occasionally harmful in C++ by the way.

              – Bathsheba
              May 3 at 13:06











            • @LightnessRacesinOrbit that's just an habit, it helped me to separate the differents parts (especially when i wrote some nested ternary) Feel free to edit my answer if you think this may be more readable, answers are made for others

              – Cid
              May 3 at 13:10















            4














            That's a scope problem, you are creating the variables inside a scope and they can't be accessed outside



            if (a >= b)
            int c = a; // c and d belongs to that if scope
            int d = b;

            else
            int c = b; // c and d belongs to that else scope
            int d = a;



            Change your code to this :



            a = 1;
            b = 2;
            int c;
            int d;

            if (a >= b)
            c = a;
            d = b;

            else
            c = b;
            d = a;

            // You can now call c and d there



            A way to shorten that code would be to store the boolean value of a >= b and use it in a ternary expression to set c and d



            In example :



            a = 1;
            b = 2;
            bool IsAGreaterOrEqualToB = (a >= b);
            int c = ((IsAGreaterOrEqualToB) ? (a) : (b));
            int d = ((IsAGreaterOrEqualToB) ? (b) : (a));





            share|improve this answer




















            • 2





              You've got quite a few brackets there. How come? Also I'd add in some const but otherwise yes this is good. Prefer init over assignment!

              – Lightness Races in Orbit
              May 3 at 13:02











            • @LightnessRacesinOrbit this is an old habit I have. Like when I return something, I am used to write : return (MyVar);

              – Cid
              May 3 at 13:04






            • 2





              Okay, but why is that? It just adds noise! ? (a) : (b) is harder to read than ? a : b :(

              – Lightness Races in Orbit
              May 3 at 13:05











            • @Cid: That is occasionally harmful in C++ by the way.

              – Bathsheba
              May 3 at 13:06











            • @LightnessRacesinOrbit that's just an habit, it helped me to separate the differents parts (especially when i wrote some nested ternary) Feel free to edit my answer if you think this may be more readable, answers are made for others

              – Cid
              May 3 at 13:10













            4












            4








            4







            That's a scope problem, you are creating the variables inside a scope and they can't be accessed outside



            if (a >= b)
            int c = a; // c and d belongs to that if scope
            int d = b;

            else
            int c = b; // c and d belongs to that else scope
            int d = a;



            Change your code to this :



            a = 1;
            b = 2;
            int c;
            int d;

            if (a >= b)
            c = a;
            d = b;

            else
            c = b;
            d = a;

            // You can now call c and d there



            A way to shorten that code would be to store the boolean value of a >= b and use it in a ternary expression to set c and d



            In example :



            a = 1;
            b = 2;
            bool IsAGreaterOrEqualToB = (a >= b);
            int c = ((IsAGreaterOrEqualToB) ? (a) : (b));
            int d = ((IsAGreaterOrEqualToB) ? (b) : (a));





            share|improve this answer















            That's a scope problem, you are creating the variables inside a scope and they can't be accessed outside



            if (a >= b)
            int c = a; // c and d belongs to that if scope
            int d = b;

            else
            int c = b; // c and d belongs to that else scope
            int d = a;



            Change your code to this :



            a = 1;
            b = 2;
            int c;
            int d;

            if (a >= b)
            c = a;
            d = b;

            else
            c = b;
            d = a;

            // You can now call c and d there



            A way to shorten that code would be to store the boolean value of a >= b and use it in a ternary expression to set c and d



            In example :



            a = 1;
            b = 2;
            bool IsAGreaterOrEqualToB = (a >= b);
            int c = ((IsAGreaterOrEqualToB) ? (a) : (b));
            int d = ((IsAGreaterOrEqualToB) ? (b) : (a));






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 3 at 12:58

























            answered May 3 at 12:51









            CidCid

            6,07421229




            6,07421229







            • 2





              You've got quite a few brackets there. How come? Also I'd add in some const but otherwise yes this is good. Prefer init over assignment!

              – Lightness Races in Orbit
              May 3 at 13:02











            • @LightnessRacesinOrbit this is an old habit I have. Like when I return something, I am used to write : return (MyVar);

              – Cid
              May 3 at 13:04






            • 2





              Okay, but why is that? It just adds noise! ? (a) : (b) is harder to read than ? a : b :(

              – Lightness Races in Orbit
              May 3 at 13:05











            • @Cid: That is occasionally harmful in C++ by the way.

              – Bathsheba
              May 3 at 13:06











            • @LightnessRacesinOrbit that's just an habit, it helped me to separate the differents parts (especially when i wrote some nested ternary) Feel free to edit my answer if you think this may be more readable, answers are made for others

              – Cid
              May 3 at 13:10












            • 2





              You've got quite a few brackets there. How come? Also I'd add in some const but otherwise yes this is good. Prefer init over assignment!

              – Lightness Races in Orbit
              May 3 at 13:02











            • @LightnessRacesinOrbit this is an old habit I have. Like when I return something, I am used to write : return (MyVar);

              – Cid
              May 3 at 13:04






            • 2





              Okay, but why is that? It just adds noise! ? (a) : (b) is harder to read than ? a : b :(

              – Lightness Races in Orbit
              May 3 at 13:05











            • @Cid: That is occasionally harmful in C++ by the way.

              – Bathsheba
              May 3 at 13:06











            • @LightnessRacesinOrbit that's just an habit, it helped me to separate the differents parts (especially when i wrote some nested ternary) Feel free to edit my answer if you think this may be more readable, answers are made for others

              – Cid
              May 3 at 13:10







            2




            2





            You've got quite a few brackets there. How come? Also I'd add in some const but otherwise yes this is good. Prefer init over assignment!

            – Lightness Races in Orbit
            May 3 at 13:02





            You've got quite a few brackets there. How come? Also I'd add in some const but otherwise yes this is good. Prefer init over assignment!

            – Lightness Races in Orbit
            May 3 at 13:02













            @LightnessRacesinOrbit this is an old habit I have. Like when I return something, I am used to write : return (MyVar);

            – Cid
            May 3 at 13:04





            @LightnessRacesinOrbit this is an old habit I have. Like when I return something, I am used to write : return (MyVar);

            – Cid
            May 3 at 13:04




            2




            2





            Okay, but why is that? It just adds noise! ? (a) : (b) is harder to read than ? a : b :(

            – Lightness Races in Orbit
            May 3 at 13:05





            Okay, but why is that? It just adds noise! ? (a) : (b) is harder to read than ? a : b :(

            – Lightness Races in Orbit
            May 3 at 13:05













            @Cid: That is occasionally harmful in C++ by the way.

            – Bathsheba
            May 3 at 13:06





            @Cid: That is occasionally harmful in C++ by the way.

            – Bathsheba
            May 3 at 13:06













            @LightnessRacesinOrbit that's just an habit, it helped me to separate the differents parts (especially when i wrote some nested ternary) Feel free to edit my answer if you think this may be more readable, answers are made for others

            – Cid
            May 3 at 13:10





            @LightnessRacesinOrbit that's just an habit, it helped me to separate the differents parts (especially when i wrote some nested ternary) Feel free to edit my answer if you think this may be more readable, answers are made for others

            – Cid
            May 3 at 13:10



            Popular posts from this blog

            Club Baloncesto Breogán Índice Historia | Pavillón | Nome | O Breogán na cultura popular | Xogadores | Adestradores | Presidentes | Palmarés | Historial | Líderes | Notas | Véxase tamén | Menú de navegacióncbbreogan.galCadroGuía oficial da ACB 2009-10, páxina 201Guía oficial ACB 1992, páxina 183. Editorial DB.É de 6.500 espectadores sentados axeitándose á última normativa"Estudiantes Junior, entre as mellores canteiras"o orixinalHemeroteca El Mundo Deportivo, 16 setembro de 1970, páxina 12Historia do BreogánAlfredo Pérez, o último canoneiroHistoria C.B. BreogánHemeroteca de El Mundo DeportivoJimmy Wright, norteamericano do Breogán deixará Lugo por ameazas de morteResultados de Breogán en 1986-87Resultados de Breogán en 1990-91Ficha de Velimir Perasović en acb.comResultados de Breogán en 1994-95Breogán arrasa al Barça. "El Mundo Deportivo", 27 de setembro de 1999, páxina 58CB Breogán - FC BarcelonaA FEB invita a participar nunha nova Liga EuropeaCharlie Bell na prensa estatalMáximos anotadores 2005Tempada 2005-06 : Tódolos Xogadores da Xornada""Non quero pensar nunha man negra, mais pregúntome que está a pasar""o orixinalRaúl López, orgulloso dos xogadores, presume da boa saúde económica do BreogánJulio González confirma que cesa como presidente del BreogánHomenaxe a Lisardo GómezA tempada do rexurdimento celesteEntrevista a Lisardo GómezEl COB dinamita el Pazo para forzar el quinto (69-73)Cafés Candelas, patrocinador del CB Breogán"Suso Lázare, novo presidente do Breogán"o orixinalCafés Candelas Breogán firma el mayor triunfo de la historiaEl Breogán realizará 17 homenajes por su cincuenta aniversario"O Breogán honra ao seu fundador e primeiro presidente"o orixinalMiguel Giao recibiu a homenaxe do PazoHomenaxe aos primeiros gladiadores celestesO home que nos amosa como ver o Breo co corazónTita Franco será homenaxeada polos #50anosdeBreoJulio Vila recibirá unha homenaxe in memoriam polos #50anosdeBreo"O Breogán homenaxeará aos seus aboados máis veteráns"Pechada ovación a «Capi» Sanmartín e Ricardo «Corazón de González»Homenaxe por décadas de informaciónPaco García volve ao Pazo con motivo do 50 aniversario"Resultados y clasificaciones""O Cafés Candelas Breogán, campión da Copa Princesa""O Cafés Candelas Breogán, equipo ACB"C.B. Breogán"Proxecto social"o orixinal"Centros asociados"o orixinalFicha en imdb.comMario Camus trata la recuperación del amor en 'La vieja música', su última película"Páxina web oficial""Club Baloncesto Breogán""C. B. Breogán S.A.D."eehttp://www.fegaba.com

            Vilaño, A Laracha Índice Patrimonio | Lugares e parroquias | Véxase tamén | Menú de navegación43°14′52″N 8°36′03″O / 43.24775, -8.60070

            Cegueira Índice Epidemioloxía | Deficiencia visual | Tipos de cegueira | Principais causas de cegueira | Tratamento | Técnicas de adaptación e axudas | Vida dos cegos | Primeiros auxilios | Crenzas respecto das persoas cegas | Crenzas das persoas cegas | O neno deficiente visual | Aspectos psicolóxicos da cegueira | Notas | Véxase tamén | Menú de navegación54.054.154.436928256blindnessDicionario da Real Academia GalegaPortal das Palabras"International Standards: Visual Standards — Aspects and Ranges of Vision Loss with Emphasis on Population Surveys.""Visual impairment and blindness""Presentan un plan para previr a cegueira"o orixinalACCDV Associació Catalana de Cecs i Disminuïts Visuals - PMFTrachoma"Effect of gene therapy on visual function in Leber's congenital amaurosis"1844137110.1056/NEJMoa0802268Cans guía - os mellores amigos dos cegosArquivadoEscola de cans guía para cegos en Mortágua, PortugalArquivado"Tecnología para ciegos y deficientes visuales. Recopilación de recursos gratuitos en la Red""Colorino""‘COL.diesis’, escuchar los sonidos del color""COL.diesis: Transforming Colour into Melody and Implementing the Result in a Colour Sensor Device"o orixinal"Sistema de desarrollo de sinestesia color-sonido para invidentes utilizando un protocolo de audio""Enseñanza táctil - geometría y color. Juegos didácticos para niños ciegos y videntes""Sistema Constanz"L'ocupació laboral dels cecs a l'Estat espanyol està pràcticament equiparada a la de les persones amb visió, entrevista amb Pedro ZuritaONCE (Organización Nacional de Cegos de España)Prevención da cegueiraDescrición de deficiencias visuais (Disc@pnet)Braillín, un boneco atractivo para calquera neno, con ou sen discapacidade, que permite familiarizarse co sistema de escritura e lectura brailleAxudas Técnicas36838ID00897494007150-90057129528256DOID:1432HP:0000618D001766C10.597.751.941.162C97109C0155020