none
file input and creating a menu RRS feed

  • Question

  • I am stuck with how to do my assignment this week because the teacher never walked us through it and I was wondering if anyone could walk me through it and clear up some of the steps. I am confused on how to start off as well.

    here is my assignment:

    You work for a company called Lock Up Tite. They want you to write a front end program to grant authentication access to programs using a user name and password.

    Write a program that creates a loop and has three options for the user to select.

    • Option 1 allows the user to add a user name and password, which is then stored into a file.
    • Option 2 allows the user to enter a user name and password up to three times.  For a given attempt, the user will be notified if the attempt is successful or unsuccessful.
    • Option 3 will exit the loop and end the program

    Main Menu Requirements: When the program starts, present the user with a menu offering the three options described above. After the user chooses either option 1 or 2, the program should go back to the main menu. The final option will end the program.

    Option 1 Requirements: This option prompts the user for their user name and password and stores them in a file named C:\CIS116Asn8\Your_Name.txt.  The program should not overwrite what's already in the file; it should append the new user name and password to the end of the file.

    Option 2 Requirements: This option uses the C:\CIS116Asn8\Your_Name.txt file to verify the user is entering a valid user name and password.  It begins by asking the user for a user name and password within a loop that runs up to three times.  If the password is correct, an appropriate message will be printed, the loop will end, and the program will go back to the main menu.  If the user is unable to provide a user name and password that matches any of the combinations in the file, an appropriate message will be printed and the user will be allowed to try again.  If the user fails after three attemps, the loop should end and the program should go back to the main menu. 

    Technical Requirements:

    1. Create a module for each major task in the program.  Call the modules based on the option selected by the user.
    2. After options 1 and 2, redisplay the menu and allow the user to pick another option. Option 3 will exit the program.
    3. Make sure your program is adequately commented, uses consistent naming conventions for variable and module names, and uses appropriate indentation and line spacing to enhance the readability of the code.
    4. Name the program Firstname_Lastname_Asn8.sb

    Saturday, October 24, 2015 3:36 PM

Answers

  • Here are all the Command you should look into:

    Textwindow
    File 
    Array 
    Text 
    For Loops 
    While Loops 
    
    You seem to be trying to use us to do your HW.. That may not be the case but try and put together a program first before asking for help.

    Saturday, October 24, 2015 4:10 PM
  • Here is a raw outline of the program:

    While "true"   'keeps running

       display options and ask for answer

       if answer = 1 then option1

       elseif answer = 2 then option2

       elseif answer = 3 then endprogram

    endwhile

    sub option1

     ask for name and password , save in file

    endsub

    sub option2

      ask for name and password,  search in file for corresponding entry

      if not there ask again (max 2 times)

    endsub

    You can write the subroutines separate and test them.


    Jan [ WhTurner ] The Netherlands

    Monday, October 26, 2015 2:07 PM
    Answerer
  • Elizabeth,  with the following changes  (marked with '<<<<) in your subroutine "checkUsernamePassword"  it finds if there is a match in the file and shows OK.

    Adapt to your assignment ;-)

    Sub checkUsernamePassword
      
      ' Call the sub addUsernamePassword to get username and password
      addUsernamePassword()
      
      currentLine = 1
      
      usernameFromFile = File.ReadLine(fileName, currentLine)
      currentLine = currentLine + 1
      passwordFromFile = File.ReadLine(fileName, currentLine)
      
      While usernameFromFile <> ""
        ' username matches
        if username = usernameFromFile then
          if password = passwordFromFile then
            TextWindow.WriteLine("   O K ")                          '<<<<<<<<<<<
            usernamefromfile=""                                      '<<<< to end the while loop
            Program.Delay(3000)                                      ' <<<<<show OK for 3 secs and return to menu
          endif
        else
          ' username does not match
          currentLine = currentLine + 1                              '  <<< read next name
          usernameFromFile = File.ReadLine(fileName, currentLine)    '<<<<<<<
          currentLine = currentLine + 1                              '<<<< read next pw
          passwordFromFile = File.ReadLine(fileName, currentLine)    '<<<<<<<
        endif
      EndWhile
    EndSub


    Jan [ WhTurner ] The Netherlands

    Thursday, October 29, 2015 10:07 AM
    Answerer

All replies

  • Here are all the Command you should look into:

    Textwindow
    File 
    Array 
    Text 
    For Loops 
    While Loops 
    
    You seem to be trying to use us to do your HW.. That may not be the case but try and put together a program first before asking for help.

    Saturday, October 24, 2015 4:10 PM
  • Here are all the Command you should look into:

    Textwindow
    File 
    Array 
    Text 
    For Loops 
    While Loops 
    You seem to be trying to use us to do your HW.. That may not be the case but try and put together a program first before asking for help.


    sorry its just my teacher never went over the assignment and im am really lost as what to do. I don't need you to do it for me but just clear it up and help me understand what i need to do for the assignment. The part i have the most trouble with is file input.
    Saturday, October 24, 2015 4:25 PM
  • Well assuming you store the log in info in an array you can do the following

    input = File.ReadContents(path)
    passwords = input["password"]
    usernames = input["username"]

    Saturday, October 24, 2015 5:51 PM
  • Well assuming you store the log in info in an array you can do the following

    input = File.ReadContents(path)
    passwords = input["password"]
    usernames = input["username"]

    i dont think i use an array because in all the examples an array was not used

    Sunday, October 25, 2015 10:10 PM
  • Here is a raw outline of the program:

    While "true"   'keeps running

       display options and ask for answer

       if answer = 1 then option1

       elseif answer = 2 then option2

       elseif answer = 3 then endprogram

    endwhile

    sub option1

     ask for name and password , save in file

    endsub

    sub option2

      ask for name and password,  search in file for corresponding entry

      if not there ask again (max 2 times)

    endsub

    You can write the subroutines separate and test them.


    Jan [ WhTurner ] The Netherlands

    Monday, October 26, 2015 2:07 PM
    Answerer
  • Here is a raw outline of the program:

    While "true"   'keeps running

       display options and ask for answer

       if answer = 1 then option1

       elseif answer = 2 then option2

       elseif answer = 3 then endprogram

    endwhile

    sub option1

     ask for name and password , save in file

    endsub

    sub option2

      ask for name and password,  search in file for corresponding entry

      if not there ask again (max 2 times)

    endsub

    You can write the subroutines separate and test them.


    Jan [ WhTurner ] The Netherlands

    i would just like to know if im going in the right direction and if its ok to do it like i am or if its close to your outline. I just dont want to be doing it the wrong way.

    'Elizabeth Spencer
    'CIS 116 
    '10/24/15
    'Write a program that requires writing data to a file and reading data from a file.
    
    'parameters for the sub
    minValue = 0
    maxValue = 0
    minLength = 0
    maxLength = 0
    
    
    
    'this is where the file is stored to
    fileDirectory = "C:\CIS116Asn8\"
    fileNameOnly = "Elizabeth_Spencer.txt"
    fileNameOnly = fileDirectory + fileNameOnly
    
    File.CreateDirectory(fileDirectory)
    
    mainMenu()
    
    While menuOption <> 3
      If menuOption = 1 Then
      displayUsernamePassword()
      ElseIf menuOption = 2 then
      EndIf
    endWhile 
    'sub name: displayUsernamePassword
    'Displays the username 
    'input: username
    'output: username
    Sub displayUsernamePassword
      TextWindow.WriteLine("Enter username: ")
       TextWindow.Read()
      TextWindow.WriteLine("Enter password: ")
      TextWindow.Read()
    EndSub
    
    ' Sub Name:       mainMenu
    ' Sub Purpose:  Displays the main menu and reads the selected option from the user
    ' Inputs:              None
    ' Outputs:           menuOption - The value of the selected menu option
    '                                                         1 - Enter username and password
    '                                                         2 - 
    '                                                         3 - Exit program
    
    Sub mainMenu
      TextWindow.Clear()
      TextWindow.WriteLine("1. enter username " + "enter password")
      TextWindow.WriteLine("2. ")
      TextWindow.WriteLine("3. exit program")
      minValue = 1
      maxValue = 3
      promptString = "Select a menu option: "
      getInteger()
      
      menuOption = returnValue
     EndSub
    
    ' Sub Name:       getInteger
    ' Sub Purpose:  Returns an integer within a specified range. Prompts the user to
    '                             retry the input until a value in the range is entered. The user is
    '                             required to input an integer, and will be prompted to retry if
    '                             they input a decimal value (eg, '1' and '1.0' are valid, '1.1' is not)
    ' Inputs:              promptString - Will be shown to the user before waiting for input
    '                             minValue        - The lowest value in the valid range (inclusive)
    '                             maxValue       - The highest value in the valid range (inclusive)
    ' Outputs:            returnValue   - The value that the user entered. Guaranteed to fall
    '                                                          in the valid range.
    Sub getInteger
      TextWindow.Write(promptString)
      returnValue = TextWindow.ReadNumber()
      _integerValue = Math.Floor(returnValue)
      
      While returnValue < minValue Or returnValue > maxValue Or _integerValue <> returnValue
        ' Display an error message to the user detailing whether the number is invalid due to being a decimal or out of range
        If _integerValue <> returnValue Then
          TextWindow.WriteLine("You entered a decimal, but a whole number is required. Please enter a whole number between " + minValue + " and " + maxValue)
        else      
          TextWindow.WriteLine("The value you entered was outside of the allowed range. Please enter whole number between " + minValue + " and " + maxValue)
        EndIf
        
        ' Re-prompt the user and read in a new number
        TextWindow.Write(promptString)
        returnValue = TextWindow.ReadNumber()
        _integerValue = Math.Floor(returnValue)
      EndWhile
    EndSub  
    
    ' Sub Name:       getString
    ' Sub Purpose:  Returns a string that falls within a specified length range. Prompts
    '                             the user to retry the input until a string with a valid length is supplied.
    ' Inputs:              promptString - Will be shown to the user before waiting for input
    '                             minLength     - The shortest valid string (inclusive)
    '                             maxLength    - The longest valid string (inclusive)
    ' Outputs:            returnValue   - The string that the user entered. Guaranteed to fall
    '                                                          in the valid length range.
    Sub getString
      TextWindow.Write(promptString)
      returnValue = TextWindow.Read()
      stringLength = Text.GetLength(returnValue)
      
      While stringLength < minLength or stringLength > maxLength 
        ' The user entered a string that didn't meet requirements, so we need to get a new string from him/her
        TextWindow.WriteLine("The text you entered was not a valid length. Please enter between " + minLength + " and " + maxLength + " characters.")
           
        TextWindow.Write(promptString)
        returnValue = TextWindow.Read()
        stringLength = Text.GetLength(returnValue)
      EndWhile
    EndSub
     

    Monday, October 26, 2015 3:48 PM
  • You are going in the right direction ! 

    One thing I saw in your program: the While menuOption<>3 loop keeps asking for name / password, because menuOption doesn't get any other value after you enter 1.  Solution: put a  mainMenu() statement  just before the  EndWhile, then you return to mainMenu


    Jan [ WhTurner ] The Netherlands

    Monday, October 26, 2015 4:09 PM
    Answerer
  • You are going in the right direction ! 

    One thing I saw in your program: the While menuOption<>3 loop keeps asking for name / password, because menuOption doesn't get any other value after you enter 1.  Solution: put a  mainMenu() statement  just before the  EndWhile, then you return to mainMenu


    Jan [ WhTurner ] The Netherlands

    i will try that but what would i do for option 2? im confused as to what goes into that part.

    which sub do i put the mainMenu() statement in? i do not get where you say to put it

    • Edited by iHatePrograming Monday, October 26, 2015 4:18 PM needed to add more
    Monday, October 26, 2015 4:16 PM
  • You are going in the right direction ! 

    One thing I saw in your program: the While menuOption<>3 loop keeps asking for name / password, because menuOption doesn't get any other value after you enter 1.  Solution: put a  mainMenu() statement  just before the  EndWhile, then you return to mainMenu


    Jan [ WhTurner ] The Netherlands


    I am also wondering why i cannot run my program when i have it saved in the D directory for the total program but a file is to be saved into the C directory. it tells me the D directory is invalid 
    Monday, October 26, 2015 4:45 PM
  • the mainMenu() goes just before the EndWhile of the While menuOption<>3

    Do you use a Program.Directory statement in your testprogram? When you save the .db file on D: Program.Directory returns D:\...  Or did you put the D: disk somewhere in the program?


    Jan [ WhTurner ] The Netherlands

    Monday, October 26, 2015 5:46 PM
    Answerer
  • the mainMenu() goes just before the EndWhile of the While menuOption<>3

    Do you use a Program.Directory statement in your testprogram? When you save the .db file on D: Program.Directory returns D:\...  Or did you put the D: disk somewhere in the program?


    Jan [ WhTurner ] The Netherlands


    ok the first part works and the other part works now. all that was wrong was my flash drive needed to be reput in and it worked just fine.
    Monday, October 26, 2015 5:48 PM
  • the mainMenu() goes just before the EndWhile of the While menuOption<>3

    Do you use a Program.Directory statement in your testprogram? When you save the .db file on D: Program.Directory returns D:\...  Or did you put the D: disk somewhere in the program?


    Jan [ WhTurner ] The Netherlands


    ok the first part works and the other part works now. all that was wrong was my flash drive needed to be reput in and it worked just fine.

    now i am  trying to finsih my assignment but im so confused as what to do that i dont even know where to go now. I'm sorry for asking for so much help but i do not know anything about programming and the book we have for class is not set up for small basic so its very hard. i just need to get better understanding of the topic and see maybe a few examples of something similar to what i need to do for my assignment. any help you could give me would be nice. thanks
    Monday, October 26, 2015 5:58 PM
  • the mainMenu() goes just before the EndWhile of the While menuOption<>3

    Do you use a Program.Directory statement in your testprogram? When you save the .db file on D: Program.Directory returns D:\...  Or did you put the D: disk somewhere in the program?


    Jan [ WhTurner ] The Netherlands


    ok the first part works and the other part works now. all that was wrong was my flash drive needed to be reput in and it worked just fine.


    now i am  trying to finsih my assignment but im so confused as what to do that i dont even know where to go now. I'm sorry for asking for so much help but i do not know anything about programming and the book we have for class is not set up for small basic so its very hard. i just need to get better understanding of the topic and see maybe a few examples of something similar to what i need to do for my assignment. any help you could give me would be nice. thanks

    i am still having trouble with this assignment. i got an online tutor from my school and he said i need to compare that the information entered is the same as what is saved but i am super confused by that. He tried to clear it up and kinda did but i am still lost. any way you could help clear it up more?

    Tuesday, October 27, 2015 9:10 PM
  • In option1 you write the entered name and password to the file. If a number of different names are used the file contains that number of lines.

    In option2 you enter a name and password. To check if these are in the file, you have to read all lines in the file compare the read names with that one you entered. If found you also have to compare the password found on the same line in the file with the given password. So if both match you have entered a valid name/password. 


    Jan [ WhTurner ] The Netherlands

    Wednesday, October 28, 2015 3:51 PM
    Answerer
  • In option1 you write the entered name and password to the file. If a number of different names are used the file contains that number of lines.

    In option2 you enter a name and password. To check if these are in the file, you have to read all lines in the file compare the read names with that one you entered. If found you also have to compare the password found on the same line in the file with the given password. So if both match you have entered a valid name/password. 


    Jan [ WhTurner ] The Netherlands

    ok i really need help with this step. Option 2 Requirements: This option uses the C:\CIS116Asn8\Your_Name.txt file to verify the user is entering a valid user name and password.  It begins by asking the user for a user name and password within a loop that runs up to three times.  If the password is correct, an appropriate message will be printed, the loop will end, and the program will go back to the main menu.  If the user is unable to provide a user name and password that matches any of the combinations in the file, an appropriate message will be printed and the user will be allowed to try again.  If the user fails after three attemps, the loop should end and the program should go back to the main menu.

    i did most of that stuff but i do not know how to print a message and how to make sure the program is checking the file. i am so confused and my assignment is do by 7.

    'Elizabeth Spencer
    'CIS 116 
    '10/24/15
    'Write a program that requires writing data to a file and reading data from a file.
    
    'parameters for the sub
    minValue = 1
    maxValue = 200
    minLength = 1
    maxLength = 200
    username = ""
    password = ""
    
    'this is where the file is stored to
    fileDirectory = "C:\CIS116Asn8\"
    fileNameOnly = "Elizabeth_Spencer.txt"
    fileName = fileDirectory + fileNameOnly
    
    File.CreateDirectory(fileDirectory)
    
    mainMenu()
    
    While menuOption <> 3
      If menuOption = 1 Then
      displayUsernamePassword()
    ElseIf menuOption = 2 then
      checkUsernamePassword()
      EndIf
      
    ' re-display the main menu and get a value for the next run through the loop
    mainMenu() 
    endWhile 
    
    'sub name: displayUsernamePassword
    'Displays the username and password
    'input: username and password 
    'output: username and password
    Sub displayUsernamePassword
      TextWindow.WriteLine("Enter username: ")
      username = TextWindow.Read()
     TextWindow.WriteLine("Enter password: ")
     password = TextWindow.Read()
     
      File.AppendContents(fileName, username)
      File.AppendContents(fileName, password)
      TextWindow.Pause()
      
     
    EndSub
    'sub name: addUsernamePassword
    'adds the username and password  to check
    'input: username and password
    'output: username and password
    
    Sub addUsernamePassword
    promptString = "Enter username: "
      minLength = 1
      maxLength = 200
      getString()
      username = returnValue
      
     promptString = "Enter password: "
      minLength = 1
      maxLength = 200
      getString()
      password = returnValue 
      
    EndSub
      
      'sub name: checkUsernamePassword
      'checks the username and password to what is saved in the file and makes sure they match
      'input: username and password. reads in the file
      'output: if username and password match or not
    
    Sub checkUsernamePassword
    
      ' Call the sub addUsernamePassword to get username and password
      addUsernamePassword()
    
      currentLine = 1
    
      usernameFromFile = File.ReadLine(fileName, currentLine)
      currentLine = currentLine + 1
      
       passwordFromFile = File.ReadLine(fileName, currentLine)
    
      
      While usernameFromFile <> ""
          ' username matches
          if username = usernameFromFile then
              if password = passwordFromFile then
              endif
          else
              ' username does not match
              currentLine = currentLine + 2
          endif
        EndWhile
            
     EndSub
    
    
    ' Sub Name:       mainMenu
    ' Sub Purpose:  Displays the main menu and reads the selected option from the user
    ' Inputs:              None
    ' Outputs:           menuOption - The value of the selected menu option
    '                                                         1 - Enter username and password
    '                                                         2 -  Check username and password
    '                                                         3 - Exit program
    
    Sub mainMenu
      TextWindow.Clear()
      TextWindow.WriteLine("1. enter username " + "enter password")
      TextWindow.WriteLine("2. check username and password")
      TextWindow.WriteLine("3. exit program")
      minValue = 1
      maxValue = 3
      promptString = "Select a menu option: "
      getInteger()
      
      menuOption = returnValue
     EndSub
    
    ' Sub Name:       getInteger
    ' Sub Purpose:  Returns an integer within a specified range. Prompts the user to
    '                             retry the input until a value in the range is entered. The user is
    '                             required to input an integer, and will be prompted to retry if
    '                             they input a decimal value (eg, '1' and '1.0' are valid, '1.1' is not)
    ' Inputs:              promptString - Will be shown to the user before waiting for input
    '                             minValue        - The lowest value in the valid range (inclusive)
    '                             maxValue       - The highest value in the valid range (inclusive)
    ' Outputs:            returnValue   - The value that the user entered. Guaranteed to fall
    '                                                          in the valid range.
    Sub getInteger
      TextWindow.Write(promptString)
      returnValue = TextWindow.ReadNumber()
      _integerValue = Math.Floor(returnValue)
      
      While returnValue < minValue Or returnValue > maxValue Or _integerValue <> returnValue
        ' Display an error message to the user detailing whether the number is invalid due to being a decimal or out of range
        If _integerValue <> returnValue Then
          TextWindow.WriteLine("You entered a decimal, but a whole number is required. Please enter a whole number between " + minValue + " and " + maxValue)
        else      
          TextWindow.WriteLine("The value you entered was outside of the allowed range. Please enter whole number between " + minValue + " and " + maxValue)
        EndIf
        
        ' Re-prompt the user and read in a new number
        TextWindow.Write(promptString)
        returnValue = TextWindow.ReadNumber()
        _integerValue = Math.Floor(returnValue)
      EndWhile
    EndSub  
    
    ' Sub Name:       getString
    ' Sub Purpose:  Returns a string that falls within a specified length range. Prompts
    '                             the user to retry the input until a string with a valid length is supplied.
    ' Inputs:              promptString - Will be shown to the user before waiting for input
    '                             minLength     - The shortest valid string (inclusive)
    '                             maxLength    - The longest valid string (inclusive)
    ' Outputs:            returnValue   - The string that the user entered. Guaranteed to fall
    '                                                          in the valid length range.
    Sub getString
      TextWindow.Write(promptString)
      returnValue = TextWindow.Read()
      stringLength = Text.GetLength(returnValue)
      
      While stringLength < minLength or stringLength > maxLength 
        ' The user entered a string that didn't meet requirements, so we need to get a new string from him/her
        TextWindow.WriteLine("The text you entered was not a valid length. Please enter between " + minLength + " and " + maxLength + " characters.")
           
        TextWindow.Write(promptString)
        returnValue = TextWindow.Read()
        stringLength = Text.GetLength(returnValue)
      EndWhile
    EndSub
     

    Wednesday, October 28, 2015 6:59 PM
  • Elizabeth,  with the following changes  (marked with '<<<<) in your subroutine "checkUsernamePassword"  it finds if there is a match in the file and shows OK.

    Adapt to your assignment ;-)

    Sub checkUsernamePassword
      
      ' Call the sub addUsernamePassword to get username and password
      addUsernamePassword()
      
      currentLine = 1
      
      usernameFromFile = File.ReadLine(fileName, currentLine)
      currentLine = currentLine + 1
      passwordFromFile = File.ReadLine(fileName, currentLine)
      
      While usernameFromFile <> ""
        ' username matches
        if username = usernameFromFile then
          if password = passwordFromFile then
            TextWindow.WriteLine("   O K ")                          '<<<<<<<<<<<
            usernamefromfile=""                                      '<<<< to end the while loop
            Program.Delay(3000)                                      ' <<<<<show OK for 3 secs and return to menu
          endif
        else
          ' username does not match
          currentLine = currentLine + 1                              '  <<< read next name
          usernameFromFile = File.ReadLine(fileName, currentLine)    '<<<<<<<
          currentLine = currentLine + 1                              '<<<< read next pw
          passwordFromFile = File.ReadLine(fileName, currentLine)    '<<<<<<<
        endif
      EndWhile
    EndSub


    Jan [ WhTurner ] The Netherlands

    Thursday, October 29, 2015 10:07 AM
    Answerer