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

  • SEARCH
  • Home
  • 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 8717401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:17:48+00:00 2026-06-13T06:17:48+00:00

okay, in my program i open a .txt file with three columns. 1st column

  • 0

okay, in my program i open a .txt file with three columns. 1st column has integers, 2nd has a name (character string), and 3rd has real numbers. i have figured out how to search the integers and real numbers for the item the user enters (compare variables to the arrays storing the columns)…but when i try to search for a name entered by the user, in the character array, it never works. here is a all my code… i am looking for help in CASE(2)

~ n holds the number of lines in the file

PROGRAM database
IMPLICIT NONE

CHARACTER(30)::file, search
INTEGER::err, n, m, i, k, searchtype, recordnum
LOGICAL:: alive
REAL:: grade
REAL, ALLOCATABLE:: arrayr(:)
INTEGER, ALLOCATABLE:: arrayi(:)
CHARACTER(15), ALLOCATABLE:: arrayc(:)
CHARACTER(15)::name

101 FORMAT(A)

DO
    WRITE(*,101) "What is the filename?"
    READ(*,*) file

    INQUIRE(FILE=file, EXIST=alive)
    IF (alive.EQV..TRUE.) THEN
        WRITE(*,101) "The file exists"
        OPEN (UNIT=11, FILE=file, STATUS="OLD", ACTION="READ", IOSTAT=err)
        IF (err .NE. 0) THEN    
            WRITE(*,'(2A)')"There was an error opening ", file
            STOP
        END IF
        EXIT
    ELSE IF (alive.EQV..FALSE.) THEN        
        WRITE(*,'(2A)') "There is no file by the name of: ", file
        CYCLE
    END IF
END DO


n= 0
DO
    READ(11,*,IOSTAT=k)
    IF(k.EQ.-1) EXIT
    n=n+1
END DO
REWIND(11)
ALLOCATE(arrayi(n),arrayc(n),arrayr(n))


DO i=1,n,1
    READ(11,'(I4,A,F12.2)') arrayi(i), arrayc(i), arrayr(i)
END DO

outer: DO
    DO
        WRITE(*,101)"How would you like to search the file?"
        WRITE(*,101)"1) By record number"
        WRITE(*,101)"2) By name"
        WRITE(*,101)"3) By rating"
        READ(*,*) searchtype
        SELECT CASE (searchtype)
            CASE(1)
                WRITE(*,101) "Please enter the record number:"
                READ(*,*) recordnum
                m=0
                DO i=1,n,1
                    IF (arrayi(i).EQ.recordnum) THEN
                        WRITE(*,'(I4,A,F12.2)')arrayi(i),arrayc(i), arrayr(i)
                    ELSE IF (i==n) THEN
                        write(*,*)"Sorry dude. The name you entered     was not found. Search failed"
                    END IF
                END DO
                EXIT
            CASE(2)
                WRITE(*,101) "Please enter the name:"
                READ(*,*) name
                m=0
                DO i=1,n
                    IF (name.EQ.arrayc(i)) THEN
                        WRITE(*,'(I4,A,F12.2)')arrayi(i), arrayc(i), arrayr(i)
                    ELSE IF (i==n) THEN
                        write(*,*)"Sorry dude. The name you entered was not found. Search failed"
                    END IF
                END DO
                EXIT
            CASE(3)
                WRITE(*,101) "This will return all graders greater than your search term"
                WRITE(*,101) "Please enter the minimum grade:"
                READ(*,*) grade
                m=0
                DO i=1,n,1
                    IF (arrayr(i).GT.grade) THEN
                        WRITE(*,'(I4,A,F12.2)')arrayi(i), arrayc(i), arrayr(i)
                    ELSE IF (i==n) THEN
                        write(*,*)"Sorry dude. The name you entered was not found. Search failed"
                    END IF
                END DO
                EXIT
            CASE DEFAULT
                WRITE(*,101) "Invalid entry. Please enter 1, 2, or 3."
                CYCLE
        END SELECT
    END DO


    inner: DO
        WRITE(*,101)"Would you like to search again? (Y/N)"
        READ(*,*)search
            SELECT CASE (search)
                CASE("Y","y")
                    CYCLE outer
                CASE("N","n")
                    EXIT outer
                CASE DEFAULT
                    WRITE(*,101)"Invalid entry. please enter Y or N."
                    CYCLE inner
                END SELECT
    END DO inner
END DO outer


END PROGRAM database

everytime i select case(2) in the program and then enter a name I KNOW EXISTS in the character arrayc, it skips over the if clause where (name.EQ.arrayc) and returns that the name was not found. PLEASE HELP! i am very open to other ways of doing this. i dont understand why this is happening.

There is a screenshot on this link:
http://tinypic.com/r/33tmmuc/6

  • 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-13T06:17:49+00:00Added an answer on June 13, 2026 at 6:17 am

    —-EDIT – SOLUTION—-

    You read the string by a weird format and it starts with spaces. Use this for the comparison:

    if (name==adjustl(arrayc(i))) then
    

    Check the handbook for what it does.

    Consider getting rid of the CAPITAL letters and archaic .EQ. operators.

    Use better formats and don’t use the labels for them, but a string variable or a constant. Something as

    '(i4,2x,a15,F12.0)'
    

    might be ok, check for the correct values of the numbers. Be aware that F12.2 reads 100 as 1.00.

    Also you implemented the loop wrong. The exit should be just in the place after you found the answer.

    —-EDIT – SOLUTION—-

    Works for me:

    CHARACTER(15), ALLOCATABLE:: arrayc(:)
    CHARACTER(30)::name
    INTEGER:: n
    
        allocate(arrayc(1))
        arrayc = (/"ok1","ok2"/)
        n=2
        write(*,*) arrayc(1)
    
        WRITE(*,*) "Please enter the name:"
        READ(*,*) name
        m=0
        DO i=1,n,1
            IF(name.EQ.arrayc(i)) THEN
                WRITE(*,'(A)') arrayc(i)
            ELSE IF (name.NE.arrayc(i)) THEN
                m=m+1
            END IF
        END DO
        IF (m.EQ.n) THEN
            WRITE(*,*)"Sorry dude. The name you entered was not found. Search failed"
        END IF
    end
    

    run:

    > ./a.out 
     ok1            
     Please enter the name:
    ok1
    ok1
    

    Be sure to have correct n. Also try to print the character array to be sure. Be careful not to compare name which is too long (your variable is two times longer than the strings in the array). Your practice of comparing the value of the loop index after the loop is questionable, its value is undefined.

    I would completely rewrite the loop structure. Something as:

       ...
        WRITE(*,*) "Please enter the name:"
        READ(*,*) name
    
        do i=1,n
            if(name.EQ.arrayc(i)) then
                write(*,'(A)') arrayc(i)
                exit
            else if (i==n) then
                write(*,*)"Sorry dude. The name you entered was not found. Search failed"
            end if
    
        end do
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay. I have completed my first python program.It has around 1000 lines of code.
In a C++ program, using std::ifstream, I'm attempting to open a user-specified file --
Okay so right now I'm working on a program that has a whole bunch
Okay..the output of a python program is shown into a Tkinter window..which opens separately.
Okay so I'm make a program where I will be universally accepting cocoa trackpad
Okay so I'm trying to make a little gag program that will run away
Okay, I am working on a card playing program, and I am storing card
Okay, just learnt LINQ syntax about ten minutes ago, made a first program: public
Okay, so I was teaching my girlfriend some c++, and she wrote a program
Okay, so at first when i run my win32 program the menu works fine,

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.