943,583 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2752
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 26th, 2007
0

Re: Anyone can do this in C++

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
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006
Jun 26th, 2007
0

Re: Anyone can do this in C++

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.
Reputation Points: 58
Solved Threads: 1
Light Poster
bvgsrs is offline Offline
33 posts
since Jun 2007
Jun 26th, 2007
-1

Re: Anyone can do this in C++

Click to Expand / Collapse  Quote originally posted by pixrix ...
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.
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Jun 26th, 2007
0

Re: Anyone can do this in C++

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++
}
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Jun 27th, 2007
0

Re: Anyone can do this in C++

Thanks for all the tips.. after i finish i will post it out to let u all to have a look.. thanks again
Reputation Points: 28
Solved Threads: 0
Newbie Poster
pixrix is offline Offline
18 posts
since Jun 2007
Jun 29th, 2007
0

Re: Anyone can do this in C++

this is my final program...
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 28
Solved Threads: 0
Newbie Poster
pixrix is offline Offline
18 posts
since Jun 2007
Jun 29th, 2007
0

Re: Anyone can do this in C++

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.
Reputation Points: 58
Solved Threads: 1
Light Poster
bvgsrs is offline Offline
33 posts
since Jun 2007
Jun 29th, 2007
0

Re: Anyone can do this in C++

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 ";
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jul 1st, 2007
0

Re: Anyone can do this in C++

thanks
Reputation Points: 28
Solved Threads: 0
Newbie Poster
pixrix is offline Offline
18 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: 'Segmentation Fault'
Next Thread in C++ Forum Timeline: Parted in C++





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


Follow us on Twitter


© 2011 DaniWeb® LLC