0

I am trying to assign a variable if one variable equals another value. Here is a simplified version of my problem:

#include <iostream> using namespace std; int main() { int a = 5; int b; cout << "A is this value (before if): " << a << endl; cout << "B is this value (before if): " << b << endl; if (a==5) { cout << "A equals five" << endl; int b = 6; cout << "B is this value (if stmt): " << b << endl; } cout << "B is this value (outside): " << b << endl; return 0; } 

It outputs the following:

A is this value (before if): 5 B is this value (before if): 0 A equals five B is this value (if stmt): 6 B is this value (outside): 0 

Why does variable "b" not stay assigned as 6 once it leaves the if statement? And is there a better way to assign it? In my actual code I have five variables I compare to a.

3
  • 1
    remove int from int b = 6; - then learn about scope Commented Apr 27, 2017 at 18:57
  • Different b. The inner scope b hides the first by name. And fyi this invokes undefined behavior regardless, as the outer b is never assigned a value before evaluation during your output insertions. Commented Apr 27, 2017 at 18:58
  • This isn't the problem, but do you really need the extra stuff that std::endl does? '\n' ends a line. Commented Apr 27, 2017 at 19:19

4 Answers 4

2

You declared a new variable inside the if block. Replace the variable declaration with an assignment.

Also, you should initialize the original b variable. Printing its value without initializing it results in undefined behavior.

#include <iostream> using namespace std; int main() { int a = 5; int b = 0; cout << "A is this value (before if): " << a << endl; cout << "B is this value (before if): " << b << endl; if (a==5) { cout << "A equals five" << endl; b = 6; cout << "B is this value (if stmt): " << b << endl; } cout << "B is this value (outside): " << b << endl; return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

1

Because int b = 6; introduces a new variable b that's initialized to 6. It doesn't set 6 to the b of the outer scope. To do that, you should remove the type specifier:

b = 6; 

Right now because b is never initialized you have undefined behaviour.

Comments

0
int main() { int a = 5; int b; // this instance of b has scope over the entire main() function cout << "A is this value (before if): " << a << endl; cout << "B is this value (before if): " << b << endl; if (a==5) { cout << "A equals five" << endl; int b = 6; // this instance of b only exists inside this if statement. As soon as the program leaves this if, this instance of b stops existing cout << "B is this value (if stmt): " << b << endl; } cout << "B is this value (outside): " << b << endl; return 0; } 

1 Comment

this looks like a code only answer. Consider to add some text, as the comments can be overlooked easily
0

int b = 6 inside of the if block is bringing a scope problem, the new variable is shadowing the variable you declared in main function

you need to do:

 if (a==5) { cout << "A equals five" << endl; b = 6; cout << "B is this value (if stmt): " << b << endl; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.