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

            Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

            Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

            What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company