0

I am very new to coding and to using Octave, and I have been having this issue with my code. Essentially, I am trying to code a program that takes an input number and executes the idea of the Collatz Conjecture. It works partially, I still need to work out how to fix the first while loop, but that is not my problem.

Almost every other time I run the script, I receive an invalid call to function error. I've searched here and other websites but I haven't seen anyone have the issue on something as simple as the input.

Here is the code:

n = input("Please enter a positive integer: ");
#
testOne = isinteger (n);
while (testOne == 1 && n < 1)
  testOne = isinteger (n);
  n = input("Number is not a positive integer. Please enter a positive integer: ");
endwhile
#
while (n > 1)
  count = 1 + count;
  if (rem(n, 2) > 0)
    n = 3*n + 1;
    disp(n)
  else
    n = n/2;
    disp(n)
  endif
endwhile
printf("The number of steps is %d", count)

And here is the error:

error: called from
    OliviaCCHW
    OliviaCCHW at line 9 column 3

To be absolutely clear, the "line 9 column 3" points to the n = input line, right between the n and the equal sign.

ollie
  • 1
  • 2

1 Answers1

1

It looks like your program is not able to handle input that is not a positive integer. Try adding a check to see if the input is a positive integer, and if not, ask the user to input a positive integer.

You can use the isinteger function to check if the input is an integer. If it is not, you can ask the user to input a positive integer.

Mohamed Elgazar
  • 778
  • 4
  • 9