943,603 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1964
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 24th, 2008
0

Lover Game, Remember Names

Expand Post »
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?

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. char firstLover[20];
  10. char secondLover[20];
  11.  
  12. unsigned int lovePercent;
  13.  
  14. cout << "Enter The First Lovers Name: ";
  15. cin >> firstLover;
  16.  
  17. cout << endl << "Enter The Second Lovers Name: ";
  18. cin >> secondLover;
  19.  
  20. srand ( time(NULL) );
  21.  
  22. lovePercent = rand() % 100 + 1;
  23.  
  24. cout << endl << "Your Love Percentage Is : " << lovePercent << "%!";
  25.  
  26. getch();
  27. }

Please reply with help, A.S.A.P
Last edited by Black Magic; Apr 24th, 2008 at 4:43 pm.
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 24th, 2008
0

Re: Lover Game, Remember Names

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.
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Apr 24th, 2008
0

Re: Lover Game, Remember 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!
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 24th, 2008
0

Re: Lover Game, Remember Names

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!
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Apr 24th, 2008
1

Re: Lover Game, Remember Names

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.
Last edited by hammerhead; Apr 24th, 2008 at 5:12 pm.
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Apr 24th, 2008
0

Re: Lover Game, Remember Names

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!
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Apr 24th, 2008
0

Re: Lover Game, Remember Names

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?
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 24th, 2008
0

Re: Lover Game, Remember Names

@majestic0110
Random numbers come after applying the formula, it is however only to fool the end user . Lets say for example that I get result as '5' after application of the formula. Now I can use
C++ Syntax (Toggle Plain Text)
  1. if(result==5)
  2. {
  3. lovePercent=rand()%100+70;
  4. }
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.
Last edited by hammerhead; Apr 24th, 2008 at 5:35 pm.
Reputation Points: 46
Solved Threads: 24
Posting Whiz in Training
hammerhead is offline Offline
248 posts
since May 2006
Apr 24th, 2008
0

Re: Lover Game, Remember Names

ok, thank you, ive decided to do whitch ever strategy is most effecdent, so would it be hammers idea?
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Apr 24th, 2008
0

Re: Lover Game, Remember Names

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.

cpp Syntax (Toggle Plain Text)
  1. unsigned int LetterToInt(char letter)
  2. {
  3. unsigned int i = (unsigned int)letter;
  4. if (i >= 65 && i <= 90)
  5. {
  6. i += 32;
  7. }
  8. return i;
  9. }
  10.  
  11. unsigned int NameToInt(char* name)
  12. {
  13. int i = 0;
  14. unsigned int j = 0;
  15.  
  16. while (name[i] != 0)
  17. {
  18. j += (LetterToInt(name[i]) * (i+1));
  19. i++;
  20. }
  21. return j;
  22. }
Reputation Points: 13
Solved Threads: 7
Light Poster
tymk is offline Offline
35 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Error when evaluate some Postfix
Next Thread in C++ Forum Timeline: If i was to make a 2 player game what would i use?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC