Run-time errors lesson

Hey, I was doing the run-time errors lesson where we have to fix this:

#include <iostream>

int main() {
  
  int width = 20;
  int length = 0;

  int ratio = width / length;
  
  std::cout << ratio << "\n";
  
}

Changed it to this:


#include <iostream>

int main() {
  
  int width = 20;
  int length = 30;

  int ratio = width / length;
  
  std::cout << ratio << "\n";
  
}

It works fine, but the answer gives me 0, which is supposed to be 0.6666…
I changes int ratio to double ratio, same thing. Anyone knows? Thanks.

Doing division between two integers is going to result in an integer. It would be weird if the result out of nowhere got a different type from the inputs, right?

Converting an integer to double isn’t going to bring back information that was discarded. That information is not stored in an integer.

If you do division between doubles, what type would the result have?

1 Like