Solving Quadratic Equations: Invalid Inputs

Click For Summary
SUMMARY

This discussion focuses on handling invalid inputs in Fortran when solving quadratic equations. The user seeks assistance in prompting for re-entry of coefficients A, B, and C if non-real values are inputted. A suggested solution involves reading input as a string and validating it before conversion to a real number. This method ensures that the program can handle errors gracefully and guide the user to provide valid input.

PREREQUISITES
  • Basic knowledge of Fortran programming
  • Understanding of real number data types in Fortran
  • Familiarity with conditional statements in programming
  • Experience with string manipulation in Fortran
NEXT STEPS
  • Learn how to read input as a string in Fortran
  • Research error handling techniques in Fortran
  • Explore the use of regular expressions for input validation
  • Study examples of user input validation in Fortran programming
USEFUL FOR

Students learning Fortran, educators teaching programming concepts, and developers implementing input validation in numerical applications.

Luke1986
Messages
2
Reaction score
0

Homework Statement



IMPLICIT NONE
REAL :: A, B, C, DISCR, X1, X2, x1i, x2i
CHARACTER(3)::ANS,ANS1
! Reads the coefficients for the quadratic equation
33 WRITE(*,*)")"Please enter a *REAL*NUMBER* coefficient ( A )."
READ(*,*)A
WRITE(*,*)"Please enter a *REAL*NUMBER* coefficient ( B )."
READ(*,*)B
WRITE(*,*)"Please enter a *REAL NUMBER* coefficient ( C )."
READ(*,*)C
WRITE(*,*)"The coefficients for A,B,C are",A,B,C

Homework Equations



This is only a little part of the whole program. My question is when I input a invalid character, like a letter or symbol, I want to tell the user that it is a error. Since A,C,B are all real variables how do I ask If I can let the user reenter a new value. Maybe an If statement? But I think you cannot compare reals and character. I am new to fortran my prof keeps failing me its my 4th time taking this course. It is very hard for me to comprehend so much details. Can someone assist me please? thank you!

The Attempt at a Solution

 
Physics news on Phys.org
Hi Luke1986! http://img96.imageshack.us/img96/5725/red5e5etimes5e5e45e5e25.gif

To validate input, you can read it as characters or a string and check that it conforms to a real before treating it as such.

I'm sure almost any textbook would have this or something similar as an example.

Good luck!
 
Last edited by a moderator:

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
4
Views
2K