| | |
Anyone can do this in C++
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
First thing I'd do is calculate the random value first. This way you have the option of asking "Do you want to try another number?"
Next thing, rather than asking for each individual number (since you are only using 0-9) have the user enter a 5 digit value.
I'll let you work out the syntax for your program, but an array is just a variable that can contain multiple values. For example:
defines the variable ary to contain 5 values, ary[0] thru ary[4]. So in your code,
ary[0] would be a
ary[1] would be b
ary[2] would be c
ary[3] would be d
ary[4] would be e
Next thing, rather than asking for each individual number (since you are only using 0-9) have the user enter a 5 digit value.
I'll let you work out the syntax for your program, but an array is just a variable that can contain multiple values. For example:
int ary[5]defines the variable ary to contain 5 values, ary[0] thru ary[4]. So in your code,
ary[0] would be a
ary[1] would be b
ary[2] would be c
ary[3] would be d
ary[4] would be e
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jun 2007
Posts: 33
Reputation:
Solved Threads: 1
One of your requirement was "....and keep a count of the digits that match .... "
but your code is silent on this.
I suggest you can use a simple int which is initialized at start and would be incremented in the " if part of comparison ( a == A)" of the code.
The value of the variable after all such ifs ( i.e ..e == E )
would be value of digits that are same and the same can be printed.
Further ... knowledge of arrarys that definitely simplify the code.
I suggest you to refer to K & R - the C programming text - concept wise it is the best and price wise affordable aswell.
you can declare a single dimensional array as
int lottery [5] // it can hold only 5 values.
int user[5]
and for receiving the values
int i = 0;
while ( i < 5) {
cin >> lottery[i]
i++;
}
... like wise for user array and now you tell me on the comparison logic using arrays.
Look out for info at the following link http://www.daniweb.com/forums/thread50370.html
at one stage the info contained was of great help to me.
but your code is silent on this.
I suggest you can use a simple int which is initialized at start and would be incremented in the " if part of comparison ( a == A)" of the code.
The value of the variable after all such ifs ( i.e ..e == E )
would be value of digits that are same and the same can be printed.
Further ... knowledge of arrarys that definitely simplify the code.
I suggest you to refer to K & R - the C programming text - concept wise it is the best and price wise affordable aswell.
you can declare a single dimensional array as
int lottery [5] // it can hold only 5 values.
int user[5]
and for receiving the values
int i = 0;
while ( i < 5) {
cin >> lottery[i]
i++;
}
... like wise for user array and now you tell me on the comparison logic using arrays.
Look out for info at the following link http://www.daniweb.com/forums/thread50370.html
at one stage the info contained was of great help to me.
Last edited by bvgsrs; Jun 26th, 2007 at 4:23 am.
See now we know what you don't know.. 
See this link to learn teh abt arrays in C++. It tells you:
- What is an array?
- How create an array ?
- How to initialize an array ?
- How to access elements of an array ?
- Finally multidimensional arrays are described if you're interested.

See this link to learn teh abt arrays in C++. It tells you:
- What is an array?
- How create an array ?
- How to initialize an array ?
- How to access elements of an array ?
- Finally multidimensional arrays are described if you're interested.
Are you Agile.. ?
Wow! That's some scary looking code you've cooked up!
Why don't you grab a copy of the c++ primer or some book like it. Go through it completely before attempting something like this. I think you are just going to confuse the hell out of yourself if you keep going in this way.
Just a tip from another code neophyte...
Here's a snippet to get the input into an array:
int arr[5];
int i =0;
int x;
while (i<5)
{
cin >> x;
arr[i] = x;
i++
}
Why don't you grab a copy of the c++ primer or some book like it. Go through it completely before attempting something like this. I think you are just going to confuse the hell out of yourself if you keep going in this way.
Just a tip from another code neophyte...
Here's a snippet to get the input into an array:
int arr[5];
int i =0;
int x;
while (i<5)
{
cin >> x;
arr[i] = x;
i++
}
•
•
Join Date: Jun 2007
Posts: 18
Reputation:
Solved Threads: 0
this is my final program...
C++ Syntax (Toggle Plain Text)
#include<iostream> using std::cout; using std::cin; using std::endl; #include <cstdlib> #include <ctime> int main() { int wilson; const int Size = 5; int lottery[Size]; int wilsonArray[Size]; int frequency=0; cout << "Please Enter 5 Number of Your Prediction?" << endl; cin >> wilson; while (wilson < 0 || wilson > 99999) { cout << "Invalid Number Enter" << endl << "Please Re-enter 5 Number of Your Prediction?" << endl; cin >> wilson; } if( wilson >= 0 && wilson < 100000) { wilsonArray[0]=wilson/10000; wilsonArray[1]=wilson%10000/1000; wilsonArray[2]=wilson%1000/100; wilsonArray[3]=wilson%100/10; wilsonArray[4]=wilson%10; cout << "\nWilson Prediction" << endl; cout << " *********************" << endl << " | "; for(int i = 0; i < 5; i++) cout << wilsonArray[i] << " | "; cout << "\n *********************" << endl; srand(time(0)); cout << "\nLottery Result"<< endl; cout << " *********************" << endl << " | "; for (int j = 0; j < 5; j++) { lottery[j] = rand() % 10; cout << lottery[j] << " | "; } cout << "\n *********************" << endl; for (int k = 0; k < 5; k++) if(wilsonArray[k] == lottery[k]) ++frequency; if(frequency == 0 ) cout << "\nThere are none of your prediction that matches" << endl; if(frequency == 1 ) cout << "\nThere are one number of your prediction that matches" << endl; if(frequency == 2 ) cout << "\nThere are two number of your prediction that matches" << endl; if(frequency == 3 ) cout << "\nThere are three number of your prediction that matches" << endl; if(frequency == 4 ) cout << "\nThere are four number of your prediction that matches" << endl; if(frequency == 5 ) cout << "\n***************************************************" << endl << "| Congratulation!! All of Your Prediction Matches |" << endl<< "***************************************************" << endl; } return 0; }
Last edited by ~s.o.s~; Jun 29th, 2007 at 5:47 am. Reason: Added code tags, learn to use them.
•
•
Join Date: Jun 2007
Posts: 33
Reputation:
Solved Threads: 1
Its really gr8 ... that you have come up with a this fine code during your initial attempts.
I have a few suggestions:
instead of :
you can also use :
also the frequency can be given in a single statement as :
pl. do correct me if I am wrong.
Thanks and Regards
Ram Sharma
I have a few suggestions:
instead of :
wilsonArray[0]=wilson/10000;
wilsonArray[1]=wilson%10000/1000; wilsonArray[2]=wilson%1000/100; wilsonArray[3]=wilson%100/10; wilsonArray[4]=wilson%10;
you can also use :
int i=0;
while (n > 0) {
int a = n%10;
wilsonArray[i++] =a;
n = n/10;
also the frequency can be given in a single statement as :
if ( frequency < 5) {
cout << "\nThere are" << frequency << " number of your prediction that matches" << endl;
}
else if (frequency == 5) {
Congratulation!! All of Your Prediction Matches |" <<
}
pl. do correct me if I am wrong.
Thanks and Regards
Ram Sharma
Last edited by bvgsrs; Jun 29th, 2007 at 6:59 am.
Since the frequency is initialized with 0 and can hold a maximum value of 5, we can also write:
c Syntax (Toggle Plain Text)
if(frequency != 5) cout << "\nThere are" << frequency << " number of your prediction that matches"; else cout << "Congratulation!! All of Your Prediction Matches ";
I don't accept change; I don't deserve to live.
![]() |
Other Threads in the C++ Forum
- Previous Thread: 'Segmentation Fault'
- Next Thread: Parted in C++
| Thread Tools | Search this Thread |
api application array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






