How do I allow data requested by a user to be printed using arrays in free-BASIC?

92 Views Asked by At

For this problem I need to have the user input a number and in return receive the Name, current salary and new salary of the teacher. I've tried something like this

Dim Names(10)as String
Dim Yos(10) as Integer
Dim Sal(10) as Integer
Dim Nsal(10) as Integer
Dim Rate(10) as Integer
Teachers = 2
Teacher_Num = 0
Dim Number as Integer
Dim Answer as String

PRINT " Congrats! 10 of you have been chosen to receive a raise in your salary :D. Please follow instructions below :)."

For Count = 1 To Teachers

    Teacher_Num = Teacher_Num + 1
    Print "Your number is:",Teacher_Num
    Input " Nice to meet you! What's your name? :D ",Names(10)
    Input " How many years have you serve here :O?",Yos(10)
    Input "What is your current Salary?",Sal(10)

IF Sal(10) > 1 then

    Rate(10)= (Yos(10) * 2) +100
    Nsal(10) = Rate(10) * (Sal(10))/100
    Print " Your name is: ",Names(10)
    Print " Your Previous Salary was: ",Sal(10)
    Print " Your New Salary is: ",Nsal(10)
    Print " Thank you for your time :D, Please allow the next user to begin. (*Dear new user, Please press enter to begin*)"
End If
If Count = Teachers Then

    Input "Would you like to see a specific name and current salary of a teacher or yourself?",Answer   
If Answer = "Yes" or Answer = "yes" Then

    Input "Please input the Teacher's Number",Number
    Print "Here's your information: ",Names(Number),Sal(Number),Nsal(Number) `Else`
    If Answer = "No" OR Answer = "no" Then
      End If
End If
End If
Sleep
Next
End

But it doesn't work (The link below shows the error) Please someone help! This is for an Sba...My teacher wanted my class to input grades a while ago but no one knows how to do this so know one submitted... I've been trying to do this for days but we were never taught much about arrays :(

2

There are 2 best solutions below

1
Joe On

The first problem is that Names(10), Rate(10), etc. should be Names(Count), Rate(Count), etc.

As Count goes from 1 to 10, you want to be using that slot in the arrays. Each time your FOR...NEXT block loops, Names(Count) will resolve to Names(1), Names(2), Names(3) ... Names(10).

Second Issue

Yos, Sal, Nsal, and Rate should be defined as Double instead of Integer. Integers won't calculate decimals and so you'll lose precision when calculating the new salary (If I enter 30000 as my salary for one year, it shows -202 instead of 30600). If you intend to round to the nearest dollar, then use doubles for precision and CINT() to round to the nearest integer:

Dim Yos(10) as Double
Dim Sal(10) as Double
Dim Nsal(10) as Double
Dim Rate(10) as Double
...
Rate(Count) = (Yos(Count) * 2) + 100
Nsal(Count) = CINT(Rate(Count) * (Sal(Count)) / 100)
0
ExagonX On

First of all you have to initialize all the variables and arrays. for what you want to do the FOR loop alone is not suitable Copy this source code and compile it for learn how work

If you dont understand somethink feel free to ask

'  Declare it is not indispensable but useful if you want to know how many and which SUB and FUNCTION are inside the program when the code is long.
Declare SUB Salary()  

Sub Salary()
    ' We make a multi dimensional array is more usefull 
    Dim as String Identity(10,5) 

    ' We initialize the variables by giving a value that we will use as an index
    Dim as Integer Teachernum = 1, NameTeacher = 2, YearOfServe = 3, CurrentSalary = 4, FutureSalary = 5

    ' We initialize a string variable to use as a prompt
    Dim As String PromptWord
    Dim As integer InsValue = 0 ' Initialize a var to use inside FOR Loop
    Dim As Integer TheExit = 0 ' Initialize a TheExit Var to use for the loop 
    Dim As Integer InsData = 1 ' You need for control the inser data
    Dim As String TeacherFound = "" ' You Need this var for print message status during search
    PRINT " Congrats! 10 of you have been chosen to receive a raise in your salary :D. Please follow instructions below :)."

    Do Until (TheExit = 1) 'make a loop while the variable TheExit is not equal to 1




        If InsData = 1 Then 'This start the data insert  if = 1

        For InsValue = 1 To 10 Step 1

         Identity(InsValue, Teachernum) = str(InsValue) ' Put in the array the number of teacher where the Index is same of loop step
         Print "Your number is:" & Identity(InsValue, Teachernum) 'We use the & symbol to join strings without spaces
         Line Input " Nice to meet you! What's your name? :D "; Identity(InsValue, NameTeacher) 'Put the name of teacher 
         Line Input " How many years have you serve here :O ?"; Identity(InsValue, YearOfServe) 'punt the year o service
         Line Input " What is your current Salary?"; Identity(InsValue, CurrentSalary) 'put current salary


            IF Val(Identity(InsValue, CurrentSalary)) > 1 then  'VAL converts a string into an integer value that can be calculated 

            ' With STR and VAL can calculate the value inside String array
            ' STR Convert a Integer to String
            ' VAL Conver a string to Integer

            ' I use this method to not initialize too many different types of variables
            Identity(InsValue, FutureSalary) = Str( ((Val(Identity(InsValue, YearOfServe)) * 2) +100) * Val(Identity(InsValue, CurrentSalary)) / 100)

            ' FutureSalary = ( (YearOfService * 2 ) +100 ) * CurrentSalary / 100 (If is this you want calculate
            ' Rate(10)= (Yos(10) * 2) +100
            ' Nsal(10) = Rate(10) * (Sal(10))/100

                Print " Your name is: " & Identity(InsValue,NameTeacher)
                Print " Your Previous Salary was: " & Identity(InsValue, CurrentSalary)
                Print " Your New Salary is: " & Identity(InsValue,FutureSalary)
                Print " Thank you for your time :D, Please allow the next user to begin. (*Dear new user, Please press enter to begin*)"

            End If

         Line Input " Press Enter for continue... ";  PromptWord 'Ask for enter for jump to the next





        Next InsValue
        InsData = 0 ' When loop this dont stat the Insert data
    End if
    'Finish Loop now the check  ask if you want check the data inserted 
    Line Input "Would you like to see a specific name and current salary of a teacher or yourself ? Yes / No "; PromptWord
    TeacherFound = "" 'Reset the var

    If UCASE(PromptWord) = "YES"  Then  ' UCASE UCASE It transforms the string into capital letters so if you write with upper and lower case letters it does not differ
        Line Input "Please input the Teacher's Number or Name"; PromptWord
        For InsValue = 1 To 10 Step 1
            If ( Val(PromptWord) = Val(Identity(InsValue, Teachernum))) Or ( UCASE(PromptWord) = UCASE(Identity(InsValue,NameTeacher)) )Then 
                Print "Teacher Found num:" & Identity(InsValue, Teachernum)
                Print "Teacher Name:" &  Identity(InsValue,NameTeacher)
                Print "Previous Salary was: " & Identity(InsValue, CurrentSalary)
                Print "New Salary is: " & Identity(InsValue,FutureSalary)
                TeacherFound = "Found"
            Else
                TeacherFound = TeacherFound & "" 'Add a empty value each round
            End If

        Next InsValue 
        If TeacherFound = "" Then Print "No Teacher Found !" 
    Else
        Print "The Program is Done Press Enter for exit"
        Line Input PromptWord
        TheExit = 1 ' Make end to the loop Do until
    End If

    Loop

End Sub

' Call the Sub Salary like a command

Salary()