Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do loop question 1

Status
Not open for further replies.

nitroman84

Technical User
May 10, 2011
5
US
Hello, I'm racking my brain here. I have been self teaching fortran for some time now and this is the program I use for practice. In my practice program I am asking for an input of 0 or 1 as a yes or no answer to continue execution and any other # will give an error message. My question is how to expand this to include letters a-z as also giving an error message? As my code stands I just get a runtime error if a letter is accidentally entered.

Here is the source code...

Program Practice

Implicit None

Character :: Uname*25, Uage*3, Ustate*25

Integer :: Uanswer1, Uanswer2

Call System ("title My Practice Program && mode 50,20 && color 80")

Print "(a,/)", 'Enter your name, age and state'
Print "(/,a,/,/,a,/,a)", 'Continue ?', 'Type 1 to continue', &
'Type 0 to exit program'
Read *, Uanswer1

Do
Do
If (Uanswer1 >= 2) Then !<--- this if statement is the code in question.
Call System ('title Error! && mode 20,5 && color ce')
Print "(/,/,a,/,/,a,/,a)", 'Error!','Type 1 to continue', 'Type 0 to exit'
Read *, Uanswer1
Else
Call System ('title My Practice Program && mode 50,20 && color 80 ')
Exit
End If
End Do

If (Uanswer1 == 0) Then
Exit
Else If (Uanswer1 == 1) Then
Continue
Else
Exit
End If

Open(10, File = 'C:\Users\Public\Desktop\Output.txt', Status = 'Unknown')

Print "(/,/,a)", 'Enter name'
Read *, Uname
Print "(/,a)", 'Enter age'
Read *, Uage
Print "(/,a)", 'Enter state'
Read *, Ustate

Write(10, *) " "
Write(10, *) " Name : ", Uname
Write(10, *) " Age : ", Uage
Write(10, *) " State : ", Ustate
Write(10, *) " ________________________________ "

Print "(/,/,a)", 'Enter another?'
Read *, Uanswer2

Do
If (Uanswer2 >= 2) Then
Call System ('title Error! && mode 20,5 && color ce')
Print "(/,/,a,/,/,a,/,a)", 'Error!','Type 1 to continue', 'Type 0 to exit'
Read *, Uanswer2
Else
Call System ('title My Practice Program && mode 50,20 && color 80 ')
Exit
End If
End Do

If (Uanswer2 == 0) Then
Exit
Else If (Uanswer2 == 1) Then
Continue
Else
Exit
End If
End Do

Close(10)

If (Uanswer1 == 0) Then
Print "(/,/,a,/,/,a)", 'Program was stopped before execution', &
'Press Enter to exit'
Else If (Uanswer1 == 1) Then
Call System ('C:\Users\Public\Desktop\Output.txt')
Print "(/,/,a,/,/,a)", 'Results have been written to "Output.txt"', &
'Press Enter to exit'
End If

Read *,

End Program Practice

Any help would be much appreciated.
 
You might declare Unanswer1 CHARACTER instead of INTEGER

YES will be represented either by '1', 'y' or 'Y', all other values meaning 'NO'

Another solution would be to add and IOSTAT clause to the read statement, which allows you to test whether the reading has met a trouble or not.

About the organization of you program, I don't like very much the fact that you need several loops when a single one is sufficient. The general idea, for this kind of algorithm, is to construct a state machine. Following the current state, you read something which leads to perform a certain task and to change the state accordingly.

François Jacq
 
Thanks for the info, and yes I know it is a real rat's nest of a source file. I was attempting to first get it to do what I wanted and second see what I could do after that to trim it down some.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top