You are not logged in.

#1 2024-10-14 01:08:54

BluePyTheDeer_
Member
Registered: 2024-05-23
Posts: 95

[SOLVED] What is wrong with my Dart code?

I have looked for AGES for the solution but it still doesn't do what I tell it to.

Context: It's supposed to be a grade checker I made for fun to experiment with Dart, but it just doesn't work, and when it does, it just decides to print all if statements.

Code:

import 'dart:io';

void main() {
  gradeCheck() {
    stdout.write("What is your grade, from 0-10?");
    String? grade = stdin.readLineSync();
    if (grade != null) ; {
      int totalGrade = int.parse(grade!);
      if (totalGrade == 0) ;
      {
        print("You failed the test, try harder next time!");
      }
      if (totalGrade <= 5) ;
      {
        print("You pass and did well, good job!");
      }
      if (totalGrade <= 10) ;
      {
        print("You got a very nice score, great job, keep it up!");
      }
      if (totalGrade == 10) ;
      {
        print("And you got a 10, great job!");
      }
    }
  }

  gradeCheck();
}

Edit: found the null thing to work by adding a ! next to it, but now it prints all if statements.

Last edited by BluePyTheDeer_ (2024-10-15 17:38:00)


I messed my Arch Linux installation, then fixed it smile
"Sometimes the best complexity is simplicity." - BluePy, 1856.

Offline

#2 2024-10-15 10:01:51

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,102

Re: [SOLVED] What is wrong with my Dart code?

Remove all the semicolons from your if statement lines (which is why you also need to workaround with the nullchecking "!" because you're not letting the if statement determine the scope it needs to run), you're "finishing" the statement that way, which prevents it from actually evaluating the next lines to be ran conditionally based on the result.

Compare with the examples from https://www.darttutorial.org/dart-tutor … f-else-if/ .

Last edited by V1del (2024-10-15 10:55:41)

Offline

#3 2024-10-15 17:23:30

BluePyTheDeer_
Member
Registered: 2024-05-23
Posts: 95

Re: [SOLVED] What is wrong with my Dart code?

Thank you so much! I had tried EVERYTHING from seeing forsaken Stack Overflow forum posts to random pages, just, Thank you!

Finally, I can rest knowing that the Dart script I made finally works as intended.

Last edited by BluePyTheDeer_ (2024-10-15 17:26:01)


I messed my Arch Linux installation, then fixed it smile
"Sometimes the best complexity is simplicity." - BluePy, 1856.

Offline

#4 2024-10-16 01:31:17

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,102

Re: [SOLVED] What is wrong with my Dart code?

I'm fairly certain most resources will have had this correct, but seems you didn't see the forest for the trees unless it was explicitly pointed out tongue

Offline

#5 2024-10-16 02:14:51

BluePyTheDeer_
Member
Registered: 2024-05-23
Posts: 95

Re: [SOLVED] What is wrong with my Dart code?

I had never learnt an OOP language, so I just went "So everything needs ;s, amazing."


I messed my Arch Linux installation, then fixed it smile
"Sometimes the best complexity is simplicity." - BluePy, 1856.

Offline

#6 2024-10-16 12:18:41

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,102

Re: [SOLVED] What is wrong with my Dart code?

OOP has nothing to do with this, you generally need some control characters to know when a statement starts or ends. Assuming you're used to python, where white space/the indent level controls this.

In languages with these charactes you could theorethically write the entire program on a single line, not very practical, but hey smile

Offline

Board footer

Powered by FluxBB