Anyone can do this in C++

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Anyone can do this in C++

 
0
  #11
Jun 26th, 2007
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:
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 33
Reputation: bvgsrs is an unknown quantity at this point 
Solved Threads: 1
bvgsrs bvgsrs is offline Offline
Light Poster

Re: Anyone can do this in C++

 
0
  #12
Jun 26th, 2007
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.
Last edited by bvgsrs; Jun 26th, 2007 at 4:23 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Anyone can do this in C++

 
-1
  #13
Jun 26th, 2007
Originally Posted by pixrix View Post
this is my program..
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.
Are you Agile.. ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 222
Reputation: JRM will become famous soon enough JRM will become famous soon enough 
Solved Threads: 14
JRM's Avatar
JRM JRM is offline Offline
Posting Whiz in Training

Re: Anyone can do this in C++

 
0
  #14
Jun 26th, 2007
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++
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 18
Reputation: pixrix is an unknown quantity at this point 
Solved Threads: 0
pixrix pixrix is offline Offline
Newbie Poster

Re: Anyone can do this in C++

 
0
  #15
Jun 27th, 2007
Thanks for all the tips.. after i finish i will post it out to let u all to have a look.. thanks again
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 18
Reputation: pixrix is an unknown quantity at this point 
Solved Threads: 0
pixrix pixrix is offline Offline
Newbie Poster

Re: Anyone can do this in C++

 
0
  #16
Jun 29th, 2007
this is my final program...
  1. #include<iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7. #include <cstdlib>
  8.  
  9. #include <ctime>
  10.  
  11. int main()
  12. {
  13. int wilson;
  14. const int Size = 5;
  15. int lottery[Size];
  16. int wilsonArray[Size];
  17. int frequency=0;
  18.  
  19. cout << "Please Enter 5 Number of Your Prediction?" << endl;
  20. cin >> wilson;
  21.  
  22. while (wilson < 0 || wilson > 99999)
  23. {
  24. cout << "Invalid Number Enter" << endl << "Please Re-enter 5 Number of Your Prediction?" << endl;
  25. cin >> wilson;
  26. }
  27.  
  28. if( wilson >= 0 && wilson < 100000)
  29. {
  30.  
  31. wilsonArray[0]=wilson/10000;
  32. wilsonArray[1]=wilson%10000/1000;
  33. wilsonArray[2]=wilson%1000/100;
  34. wilsonArray[3]=wilson%100/10;
  35. wilsonArray[4]=wilson%10;
  36.  
  37. cout << "\nWilson Prediction" << endl;
  38. cout << " *********************" << endl << " | ";
  39. for(int i = 0; i < 5; i++)
  40. cout << wilsonArray[i] << " | ";
  41. cout << "\n *********************" << endl;
  42.  
  43. srand(time(0));
  44.  
  45. cout << "\nLottery Result"<< endl;
  46. cout << " *********************" << endl << " | ";
  47. for (int j = 0; j < 5; j++)
  48. {
  49. lottery[j] = rand() % 10;
  50. cout << lottery[j] << " | ";
  51.  
  52. }
  53. cout << "\n *********************" << endl;
  54. for (int k = 0; k < 5; k++)
  55. if(wilsonArray[k] == lottery[k])
  56. ++frequency;
  57.  
  58. if(frequency == 0 )
  59. cout << "\nThere are none of your prediction that matches" << endl;
  60. if(frequency == 1 )
  61. cout << "\nThere are one number of your prediction that matches" << endl;
  62. if(frequency == 2 )
  63. cout << "\nThere are two number of your prediction that matches" << endl;
  64. if(frequency == 3 )
  65. cout << "\nThere are three number of your prediction that matches" << endl;
  66. if(frequency == 4 )
  67. cout << "\nThere are four number of your prediction that matches" << endl;
  68. if(frequency == 5 )
  69. cout << "\n***************************************************" << endl << "| Congratulation!! All of Your Prediction Matches |" << endl<< "***************************************************" << endl;
  70.  
  71. }
  72.  
  73.  
  74. return 0;
  75. }
Last edited by ~s.o.s~; Jun 29th, 2007 at 5:47 am. Reason: Added code tags, learn to use them.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 33
Reputation: bvgsrs is an unknown quantity at this point 
Solved Threads: 1
bvgsrs bvgsrs is offline Offline
Light Poster

Re: Anyone can do this in C++

 
0
  #17
Jun 29th, 2007
Its really gr8 ... that you have come up with a this fine code during your initial attempts.
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,627
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Anyone can do this in C++

 
0
  #18
Jun 29th, 2007
Since the frequency is initialized with 0 and can hold a maximum value of 5, we can also write:
  1. if(frequency != 5)
  2. cout << "\nThere are" << frequency << " number of your prediction that matches";
  3. else
  4. cout << "Congratulation!! All of Your Prediction Matches ";
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 18
Reputation: pixrix is an unknown quantity at this point 
Solved Threads: 0
pixrix pixrix is offline Offline
Newbie Poster

Re: Anyone can do this in C++

 
0
  #19
Jul 1st, 2007
thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC