Creating a Love Calculator in C++

Creating Love Calculators in different ways to trick my friends was one of my hobbies. Here i’ll explain one method of doing it using C++. I’ll explain the basic logic and you’ll do the decorations and modifications.


How to Calculate

I’ll explain it with an example. Take  2 names Leo John and Mary Mathew. We’ll start by calculating the number of occurrences of each letters L, O, V, E, S in both names.

no. of occurrences L = 1 + 0 = 1
no. of occurrences O = 2 + 0 = 2
no. of occurrences V = 0 + 0 = 0
no. of occurrences E = 1 + 1 = 2
no. of occurrences S = 0 + 0 = 0

Now write down the numbers in order

1,  2,  0, 2,  0

Now we should add each adjacent numbers to get the next corresponding number. ie, the next number will be

1+2, 2+0, 0+2, 2+0 = 3, 2, 2, 2

Again next step,
3+2, 2+2, 2+2 = 5, 4, 4
5+4, 4+4 = 9, 8

Now we’ve got a number between 0 and 100. So that’s it! We’ve got the percentage of love, 98 %. In some cases we may get a number which adds up to get a 2 digit number. For example,

VineeshKumar and Shreya Ghoshal
1, 1, 1, 3, 3
2, 2, 4, 6
4, 6, 10 (Oh no.. It wrong, Write it as 4, 6, 1, 0)
4, 6, 1, 0
10, 7, 1 (We change it to 1, 0, 7, 1)
1, 0, 7, 1
1, 7, 8
8, 15
8, 1, 5
9, 6

So we’ve got 96 %. We’ll now continue to our program

Implementation

To implement the same in C++, First we add 2 functions. 

1. countLetter(char c, char *s)
       Returns the number of occurrences of character c in string s. 

2. long findNextNumber(long num)
       Returns the next number in the series. for example, If we pass 123 in to it, it gives back 35, after adding the adjacent numbers. Here are the steps we did to calculate the ‘next number’ of 12681

  • separate the digits of 12681 and store it in an array. Thus we get an array 1 8 6 2 1
  • Set newNumber = 0
  • Our array is in the reverse order, so we loop from last digit to first digit.
  • We check whether the sum of current number and next number is greater than 9, if so we multiply it by 100, else we multiply by 10 and then add the sum of 2 numbers to it.
Here is order of execution
  • newNumber = 0
  • newNumber = (0 * 10) + (1 + 2) = 3
  • newNumber = (3 * 10) + (2 + 6) = 38
  • newNumber = (38 * 100) + (6 + 8) = 3814 (multiplied by 100 because 6+8 is > 9)
  • newNumber = (3814 * 10) + (8 + 1) = 38149
3. Main function

       Now we’ll read 2 strings name1 and name2. Then we’ll calculate the first number by joining the counts of letters in ‘LOVES’ using the above logic. After getting the first number, we continue finding the next number until we get a number less than or equal to 100. and that’s it. We get the result.

 
Here is the program:
 


A little words more..

  • One common error is case-sensitivity. The user may enter uppercase and lowercase letters. In our program we’ve converted all letters to lowercase before counting, and have done it in countLetter function.
  • The number of letters is limited to 50. Try implementing dynamic allocation instead, so that user can enter any number of letters.
  • One other error that may occur, only in case user tries to test our program, if count of any of the letter LOVES in both names is greater than 99 i.e., a 3 digit number, we should multiply the previous number by 1000 instead of 100 in lines 22 to 29.

Leave a Reply

Your email address will not be published. Required fields are marked *