Hey,
I was just mucking around and made a simple random number generator and made a "love percentage game" but, if you re-open the .exe and type the same names in it gives another completely different percentage, would i save the names to do a file or some kind?

#include <iostream>
#include <conio.h>
#include <time.h>

using namespace std;

int main()
{
    char firstLover[20];
    char secondLover[20];
    
    unsigned int lovePercent;
    
    cout << "Enter The First Lovers Name: ";
    cin  >> firstLover;
    
    cout << endl << "Enter The Second Lovers Name: ";
    cin  >> secondLover;
    
    srand ( time(NULL) );
    
    lovePercent = rand() % 100 + 1;
    
    cout << endl << "Your Love Percentage Is : " << lovePercent << "%!";
    
    getch();
}

Please reply with help, A.S.A.P :)

Recommended Answers

All 15 Replies

The program is correct. There is no relation between the names and the love percentage and thus the program only displays random numbers between 0 to 100.

You may want to use some relationship between names and random number generation in order to get a similar percentage for a given set of names.

I get you, but not how to do that,

Could you please post a little example and then i would work on it from that

Thank you!

I guess that you could store the results in an external text file, then when two matching names are entered, check if they exist on that text file and then print out a corresponding score for that pair. Of course, the pairs are not stored in the text file until after they have been entered into the game at least once. This method seems a little 'clunky' though so maybe someone might come up with something better. Hope this helps!

That depends on how you want the love percentage to be calculated and if you want it to be the same for a given set of names. For example you can say that the love percentage will be higher if the two names are similar.

Now you can define 'similar' in your own manner, for example two names can be similar if
1. They have the same length or
2. They have same vowels or
3. Their edit distance is minimum

etc etc. After that you can threshold or use a formula to relate probability to the names. For example if the difference in length of the names is 2 then percentage will be between 0-30, if it is 1 then it will be between 30-60 and if the length is same then the percentage will be between 60-100.

commented: Good idea ! +2

That is a very good idea Hammerhead, only trouble is that it is not truly random if there are formulas involved but that might not matter to OP. Nice plan, that approach did not occur to me!

i like hammers idea but i think im going to go for maj's solution, how would i save the names to file? i know how to write like this is a line etc, would i just do that? oh and how would i pair the two names tomorrow?

@majestic0110
Random numbers come after applying the formula, it is however only to fool the end user :D. Lets say for example that I get result as '5' after application of the formula. Now I can use

if(result==5)
{
lovePercent=rand()%100+70;
}

by doing so I will still get variable percentage. I can make similar cases for result==4,3,2 and so on.

@Black Magic
My file handling is weak, but you can make a structure of two strings(names) and then use read(), write() functions of fstream to read/write the structure. I suggest you to wait for someone more experienced to reply.

ok, thank you, ive decided to do whitch ever strategy is most effecdent, so would it be hammers idea?

I came up with these helper functions to convert a name to a number. Even with different capitalization, the same name will be the same number each time. You can then add these numbers together and use the result to seed the random number generator or use the numbers in some other way to come up with your percentage.

unsigned int LetterToInt(char letter)
{
	unsigned int i = (unsigned int)letter;
	if (i >= 65 && i <= 90)
	{
		i += 32;
	}
	return i;
}

unsigned int NameToInt(char* name)
{
	int i = 0;
	unsigned int j = 0;
	
	while (name[i] != 0)
	{
		j += (LetterToInt(name[i]) * (i+1));
                i++;
	}
	return j;
}

Im not exactly sure if this would work, but couldn't you just use srand() to ensure that results of same names come out the same. Like use the length of both strings combined instead of time(NULL). not sure if this will work though.

Hello, I have decided to save the names to file etc, thanks to help from my other post i have gotten this code

#include <time.h>
#include <conio.h>
#include <fstream>
#include <iostream>


using namespace std;

int main()
{
    ofstream myfile;
    
    char firstLover[20];
    char secondLover[20];
    
    unsigned int lovePercent;
    
    cout << "Enter The First Lovers Name: ";
    cin  >> firstLover;
    
    cout << endl << "Enter The Second Lovers Name: ";
    cin  >> secondLover;
    
    srand ( time(NULL) );
    
    lovePercent = rand() % 100 + 1;
    
    cout << endl << "Your Love Percentage Is : " << lovePercent << "%!";
    
    myfile.open ("lover_names.txt", ios::ate | ios::app);
    myfile << firstLover << " " << secondLover << " " << lovePercent << "%\n";
    myfile.close();
    
    getch();
}

Now, I don't know how i am going to make it so the compiler can see if two names are the same, and if so, give the same percentage, help please, thanks

Can someone please help??

Okay one way to go about this would be using getline() to get the entire line. Use any delimiter to split the line into three substrings, namely firstLover, secondLover and lovePercent. Compare these values with the input provided by the user and display them accordingly.

Of course you will need to change the way you save in the file as

myfile << firstLover << delim << secondLover << delim<< lovePercent <<delim<< "%\n";

Thanks, but the real confusion is how i would select the answer if the names are inputted again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.