Computer Science Canada help, attempt to read past eof |
Author: | CarrieP [ Fri Jun 22, 2007 12:04 pm ] |
Post subject: | help, attempt to read past eof |
my problem is on line 11 (there is a datafile to go with this) %phonebook var wrongCount, realCount : real var stream, count, sortBy, typeOfSort : int var nums : string open : stream, "phonebook.dat", get wrongCount := 0 loop exit when eof (stream) get : stream, skip get : stream, nums : * wrongCount := wrongCount + 1 end loop realCount := wrongCount / 5 count := ceil (realCount) var lName, fName, street, phoneNum, streetNum : array 1 .. count of string close : stream open : stream, "H:/Assignments/phonebook.dat", get for i : 1 .. count get : stream, lName (i) : * get : stream, fName (i) : * get : stream, phoneNum (i) : * get : stream, street (i) : * get : stream, streetNum (i) : * end for close : stream for i : 1 .. count put i, " ", lName (i) : 8, " ", fName (i) : 8, " ", streetNum (i) : 4, " ", street (i) : 16, " ", phoneNum (i) end for put " " %number trap for sorting var num : string var len, nonLettersInNum, signsOutOfPlace, decimalsInNum, nonRandCharsInNum : int const letters := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const randChrs := "`~!@#$%^&*()_=[{]}|;:'<,>.?/" const signs := "+-" const decimal := "." const plus := "+" loop nonLettersInNum := 0 signsOutOfPlace := 0 decimalsInNum := 0 nonRandCharsInNum := 0 put "What would you like to sort the list by?" put "1. LN" put "2. FN" put "3. Street" put "4. Phone #" get num put " " len := length (num) for h : 1 .. len if index (letters, num (h)) = 0 then nonLettersInNum := nonLettersInNum + 1 end if if index (randChrs, num (h)) = 0 then nonRandCharsInNum := nonRandCharsInNum + 1 end if if index (decimal, num (h)) not= 0 then decimalsInNum := decimalsInNum + 1 end if end for for h : 2 .. len if index (signs, num (h)) not= 0 then signsOutOfPlace := signsOutOfPlace + 1 end if end for exit when nonLettersInNum = len and signsOutOfPlace = 0 and decimalsInNum < 2 and nonRandCharsInNum = len and num < "5" and num > "0" cls put "Invalid Number" put " " end loop cls sortBy := strint (num) loop nonLettersInNum := 0 signsOutOfPlace := 0 decimalsInNum := 0 nonRandCharsInNum := 0 put "What sort would you like to use?" put "1. Bubble" put "2. Exchange" put "3. Selection" get num put " " len := length (num) for h : 1 .. len if index (letters, num (h)) = 0 then nonLettersInNum := nonLettersInNum + 1 end if if index (randChrs, num (h)) = 0 then nonRandCharsInNum := nonRandCharsInNum + 1 end if if index (decimal, num (h)) not= 0 then decimalsInNum := decimalsInNum + 1 end if end for for h : 2 .. len if index (signs, num (h)) not= 0 then signsOutOfPlace := signsOutOfPlace + 1 end if end for exit when nonLettersInNum = len and signsOutOfPlace = 0 and decimalsInNum < 2 and nonRandCharsInNum = len and num < "4" and num > "0" cls put "Invalid Number" put " " end loop cls typeOfSort := strint (num) var temp : string if sortBy = 1 then if typeOfSort = 1 then %bub. sort for i : 1 .. count for j : 1 .. count - 1 if lName (j) > lName (j + 1) then temp := lName (j) lName (j) := lName (j + 1) lName (j + 1) := temp temp := fName (j) fName (j) := fName (j + 1) fName (j + 1) := temp temp := street (j) street (j) := street (j + 1) street (j + 1) := temp temp := phoneNum (j) phoneNum (j) := phoneNum (j + 1) phoneNum (j + 1) := temp temp := streetNum (j) streetNum (j) := streetNum (j + 1) streetNum (j + 1) := temp end if end for end for %end bub elsif typeOfSort = 2 then %ex sort var s : int for i : 1 .. count - 1 s := i for j : i .. count if lName (j) < lName (s) then s := j end if end for %swap temp := lName (i) lName (i) := lName (s) lName (s) := temp temp := fName (i) fName (i) := fName (s) fName (s) := temp temp := street (i) street (i) := street (s) street (s) := temp temp := phoneNum (i) phoneNum (i) := phoneNum (s) phoneNum (s) := temp temp := streetNum (i) streetNum (i) := streetNum (s) streetNum (s) := temp end for %end exchange sort elsif typeOfSort = 3 then %selection sort for i : 1 .. count - 1 for j : i + 1 .. count if lName (j) <= lName (i) then %swap procedure (exchange of places) temp := lName (j) lName (j) := lName (i) lName (i) := temp temp := fName (j) fName (j) := fName (i) fName (i) := temp temp := street (j) street (j) := street (i) street (i) := temp temp := phoneNum (j) phoneNum (j) := phoneNum (i) phoneNum (i) := temp temp := streetNum (j) streetNum (j) := streetNum (i) streetNum (i) := temp end if end for end for %end s end if elsif sortBy = 2 then if typeOfSort = 1 then %bubble sort for i : 1 .. count for j : 1 .. count - 1 if fName (j) > fName (j + 1) then %swap procedure (exchange of places) temp := lName (j) lName (j) := lName (j + 1) lName (j + 1) := temp temp := fName (j) fName (j) := fName (j + 1) fName (j + 1) := temp temp := street (j) street (j) := street (j + 1) street (j + 1) := temp temp := phoneNum (j) phoneNum (j) := phoneNum (j + 1) phoneNum (j + 1) := temp temp := streetNum (j) streetNum (j) := streetNum (j + 1) streetNum (j + 1) := temp %end of swap end if end for end for elsif typeOfSort = 2 then var s : int for i : 1 .. count - 1 s := i for j : i .. count if fName (j) < fName (s) then s := j end if end for %swap temp := lName (i) lName (i) := lName (s) lName (s) := temp temp := fName (i) fName (i) := fName (s) fName (s) := temp temp := street (i) street (i) := street (s) street (s) := temp temp := phoneNum (i) phoneNum (i) := phoneNum (s) phoneNum (s) := temp temp := streetNum (i) streetNum (i) := streetNum (s) streetNum (s) := temp end for %end exchange sort elsif typeOfSort = 3 then %selection sort for i : 1 .. count - 1 for j : i + 1 .. count if fName (j) <= fName (i) then %swap procedure (exchange of places) temp := lName (j) lName (j) := lName (i) lName (i) := temp temp := fName (j) fName (j) := fName (i) fName (i) := temp temp := street (j) street (j) := street (i) street (i) := temp temp := phoneNum (j) phoneNum (j) := phoneNum (i) phoneNum (i) := temp temp := streetNum (j) streetNum (j) := streetNum (i) streetNum (i) := temp %end of swap end if end for end for end if elsif sortBy = 3 then if typeOfSort = 1 then %bubble sort for i : 1 .. count for j : 1 .. count - 1 if street (j) > street (j + 1) then %swap procedure (exchange of places) temp := lName (j) lName (j) := lName (j + 1) lName (j + 1) := temp temp := fName (j) fName (j) := fName (j + 1) fName (j + 1) := temp temp := street (j) street (j) := street (j + 1) street (j + 1) := temp temp := phoneNum (j) phoneNum (j) := phoneNum (j + 1) phoneNum (j + 1) := temp temp := streetNum (j) streetNum (j) := streetNum (j + 1) streetNum (j + 1) := temp %end of swap end if end for end for %end bubble sort elsif typeOfSort = 2 then %exchange sort var s : int for i : 1 .. count - 1 s := i for j : i .. count if street (j) < street (s) then s := j end if end for temp := lName (i) lName (i) := lName (s) lName (s) := temp temp := fName (i) fName (i) := fName (s) fName (s) := temp temp := street (i) street (i) := street (s) street (s) := temp temp := phoneNum (i) phoneNum (i) := phoneNum (s) phoneNum (s) := temp temp := streetNum (i) streetNum (i) := streetNum (s) streetNum (s) := temp end for %end exchange sort elsif typeOfSort = 3 then %selection sort for i : 1 .. count - 1 for j : i + 1 .. count if street (j) <= street (i) then %swap procedure (exchange of places) temp := lName (j) lName (j) := lName (i) lName (i) := temp temp := fName (j) fName (j) := fName (i) fName (i) := temp temp := street (j) street (j) := street (i) street (i) := temp temp := phoneNum (j) phoneNum (j) := phoneNum (i) phoneNum (i) := temp temp := streetNum (j) streetNum (j) := streetNum (i) streetNum (i) := temp %end of swap end if end for end for %end selection sort end if elsif sortBy = 4 then if typeOfSort = 1 then %bubble sort for i : 1 .. count for j : 1 .. count - 1 if phoneNum (j) > phoneNum (j + 1) then temp := lName (j) lName (j) := lName (j + 1) lName (j + 1) := temp temp := fName (j) fName (j) := fName (j + 1) fName (j + 1) := temp temp := street (j) street (j) := street (j + 1) street (j + 1) := temp temp := phoneNum (j) phoneNum (j) := phoneNum (j + 1) phoneNum (j + 1) := temp temp := streetNum (j) streetNum (j) := streetNum (j + 1) streetNum (j + 1) := temp %end of swap end if end for end for %end bubble sort elsif typeOfSort = 2 then %exchange sort var s : int for i : 1 .. count - 1 s := i for j : i .. count if phoneNum (j) < phoneNum (s) then s := j end if end for %swap temp := lName (i) lName (i) := lName (s) lName (s) := temp temp := fName (i) fName (i) := fName (s) fName (s) := temp temp := street (i) street (i) := street (s) street (s) := temp temp := phoneNum (i) phoneNum (i) := phoneNum (s) phoneNum (s) := temp temp := streetNum (i) streetNum (i) := streetNum (s) streetNum (s) := temp end for %end exchange sort elsif typeOfSort = 3 then %selection sort for i : 1 .. count - 1 for j : i + 1 .. count if phoneNum (j) <= phoneNum (i) then %swap procedure (exchange of places) temp := lName (j) lName (j) := lName (i) lName (i) := temp temp := fName (j) fName (j) := fName (i) fName (i) := temp temp := street (j) street (j) := street (i) street (i) := temp temp := phoneNum (j) phoneNum (j) := phoneNum (i) phoneNum (i) := temp temp := streetNum (j) streetNum (j) := streetNum (i) streetNum (i) := temp %end of swap end if end for end for %end selection sort end if end if for i : 1 .. count put i, " ", lName (i) : 8, " ", fName (i) : 8, " ", streetNum (i) : 4, " ", street (i) : 16, " ", phoneNum (i) end for |
Author: | Cervantes [ Fri Jun 22, 2007 4:36 pm ] |
Post subject: | RE:help, attempt to read past eof |
It looks like the problem is that you're getting two lines from the file every time through your loop, but you only check for eof once. So, if your file has an odd number of lines (say 3), you'll get an error. Run through it: you open it, check if it's at eof. It's not, so you get two lines. Check if it's at eof. It's not, so you try to get two more lines, but there's only one line left. So you just read past eof. |