Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8445101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:34:24+00:00 2026-06-10T09:34:24+00:00

I have a read statement that expects a number, very simple example code: program

  • 0

I have a read statement that expects a number, very simple example code:

program test
integer var
read(*,*) var
end

The thing is that I usually input a string of characters (ie: yes) on account of being distracted. How can I prevent my code from halting entirely and instead display an error message of the type You’ve entered an incorrect value. Try again?

I’m thinking something like:

    program test
    integer var
10  read(*,*) var
    if (var.not.a.number) then
      write(*,*)'You've entered an incorrect value. Try again'
      goto 10
    endif
    end

What would that var.not.a.number condition look like?
I use gfortran to compile under Ubuntu.


Edit: Thank you all! I ended up implementing HPM’s 3rd option since it was the simplest one:

    program test
    integer var,iostat,ios
10  read(*,*,iostat=ios) var
    if (ios.ne.0) then
      write(*,*)'You've entered an incorrect value. Try again'
      goto 10
    endif
    end

Special thanks to User7391 who took the effort to write all that code!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:34:26+00:00Added an answer on June 10, 2026 at 9:34 am

    You’re using list-directed input. The second * in the statement read(*,*) essentially tells the compiler/run-time system that you will provide it with something at run time that can be interpreted as an integer. If you want to give yourself the freedom to make mistakes on input you have (at least) 3 choices.

    1. You can, as @User7391’s answer already says, read the input into a character variable and parse it yourself. That kind user has even offered to write the code for you !
    2. You could modify the read command to something like read(*,*,err=1234) where 1234 is the label of your error-handling statement(s). This approach is now considered rather old-fashioned and might be frowned upon.
    3. You could modify the read command to something like read(*,*,iostat=ios) where ios is an integer variable which catches the iostat (i/o status flag) reported by the read statement. You might then write the line if (iostat/=0) ... for error handling. This is considered to be the more up to date approach.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read some vague statement that virtual inheritance doesn't provide the memory structure
I have an if/else statement that is not working. while (rdr.Read()) { string permission
I have a problem with going over an if statement that the code should
I have some codes read the query results from sqlite: while (sqlite3_step(statement) == SQLITE_ROW)
I have read that GLSL (specifically v1.0.17: my application is running under WebGL) compilers
I have read that after select we use column-names but I have found a
I have read somewhere that using the class instances as below is not a
I am creating sql queries and I have read in one book that when
Currently I have some code that is doing an SFTP via expect/tcl. Its something
I have this loop that parses machine code and emulates the instructions as if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.