Comp Sci Solving Quadratic Equations: Invalid Inputs

AI Thread Summary
The discussion focuses on handling invalid inputs in a Fortran program for solving quadratic equations. The user seeks advice on how to prompt for re-entry of coefficients A, B, and C when an invalid character is input, expressing frustration with repeated course failures. Suggestions include reading input as a string and validating it before converting to a real number. The importance of error handling and user prompts in programming is emphasized. Effective input validation is crucial for improving user experience and program reliability.
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
2
Views
6K
Replies
2
Views
2K
Replies
4
Views
1K
Replies
21
Views
2K
Replies
1
Views
10K
Replies
15
Views
3K
Replies
7
Views
2K
Back
Top