943,868 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1140
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 21st, 2008
0

Stopping same number from appearing twice?

Expand Post »
Hey, my dad was checking the "lotto" results so that inspired me to make a little program of my own, i accomplished that just got a problem..

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. srand(time(NULL));
  8.  
  9. unsigned int lotteryBall;
  10.  
  11. for(int i = 0; i <=7; ++i)
  12. {
  13. lotteryBall = rand() % 48 + 1;
  14.  
  15. cout << lotteryBall << " ";
  16.  
  17. if(i == 7)
  18. cout << "*" << lotteryBall << "*";
  19. }
  20.  
  21. cin.get();
  22.  
  23. return 0;
  24. }

I compiled and ran the code and got : 13 19 20 9 31 12 37 46 *46*

But obviously you can't have 46 twice, so I'm not sure how I could change it.

And before you say "the numbers need to go in order from smallest to largest" I will add bubble sort after I have got the main problem solved.

Many thanks.
Last edited by Black Magic; Jun 21st, 2008 at 6:10 am.
Reputation Points: 25
Solved Threads: 4
Junior Poster
Black Magic is offline Offline
177 posts
since Apr 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

The reason you got 46 twice there isn't because the random number happends to be the same, its because you are printing the same variable twice without modifying it. If you want to make it so each number can only come up once, you have to make an array for the numbers to go into it and then check it to see if it has already been added.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

I've modified the script a bit

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. srand(time(NULL));
  8.  
  9. unsigned int lotteryBall;
  10.  
  11. for(int i = 0; i <=7; ++i)
  12. {
  13. lotteryBall = rand() % 48 + 1;
  14.  
  15. cout << lotteryBall << " ";
  16.  
  17. if(i == 7)
  18. lotteryBall = rand() % 48 + 1;
  19. cout << "*" << lotteryBall << "*";
  20. }
  21.  
  22. cin.get();
  23.  
  24. return 0;
  25. }

just added 1 more line
Reputation Points: 24
Solved Threads: 8
Junior Poster in Training
Tigran is offline Offline
90 posts
since Jun 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

That wont work, you fergot to add curly brackets for the if statement.
Last edited by William Hemsworth; Jun 21st, 2008 at 9:25 am.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

how can i edit my post?
Reputation Points: 24
Solved Threads: 8
Junior Poster in Training
Tigran is offline Offline
90 posts
since Jun 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

You can only edit your post within 30 minutes of posting it, you will just have to repost it with the correct changes.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

Is this right?
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. srand(time(NULL));
  8.  
  9. unsigned int lotteryBall[9];
  10.  
  11. for(int i = 0; i <=7; ++i)
  12. {
  13. lotteryBall[i] = rand() % 48 + 1;
  14.  
  15. cout << lotteryBall[i] << " ";
  16.  
  17. if(i == 7)
  18. { do {
  19. lotteryBall[i+1] = rand() % 48 + 1;}
  20. while (
  21. (lotteryBall[i+1]==lotteryBall[0])||
  22. (lotteryBall[i+1]==lotteryBall[1])||
  23. (lotteryBall[i+1]==lotteryBall[2])||
  24. (lotteryBall[i+1]==lotteryBall[3])||
  25. (lotteryBall[i+1]==lotteryBall[4])||
  26. (lotteryBall[i+1]==lotteryBall[5])||
  27. (lotteryBall[i+1]==lotteryBall[6])||
  28. (lotteryBall[i+1]==lotteryBall[7]));
  29.  
  30. cout << "*" << lotteryBall[i+1] << "*"; }
  31. }
  32.  
  33. cin.get();
  34.  
  35. return 0;
  36. }

Probably much more efficient way that it can be done
Last edited by salman213; Jun 21st, 2008 at 3:29 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

yep.. try learning loops salman
Last edited by William Hemsworth; Jun 21st, 2008 at 3:28 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

would this be better or worse ???

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int check (unsigned int checkvariable[]);
  6.  
  7. int main()
  8. {
  9. srand(time(NULL));
  10.  
  11. unsigned int lotteryBall[9];
  12.  
  13. for(int i = 0; i <=7; ++i)
  14. {
  15. lotteryBall[i] = rand() % 48 + 1;
  16.  
  17. cout << lotteryBall[i] << " ";
  18.  
  19. if(i == 7)
  20. {
  21. do {
  22. lotteryBall[i+1] = rand() % 48 + 1;
  23. }while (check(lotteryBall));
  24. cout << "*" << lotteryBall[i+1] << "*";
  25. }
  26.  
  27. }
  28.  
  29. cin.get();
  30.  
  31. return 0;
  32. }
  33.  
  34. int check ( unsigned int checkvariable[])
  35. {
  36. for(int a=0;a<=7;++a)
  37. {
  38. if (checkvariable[a]==checkvariable[8])
  39. {return true;
  40. break;}
  41. else if (a==7)
  42. return false;
  43. }
  44.  
  45. }
Reputation Points: 10
Solved Threads: 0
Light Poster
salman213 is offline Offline
32 posts
since Jun 2008
Jun 21st, 2008
0

Re: Stopping same number from appearing twice?

I thought you needed to #include <cstdlib> in order to use the rand() function...?
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Ellisande is offline Offline
53 posts
since Jun 2008

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: Use of getc()/putc() for reading/writing chars, floats, etc. from binary files?
Next Thread in C++ Forum Timeline: how to make system() work in DOS Box





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


Follow us on Twitter


© 2011 DaniWeb® LLC