| | |
Stopping same number from appearing twice?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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..
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { srand(time(NULL)); unsigned int lotteryBall; for(int i = 0; i <=7; ++i) { lotteryBall = rand() % 48 + 1; cout << lotteryBall << " "; if(i == 7) cout << "*" << lotteryBall << "*"; } cin.get(); return 0; }
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.
C Plus Plus Coder.
Fourteen Years Of Age
Fourteen Years Of Age
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
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.
I need pageviews! most fun profile ever :)
•
•
Join Date: Jun 2008
Posts: 67
Reputation:
Solved Threads: 5
I've modified the script a bit
just added 1 more line
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { srand(time(NULL)); unsigned int lotteryBall; for(int i = 0; i <=7; ++i) { lotteryBall = rand() % 48 + 1; cout << lotteryBall << " "; if(i == 7) lotteryBall = rand() % 48 + 1; cout << "*" << lotteryBall << "*"; } cin.get(); return 0; }
just added 1 more line
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
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.
I need pageviews! most fun profile ever :)
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
You can only edit your post within 30 minutes of posting it, you will just have to repost it with the correct changes.
I need pageviews! most fun profile ever :)
•
•
Join Date: Jun 2008
Posts: 32
Reputation:
Solved Threads: 0
Is this right?
Probably much more efficient way that it can be done
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { srand(time(NULL)); unsigned int lotteryBall[9]; for(int i = 0; i <=7; ++i) { lotteryBall[i] = rand() % 48 + 1; cout << lotteryBall[i] << " "; if(i == 7) { do { lotteryBall[i+1] = rand() % 48 + 1;} while ( (lotteryBall[i+1]==lotteryBall[0])|| (lotteryBall[i+1]==lotteryBall[1])|| (lotteryBall[i+1]==lotteryBall[2])|| (lotteryBall[i+1]==lotteryBall[3])|| (lotteryBall[i+1]==lotteryBall[4])|| (lotteryBall[i+1]==lotteryBall[5])|| (lotteryBall[i+1]==lotteryBall[6])|| (lotteryBall[i+1]==lotteryBall[7])); cout << "*" << lotteryBall[i+1] << "*"; } } cin.get(); return 0; }
Probably much more efficient way that it can be done
Last edited by salman213; Jun 21st, 2008 at 3:29 pm.
•
•
Join Date: Mar 2008
Posts: 1,427
Reputation:
Solved Threads: 115
yep.. try learning loops salman
Last edited by William Hemsworth; Jun 21st, 2008 at 3:28 pm.
I need pageviews! most fun profile ever :)
•
•
Join Date: Jun 2008
Posts: 32
Reputation:
Solved Threads: 0
would this be better or worse
???
??? C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int check (unsigned int checkvariable[]); int main() { srand(time(NULL)); unsigned int lotteryBall[9]; for(int i = 0; i <=7; ++i) { lotteryBall[i] = rand() % 48 + 1; cout << lotteryBall[i] << " "; if(i == 7) { do { lotteryBall[i+1] = rand() % 48 + 1; }while (check(lotteryBall)); cout << "*" << lotteryBall[i+1] << "*"; } } cin.get(); return 0; } int check ( unsigned int checkvariable[]) { for(int a=0;a<=7;++a) { if (checkvariable[a]==checkvariable[8]) {return true; break;} else if (a==7) return false; } }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Use of getc()/putc() for reading/writing chars, floats, etc. from binary files?
- Next Thread: how to make system() work in DOS Box
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






