0

After Receiving an integer as input from the keyboard, and passing it to a subroutine that should be responsible for returning the first number, how does one loop through the input and get the first value? If they enter 123456 , I would need to return 6, then 5, then 3, then 1.in different subroutines.

    PROGRAM DATING
    IMPLICIT NONE
    INTEGER :: num, first, second, fourth, sixth    ! varialbe declaration
    CHARACTER(Len=10) :: output
    WRITE *,' What number did prison Ex give you? '
    READ *, num
    CALL FIRST( + num)
    CALL SECOND( + second)
    CALL FOURTH( + fourth)
    CALL SIXTH( + sixth)
    WRITE(output,'(i2.2)')num                       ! program output
    WRITE *, "Use"                                  ! program output
    END PROGRAM DATING

    SUBROUTINE FIRST(first)
    IMPLICIT NONE
    RETURN
    END SUBROUTINE FIRST

    SUBROUTINE SECOND(Second)
    IMPLICIT NONE
    RETURN
    END SUBROUTINE SECOND

    SUBROUTINE FOURTH(fourth)
    IMPLICIT NONE
    RETURN
    END SUBROUTINE FOURTH

    SUBROUTINE SIXTH(sixth)
    IMPLICIT NONE
    RETURN
    END SUBROUTINE SIXTH

I have tried to develop subroutines to handle each case but looping through input is tricky

4
  • Welcome, I suggest taking the tour. What about reading the individual digit characters as separate integers straoght away? Do you need to read it as one integer? But I do not ezactly understand what the subroutines are supposed to do. Why is there no second and third? Why is there sixth, when 12345 has only 5 digits? Please explain what exactly should happen. Commented Feb 12, 2023 at 20:55
  • Are changing the code in your question based on my answer? Commented Feb 12, 2023 at 21:20
  • 1
    I rolled the question back to the state before my answer. If you wish to ask about a new code that implements techniques showed in my answer, you are welcome to do so, but you must explain the exact actual problem with the new code. And it should most likely go inte a new question post. Commented Feb 13, 2023 at 14:38
  • Unrelated but I prefer to put all functions and subroutines inside the program block, after a contains keyword. The compiler can check argument types and other things if you do so. Commented Feb 13, 2023 at 15:01

1 Answer 1

1

If you want to read six integer digits from a string with six digits, you can read them directly:

integer :: digits(6)

read(*,'(6i1)')  digits

You can also convert an existing integer number to a string and do the same:

character(6) :: string

write(string,*) num
read(string,'(6i1)') digits

And you can also compute the digits from the integer using division by 10 and remainders after division:

sixth = mod(num, 10)
num = num/10
fifth = mod(num, 10)
num = num/10
...

Or just make a loop and use the digits array. The above just shows the main idea.

You must make sure that there are indeed six digits or implement some checks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.