Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exercise 5.7_c possible error. #762

Open
Overload86 opened this issue Jul 12, 2020 · 2 comments
Open

Exercise 5.7_c possible error. #762

Overload86 opened this issue Jul 12, 2020 · 2 comments

Comments

@Overload86
Copy link

if (int ival = get_value())
    cout << "ival = " << ival << endl;
if (!ival)     
    cout << "ival = 0\n";

What you wrote is probably also true, but the more glaring error to me is, that ival is defined inside the "if" condition. After the first if ends with 'cout'ing the string, the variable will lose it's scope and the second if cannot evaluate it's condition, or am I mistaken? (Same if you change it to an else/if.

So the correct code should be:

int ival = get_value();
if (ival)
   cout << "ival = " << ival << endl;
else
   cout << "ival = 0" << endl;
@emmanuelmoon
Copy link
Contributor

Yeah, "ival" doesn't register in the second if as it is out of scope.

@Muhammad-Murtaazaa
Copy link

ig the correct code should be

int ival = get_value();
if(ival){
cout<<"ival= "<<ival<<endl;
}
else{
cout<<"ival= '<<ival<<endl;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants