| | |
Generate 6 Random Numbers
![]() |
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
Hi guys
I am working on a Lottery program to generate 6 unique numbers. So far I have the following:
CLASS LOOKS LIKE THIS:
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
===============
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)
#include "stdafx.h" #include <string> #include <ctime> #pragma once #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers class Lottery // Lottery class definition { public: Lottery(); // constructor that initializes a Lottery object void setLottery(int []); // function that gets lottery numbers int getLottery(); // function that retrieve the lottery numbers bool checkLottery(int, int []); // function that evaluates the lottery numbers void displayLottery(int []); // function that displays the lottery numbers private: int lotNumbers[6]; // lottery number }; // end of class Lottery
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> #include <ctime> using std::string; #include "lottery.h" // include definition of class lottery // constructor for the Lottery Lottery::Lottery() { } // end of Lottery constructor bool Lottery::checkLottery(int lotnum, int lottery_numbers[6]) { for (int i=0; i < 6; i++) { if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0) { return false; } } return true; } void Lottery::setLottery(int lottery_numbers[6]) { lotNumbers[6] = lottery_numbers[6]; } int Lottery::getLottery() { return lotNumbers[6]; int lot_count = 0; // counter for the number of valid lottery numbers found while (lot_count < 6) { time_t = t; srand(unsigned (time(&t))); int lotnum = (rand() * 53); if (checkLottery(lotnum, lotnumbers)) { lotnumbers[lot_count] = lotnum; lot_count++; } } } void Lottery::displayLottery() { getLottery(); for (int j = 0; j < 6; j++) { cout << getLottery() << " "; } getche(); }
C++ Syntax (Toggle Plain Text)
#include "stdafx.h" #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> #include <ctime> #include "lottery.h" // iinclude definition of class lottery // function main begins program execution int main() { int lottery_numbers[6]; Lottery lotteryDraw; //create Lottery object lotteryDraw.displayLottery(); return 0; // indicate successful termination } // end main function
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
>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.
>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.
I'm here to prove you wrong.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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.
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.
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
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:
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)
#include "stdafx.h" #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; #include <cstdlib> using std::rand; using std::srand; #include <ctime> using std::time; #include "lottery.h" // include definition of class lottery // constructor for the Lottery Lottery::Lottery() { } // end of Lottery constructor bool Lottery::checkLottery(int lotnum, int lottery_numbers[6]) { for (int i=0; i < 6; i++) { lottery_numbers[i] = i * i; //store lottery numbers in the index if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0) { return false; } } return true; } void Lottery::setLottery(int lottery_numbers[6]) { lotNumbers[6] = lottery_numbers[6]; } int Lottery::getLottery() { return lotNumbers[6]; int lot_count = 0; // counter for the number of valid lottery numbers found while (lot_count < 6) { time_t t; srand(unsigned (time(&t))); int lotnum = 1 + rand() % 53; if (checkLottery(lotnum, lotNumbers)) { lotNumbers[lot_count] = lotnum; lot_count++; } } } void Lottery::displayLottery(int []) { getLottery(); for (int j = 0; j < 6; j++) { cout << getLottery() << " " << endl; } }
Last edited by Narue; Nov 14th, 2008 at 2:35 pm. Reason: added code tags
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
using std::cout; using std::cin; using std::endl; #include <string> using std::string; #include <cstdlib> using std::rand; using std::srand; #include <ctime> using std::time; #include "lottery.h" // include definition of class lottery // constructor for the Lottery Lottery::Lottery() { } // end of Lottery constructor bool Lottery::checkLottery(int lotnum, int lottery_numbers[6]) { for (int i=0; i < 6; i++) { lottery_numbers[i] = i * i; //store lottery numbers in the index if (lottery_numbers[i] == lotnum || lottery_numbers[i] == 0) { return false; } } return true; } void Lottery::setLottery(int lottery_numbers[6]) { lotNumbers[6] = lottery_numbers[6]; } int Lottery::getLottery() { return lotNumbers[6]; int lot_count = 0; // counter for the number of valid lottery numbers found while (lot_count < 6) { time_t t; srand(unsigned (time(&t))); int lotnum = 1 + rand() % 53; if (checkLottery(lotnum, lotNumbers)) { lotNumbers[lot_count] = lotnum; lot_count++; } } } void Lottery::displayLottery(int []) { getLottery(); for (int j = 0; j < 6; j++) { cout << getLottery() << " " << endl; } }
Hope this helps!
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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?
???
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Nov 2008
Posts: 8
Reputation:
Solved Threads: 0
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.
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)
void Lottery::displayLottery() { int fllottery_numbers[6]; flLotteryDraw(fllottery_numbers); for (int j = 0; j < 6; j++) { cout << fllottery_numbers[j] << " " << endl; } }
![]() |
Similar Threads
- program to generate random numbers for a given range (C++)
- how to use generate random numbers (Visual Basic 4 / 5 / 6)
- Program will not generate random numbers (C++)
- need help, need to generate 10 random numbers, such that their sum is less than 1 (C)
- Trying to find a better way to generate random numbers in Dev Pascal (Pascal and Delphi)
- generate random question (C)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Working with mp3 files and folders.
- Next Thread: Homework - Array/Functions
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code codesamplerunwhilecommands coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph guess gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker listing loop looping loops map math matrix memory multiple news node output pointer port problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






