none
File Input and reading from file RRS feed

  • Question

  • So i'm stuck for an assignment in my small basic class. The assignment wants us to create a menu of 3 options for the user to choose from. The first option adds a username and password to a file. The second option asks the user to enter a username and password and it'll check the file to see if the credentials they entered are correct. The third option is just to exit the program. I'm currently stuck on option 2. Currently i have sub routine set up to have the user enter the username and password and then to check the file, but it doesn't seem to be reading the file line like i want it to and is skipping the If statement and saying everything i input as the wrong credentials while also not repeating the while loop. It spits out "Invalid username or password" once and then the program stops. I'd appreciate any help! Here is the code for the sub routine thus far:

     Sub enterUser
        
        'User entering their username and password
        TextWindow.Write("Please enter your user name: ")
        enterUserName = TextWindow.Read()
        TextWindow.Write("Please enter your password: ")
        enterPassword = TextWindow.Read()
        
        'Reading username and passwords from file
        line = 1
        username = File.ReadLine(filename, line)
        line = line + 1
        password = File.ReadLine(filename, line)
       
        
        While username <> ""
          'Check to see if user name matches the file
         If enterUserName = username Then
            If enterPassword = password Then
              TextWindow.WriteLine("Your credentials are correct!")
              enterUserName = ""
            EndIf
          Else
            ' The username did not match
            line = line + 1
            username = File.ReadLine(filename, line)
            line = line + 1
            password = File.ReadLine (filename, line)
            TextWindow.WriteLine("Invalid username or password")
          EndIf
        EndWhile
        EndSub 


    Saturday, November 21, 2015 3:11 AM

Answers

  • Your program flow should be:

    ask username and password

    again: compare with next entry in file ( 1 2 3 ...)

    if right: OK message and skip the rest of checking 

    if wrong and not at end of file: goto again

    if at end of file without right entry: message invalid


    Jan [ WhTurner ] The Netherlands

    Saturday, November 21, 2015 1:12 PM
    Answerer
  • To help you out, here is a working example

    filename=Program.Directory+"\passwrd.txt"
    enterUser()
    
    Sub enterUser
      'User entering their username and password
      TextWindow.Write("Please enter your user name: ")
      enterUserName = TextWindow.Read()
      TextWindow.Write("Please enter your password: ")
      enterPassword = TextWindow.Read()
      
      'Reading username and passwords from file
      line = 1
      found="False"
      username="True"
      While username <> ""
        username = File.ReadLine(filename, line)
        password = File.ReadLine(filename, line+1)
        'Check to see if user name matches the file
        If enterUserName = username AND enterPassword = password Then
          TextWindow.WriteLine("Your credentials are correct!")
          UserName = ""
          found="True"
        Else
          ' The username did not match
          line = line + 2
          username = File.ReadLine(filename, line)
          password = File.ReadLine (filename, line+1)
        EndIf
      EndWhile
      If found<>"True" then
        TextWindow.WriteLine("Invalid username or password")
      EndIf
    EndSub 
    


    Jan [ WhTurner ] The Netherlands

    Saturday, November 21, 2015 3:56 PM
    Answerer
  • To test:

    Try to write something to a file (not with your program, but in a simple one of a few lines). Chech if there is a file with the right contents (with e.g. Explorer.)

    Then try to read back the file in SmallBasic. THen you know where is the problem.

    Writetest program:

    Fil=Program.Directory+"\testfile.txt"
    File.AppendContents(Fil,"this is a testline")
    Save this program before running, otherwise the output goes to a temporary directory!


    Jan [ WhTurner ] The Netherlands


    Tuesday, November 24, 2015 11:28 AM
    Answerer

All replies

  • Your program flow should be:

    ask username and password

    again: compare with next entry in file ( 1 2 3 ...)

    if right: OK message and skip the rest of checking 

    if wrong and not at end of file: goto again

    if at end of file without right entry: message invalid


    Jan [ WhTurner ] The Netherlands

    Saturday, November 21, 2015 1:12 PM
    Answerer
  • To help you out, here is a working example

    filename=Program.Directory+"\passwrd.txt"
    enterUser()
    
    Sub enterUser
      'User entering their username and password
      TextWindow.Write("Please enter your user name: ")
      enterUserName = TextWindow.Read()
      TextWindow.Write("Please enter your password: ")
      enterPassword = TextWindow.Read()
      
      'Reading username and passwords from file
      line = 1
      found="False"
      username="True"
      While username <> ""
        username = File.ReadLine(filename, line)
        password = File.ReadLine(filename, line+1)
        'Check to see if user name matches the file
        If enterUserName = username AND enterPassword = password Then
          TextWindow.WriteLine("Your credentials are correct!")
          UserName = ""
          found="True"
        Else
          ' The username did not match
          line = line + 2
          username = File.ReadLine(filename, line)
          password = File.ReadLine (filename, line+1)
        EndIf
      EndWhile
      If found<>"True" then
        TextWindow.WriteLine("Invalid username or password")
      EndIf
    EndSub 
    


    Jan [ WhTurner ] The Netherlands

    Saturday, November 21, 2015 3:56 PM
    Answerer
  • This doesn't seem to be working either. I've tried something similar to this as well. For some reason I feel like it isn't reading the file i have saved in the filepath. I'm really not sure where i'm going wrong. I appreciate the help though
    Monday, November 23, 2015 8:58 PM
  • To test:

    Try to write something to a file (not with your program, but in a simple one of a few lines). Chech if there is a file with the right contents (with e.g. Explorer.)

    Then try to read back the file in SmallBasic. THen you know where is the problem.

    Writetest program:

    Fil=Program.Directory+"\testfile.txt"
    File.AppendContents(Fil,"this is a testline")
    Save this program before running, otherwise the output goes to a temporary directory!


    Jan [ WhTurner ] The Netherlands


    Tuesday, November 24, 2015 11:28 AM
    Answerer