A love calculator is program that calculates love percentage or compatibility between two couples on the basis of their name and date of birth. In this tutorial I am going to teach you how to create a love calculator using visual basic, VB6.
Earlier I had created a love calculator using C++ that was a little bit complicated and was not much attractive but this time we are going to create a real love calculator in VB that will be easy to code and will be more attractive than previous one.
The love calculator we are going to create in visual basic will ask the user for two names and their date of birth, on the basis of all these info our program will calculate love percentage.
[codesyntax lang=”vb” title=”Source Code: Love Calculator Code in Visual Basic, VB” bookmarkname=”Love Calculator Code in Visual Basic, VB”]
Dim lovesum As Integer Dim he, she As String Private Sub calculate_Click() he = you.Text she = crush.Text ‘converting string characters into ASCII values For i = 1 To Len(he) lovesum = lovesum + Asc(Mid$(he, i, 1)) Next For i = 1 To Len(she) lovesum = lovesum + Asc(Mid$(she, i, 1)) Next lovesum = lovesum + day1.ListIndex + day2.ListIndex + mon1.ListIndex + mon2.ListIndex + year1.ListIndex + year2.ListIndex + 6 ‘calculating love percentage result.Caption = lovesum Mod 101 result.Caption = result.Caption + "%" End Sub Private Sub Form_Load() Form1.Show For i = 1 To 31 day1.AddItem i day2.AddItem i Next For i = 1 To 12 mon1.AddItem i mon2.AddItem i Next For i = 1910 To 2020 year1.AddItem i year2.AddItem i Next End Sub[/codesyntax]
Explaining the Working of Program (Love Calculator)
First of all, we have to declare three variables that are enough for this love calculator. String variable he and she stores the name of couple and lovesum stores an integer value that is the sum of all the information that user provides.
In calculate_Click() function, first values of he and she will be entered by user then both the names are converted into their ASCII values (character by character) one after another and sum of ASCII values are stored in lovesum integer variable. Then dates (date of birth) are also added to lovesum (if provided).
Love calculator calculates the love in percentage and it must be less then or equal to 100 but our lovesum integer variable stores a big number so we have to reduce it to a range between 0 to 100 by dividing it with 101 and using the remainder of division as the result or love percentage that will be surely less than 101.
Explaining the VB Code
The coding is self explanatory if you know a little about visual basic but for the sake of all I will explain a little bit. I had used comments in code so not a big deal to figure out this VB code. The only working thing (or problem) is the converting of names (string) into ASCII (integer) so let me explain the code.
For i = 1 To Len(he)
lovesum = lovesum + Asc(Mid$(he, i, 1))
The above VB6 code is of for-next loop that begins with a value of i equal to 1 and loops till the complete name (string) is converted to ASCII (integer). For calculating the length of string (or total no. of required loops) we had used ‘Len’ function.
Then each time the code inside loop is executed it converts 1 character of name/he (string) into ASCII value and stores that ASCII value into lovesum variable using ‘Asc’ function until the complete string is converted into ASCII value. If you didn’t understand the working then you should consider reading about ‘Asc’ function and its working!
Now I expect that you’ll create a better love calculator than me as it was just for demo for you so that you can understand the functioning and core logic behind it. You can easily add the rest of the features yourself. Good Luck!