Home > Back-end >  How can I define a boolean value under an if statement
How can I define a boolean value under an if statement

Time:12-02

import "dart:io";
import "dart:math";


String promptStudent(){
print("are you a student?");
String Student = stdin.readLineSync()!;
return Student;
}

String promptSmart(){
print("2 2=?");
String Smart = stdin.readLineSync()!;
return Smart;
}



void main() {
  String student = promptStudent();
  if(student == "yes"){
  bool isStudent = true;
  } else() {
  bool isStudent = false;
  };
  String smart = promptSmart();
  if(smart == "4"){
  bool isSmart = true;
  } else() {
  bool isSmart = false;
  };

  if(isSmart && isStudent) {
  print("he is a smart student");
  }
  else if(isSmart && !isStudent) {
  print("he is smart but not a student");
  }
  else if(!isSmart && isStudent) {
  print("he isnt smart but a student");
  }
  else if(!isSmart && !isStudent) {
  print("he is not smart and not a student");
  };

}

I wanted to set a boolean by user input (if answer to 2 2 is 4 he is smart etc.), but I get issues like the following:

main.dart:42:24: Error: Getter not found: 'isStudent'. else if(!isSmart && !isStudent) {

What is wrong here? How can I fix it?

CodePudding user response:

The isStudent and isSmart variables have block scope in the if-else statements you created. They are limited in scope to the braces {} that enclose them. Therefore, you cannot access them outside of those sections.

To access a variable outside of the if-else block scope, you need to declare the variable outside of it. Declare the variables outside of the if-else blocks and assign them within the if-else blocks. The code below also fixes a variety of other syntax errors including removing braces following the else statements, semicolons following if-else blocks, removing unused imports, and fixing naming conventions.

import "dart:io";

String promptStudent() {
  print("are you a student?");
  String student = stdin.readLineSync()!;
  return student;
}

String promptSmart() {
  print("2 2=?");
  String smart = stdin.readLineSync()!;
  return smart;
}

void main() {
  String student = promptStudent();
  late final bool isStudent;
  if (student == "yes") {
    isStudent = true;
  } else {
    isStudent = false;
  }
  String smart = promptSmart();
  late final bool isSmart;
  if (smart == "4") {
    isSmart = true;
  } else {
    isSmart = false;
  }

  if (isSmart && isStudent) {
    print("he is a smart student");
  } else if (isSmart && !isStudent) {
    print("he is smart but not a student");
  } else if (!isSmart && isStudent) {
    print("he isnt smart but a student");
  } else if (!isSmart && !isStudent) {
    print("he is not smart and not a student");
  }
}

You code can also be simplified in several ways, especially when it comes to assigning booleans.

This

late final bool isStudent;
if (student == "yes") {
  isStudent = true;
} else {
  isStudent = false;
}

can be simplified to this one-liner:

final bool isStudent = student == "yes";

This allows your code to become the far simpler following:

import "dart:io";

String promptStudent() {
  print("are you a student?");
  return stdin.readLineSync()!;
}

String promptSmart() {
  print("2 2=?");
  return stdin.readLineSync()!;
}

void main() {
  String student = promptStudent();
  final bool isStudent = student == "yes";
  
  String smart = promptSmart();
  final bool isSmart = smart == "4";

  if (isSmart && isStudent) {
    print("he is a smart student");
  } else if (isSmart && !isStudent) {
    print("he is smart but not a student");
  } else if (!isSmart && isStudent) {
    print("he isnt smart but a student");
  } else if (!isSmart && !isStudent) {
    print("he is not smart and not a student");
  }
}

CodePudding user response:

The code had some redoundand pices that lead probably to the error at hand avoiding the definition of some variable that you used for comparison at the and of your code, rewrithing as follows should solve it but i could not test myself since I your headers where not shared.

import "dart:io";
import "dart:math";


String promptStudent(){
print("are you a student?");
String Student = stdin.readLineSync()!;
return Student;
}

String promptSmart(){
print("2 2=?");
String Smart = stdin.readLineSync()!;
return Smart;
}



void main() {
  bool isStudent = false;
  bool isSmart = true;
  String student = promptStudent();
  if(student == "yes"){
     isStudent = true;
  }
  String smart = promptSmart();
  if(smart == "4"){
     isSmart = true;
  } 

  if(isSmart && isStudent) {
  print("he is a smart student");
  }
  else if(isSmart && !isStudent) {
  print("he is smart but not a student");
  }
  else if(!isSmart && isStudent) {
  print("he isnt smart but a student");
  }
  else if(!isSmart && !isStudent) {
  print("he is not smart and not a student");
  };

}

CodePudding user response:

I believe the issue is with the scope of your isStudent and isSmart. Try this:

void main() {
  String student = promptStudent();
  bool isStudent = false;
  if(student == "yes"){
    isStudent = true;
  }

  String smart = promptSmart();
  bool isSmart = false;
  if(smart == "4"){
    isSmart = true;
  }
  ...
  • Related