943,696 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4010
  • C++ RSS
Nov 14th, 2008
0

Generate 6 Random Numbers

Expand Post »
Hi guys
I am working on a Lottery program to generate 6 unique numbers. So far I have the following:

CLASS LOOKS LIKE THIS:
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <string>
  3. #include <ctime>
  4. #pragma once
  5. #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
  6.  
  7. class Lottery // Lottery class definition
  8. {
  9. public:
  10. Lottery(); // constructor that initializes a Lottery object
  11. void setLottery(int []); // function that gets lottery numbers
  12. int getLottery(); // function that retrieve the lottery numbers
  13. bool checkLottery(int, int []); // function that evaluates the lottery numbers
  14. void displayLottery(int []); // function that displays the lottery numbers
  15.  
  16. private:
  17. int lotNumbers[6]; // lottery number
  18. }; // end of class Lottery
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. #include <string>
  9. #include <ctime>
  10. using std::string;
  11.  
  12. #include "lottery.h" // include definition of class lottery
  13.  
  14. // constructor for the Lottery
  15. Lottery::Lottery()
  16. {
  17.  
  18. } // end of Lottery constructor
  19.  
  20. bool Lottery::checkLottery(int lotnum, int lottery_numbers[6])
  21. {
  22. for (int i=0; i < 6; i++)
  23. {
  24. if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0)
  25. {
  26. return false;
  27. }
  28. }
  29. return true;
  30. }
  31. void Lottery::setLottery(int lottery_numbers[6])
  32. {
  33. lotNumbers[6] = lottery_numbers[6];
  34. }
  35.  
  36. int Lottery::getLottery()
  37. {
  38. return lotNumbers[6];
  39. int lot_count = 0; // counter for the number of valid lottery numbers found
  40. while (lot_count < 6)
  41. {
  42. time_t = t;
  43. srand(unsigned (time(&t)));
  44. int lotnum = (rand() * 53);
  45. if (checkLottery(lotnum, lotnumbers))
  46. {
  47. lotnumbers[lot_count] = lotnum;
  48. lot_count++;
  49. }
  50. }
  51. }
  52.  
  53. void Lottery::displayLottery()
  54. {
  55. getLottery();
  56. for (int j = 0; j < 6; j++)
  57. {
  58. cout << getLottery() << " ";
  59. }
  60. getche();
  61. }
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. #include <string>
  8. #include <ctime>
  9.  
  10. #include "lottery.h" // iinclude definition of class lottery
  11.  
  12. // function main begins program execution
  13.  
  14. int main()
  15. {
  16. int lottery_numbers[6];
  17.  
  18. Lottery lotteryDraw; //create Lottery object
  19.  
  20. lotteryDraw.displayLottery();
  21.  
  22. return 0; // indicate successful termination
  23.  
  24. } // end main function
I am getting the following errors and I do not understand where to go from here.
wrig_9.cpp
c:\users\jeannie\documents\visual studio 2005\projects\wrig_9\wrig_9\wrig_9.cpp(25) : error C2660: 'Lottery::displayLottery' : function does not take 0 arguments
lottery.cpp

c:\users\jeannie\documents\visual studio 2005\projects\wrig_9\wrig_9\lottery.cpp(47) : error C2513: '__time64_t' : no variable declared before '='
c:\users\jeannie\documents\visual studio 2005\projects\wrig_9\wrig_9\lottery.cpp(47) : error C2065: 't' : undeclared identifier
c:\users\jeannie\documents\visual studio 2005\projects\wrig_9\wrig_9\lottery.cpp(48) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Generating Code...
Build log was saved at "file://c:\Users\Jeannie\Documents\Visual Studio 2005\Projects\wrig_9\wrig_9\Debug\BuildLog.htm"
wrig_9 - 4 error(s), 0 warning(s)
====================================
I think the arrays are not declared properly and the calls are inaccurate.
Any help is appreciated. Please let me know what I am doing wrong.

Thanks so much
techgenie

===============
Last edited by Narue; Nov 14th, 2008 at 12:09 pm. Reason: added code tags
Reputation Points: 40
Solved Threads: 0
Newbie Poster
techgenie is offline Offline
8 posts
since Nov 2008
Nov 14th, 2008
1

Re: Generate 6 Random Numbers

>error C2660: 'Lottery::displayLottery' : function does not take 0 arguments
>void displayLottery(int []);
Looks like it takes one argument to me. You can't lie to your compiler, it doesn't work.

>error C2513: '__time64_t' : no variable declared before '='
>time_t = t;
Yep, this error is pretty straightforward too. You need to name your variables.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 14th, 2008
0

Re: Generate 6 Random Numbers

Why are you passing an array to displayLottery (in the declaration), it's not used in the method? Also, when you set the lottery you are only setting index # 6. And if you had properly passed your array from main it would be an out of range index (which will throw an exception).

Also, when you generate random numbers, use a mod (%) operator, not a * operator. I may be wrong, but I think it's possible for this to generate a int so big that it's bigger than an int's range. rand() % (MAX + 1) will generate a number between 0 and MAX, inclusive.
Last edited by skatamatic; Nov 14th, 2008 at 1:28 pm.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Nov 14th, 2008
0

Re: Generate 6 Random Numbers

Thank you skatamatic for that suggestion on the modulus (%). Narue I realized I had omitted the <ctime> and <cstdlib> libraries. After adding them it made it easier for me to identify the other errors.

As the program stands right now; it has finally compiled but I do not get random numbers between 1 and 53, as it should. I am getting an extremely large number out of range
(- 858993460). I get 6 of the same numbers.
Any suggestions?

The modified function definitions are:
C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. #include <string>
  9. using std::string;
  10.  
  11. #include <cstdlib>
  12. using std::rand;
  13. using std::srand;
  14.  
  15. #include <ctime>
  16. using std::time;
  17.  
  18. #include "lottery.h" // include definition of class lottery
  19.  
  20. // constructor for the Lottery
  21. Lottery::Lottery()
  22. {
  23.  
  24. } // end of Lottery constructor
  25.  
  26. bool Lottery::checkLottery(int lotnum, int lottery_numbers[6])
  27. {
  28.  
  29. for (int i=0; i < 6; i++)
  30. {
  31. lottery_numbers[i] = i * i; //store lottery numbers in the index
  32. if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0)
  33. {
  34. return false;
  35. }
  36. }
  37. return true;
  38. }
  39. void Lottery::setLottery(int lottery_numbers[6])
  40. {
  41. lotNumbers[6] = lottery_numbers[6];
  42. }
  43.  
  44. int Lottery::getLottery()
  45. {
  46. return lotNumbers[6];
  47. int lot_count = 0; // counter for the number of valid lottery numbers found
  48. while (lot_count < 6)
  49. {
  50. time_t t;
  51. srand(unsigned (time(&t)));
  52. int lotnum = 1 + rand() % 53;
  53. if (checkLottery(lotnum, lotNumbers))
  54. {
  55. lotNumbers[lot_count] = lotnum;
  56. lot_count++;
  57. }
  58. }
  59. }
  60.  
  61. void Lottery::displayLottery(int [])
  62. {
  63. getLottery();
  64. for (int j = 0; j < 6; j++)
  65. {
  66. cout << getLottery() << " " << endl;
  67. }
  68. }
Last edited by Narue; Nov 14th, 2008 at 2:35 pm. Reason: added code tags
Reputation Points: 40
Solved Threads: 0
Newbie Poster
techgenie is offline Offline
8 posts
since Nov 2008
Nov 14th, 2008
1

Re: Generate 6 Random Numbers

Adding code tags to other people's posts gets old very quickly. Please start using code tags when you post code.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 14th, 2008
1

Re: Generate 6 Random Numbers

C++ Syntax (Toggle Plain Text)
  1. using std::cout;
  2. using std::cin;
  3. using std::endl;
  4.  
  5. #include <string>
  6. using std::string;
  7.  
  8. #include <cstdlib>
  9. using std::rand;
  10. using std::srand;
  11.  
  12. #include <ctime>
  13. using std::time;
  14.  
  15. #include "lottery.h" // include definition of class lottery
  16.  
  17. // constructor for the Lottery
  18. Lottery::Lottery()
  19. {
  20.  
  21. } // end of Lottery constructor
  22.  
  23. bool Lottery::checkLottery(int lotnum, int lottery_numbers[6])
  24. {
  25.  
  26. for (int i=0; i < 6; i++)
  27. {
  28. lottery_numbers[i] = i * i; //store lottery numbers in the index
  29. if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0)
  30. {
  31. return false;
  32. }
  33. }
  34. return true;
  35. }
  36. void Lottery::setLottery(int lottery_numbers[6])
  37. {
  38. lotNumbers[6] = lottery_numbers[6];
  39. }
  40.  
  41. int Lottery::getLottery()
  42. {
  43. return lotNumbers[6];
  44. int lot_count = 0; // counter for the number of valid lottery numbers found
  45. while (lot_count < 6)
  46. {
  47. time_t t;
  48. srand(unsigned (time(&t)));
  49. int lotnum = 1 + rand() % 53;
  50. if (checkLottery(lotnum, lotNumbers))
  51. {
  52. lotNumbers[lot_count] = lotnum;
  53. lot_count++;
  54. }
  55. }
  56. }
  57.  
  58. void Lottery::displayLottery(int [])
  59. {
  60. getLottery();
  61. for (int j = 0; j < 6; j++)
  62. {
  63. cout << getLottery() << " " << endl;
  64. }
  65. }

Hope this helps!
Reputation Points: 40
Solved Threads: 0
Newbie Poster
techgenie is offline Offline
8 posts
since Nov 2008
Nov 14th, 2008
0

Re: Generate 6 Random Numbers

Why are you returning the 6th lottery number (again, out of range) in getLottery()? You are using arrays incorrectly. Each entry in the array needs to be accessed seperately. And what's with this line?
C++ Syntax (Toggle Plain Text)
  1. lottery_numbers[i] = i * i; //store lottery numbers in the
???
This program is pretty erronous and confusing. Fix up your syntax issues with your class methods, array passing, and loop techniques. Use google for syntax issues.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007
Nov 16th, 2008
0

Re: Generate 6 Random Numbers

I have been working on this program for quite some time and I have gotten it to compile and execute but I am getting a Debug error - Runtime Check Failure #2 Stack around the variable 'fllottery_numbers was corrupted.
the only function that has 'fllottery_numbers is the function below.
Can anyone help with resolving this issue.
Thanks for your time and patience.

C++ Syntax (Toggle Plain Text)
  1. void Lottery::displayLottery()
  2. {
  3. int fllottery_numbers[6];
  4. flLotteryDraw(fllottery_numbers);
  5.  
  6. for (int j = 0; j < 6; j++)
  7. {
  8. cout << fllottery_numbers[j] << " " << endl;
  9. }
  10. }
Reputation Points: 40
Solved Threads: 0
Newbie Poster
techgenie is offline Offline
8 posts
since Nov 2008
Nov 16th, 2008
0

Re: Generate 6 Random Numbers

What is flLotterDraw doing? It's not in the previous code. Whatever it's doing, it's corrupting your array. Post that function.
Reputation Points: 352
Solved Threads: 109
Master Poster
skatamatic is offline Offline
775 posts
since Nov 2007

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: Working with mp3 files and folders.
Next Thread in C++ Forum Timeline: Homework - Array/Functions





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


Follow us on Twitter


© 2011 DaniWeb® LLC