Loop with stop at a certain number

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

Join Date: Apr 2008
Posts: 5
Reputation: echoestroy is an unknown quantity at this point 
Solved Threads: 0
echoestroy's Avatar
echoestroy echoestroy is offline Offline
Newbie Poster

Loop with stop at a certain number

 
0
  #1
Apr 23rd, 2008
I was wondering how i could set up a random number loop, but have it stop looping when it hits 1 or any other number i assign.

i use dev C++ compiler

  1. //Damage Roller
  2.  
  3. #include <iostream>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <conio.h>
  7.  
  8. using namespace std;
  9.  
  10. int main()
  11.  
  12. {
  13. srand(time(0));
  14. do{
  15. int randomNumber = rand();
  16.  
  17. int die = (randomNumber%6)+1;
  18. cout<<"You hit for "<<die<<" damage"<<endl;
  19.  
  20. getch();
  21. }while(1);
  22. return 0;
  23. }
I Drink Your MILKSHAKE!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loop with stop at a certain number

 
1
  #2
Apr 23rd, 2008
The simplest method is to break when randomNumber is one of those values:
  1. if ( randomNumber == 5 )
  2. break;
That gets a little tedious when the list of numbers to break on is long, but you can easily scale it up with a collection:
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <set>
  5.  
  6. int main()
  7. {
  8. int init[] = { 1, 2, 5, 7, 9, 14, 18 };
  9. std::set<int> ignore ( init, init + 7 );
  10.  
  11. srand ( (unsigned)time ( 0 ) );
  12.  
  13. int r;
  14.  
  15. do {
  16. r = rand();
  17. std::cout<< r <<'\n';
  18. } while ( ignore.find ( r ) == ignore.end() );
  19. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 5
Reputation: echoestroy is an unknown quantity at this point 
Solved Threads: 0
echoestroy's Avatar
echoestroy echoestroy is offline Offline
Newbie Poster

Re: Loop with stop at a certain number

 
0
  #3
Apr 24th, 2008
thanks for the help i got it working really good now, but is there a way to add up the randomNumbers you get after they are shown?
I Drink Your MILKSHAKE!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,660
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 723
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Loop with stop at a certain number

 
1
  #4
Apr 24th, 2008
Of course. Just add them to a running total right after you print them. When the loop is done, you'll have a sum of all of the printed random numbers.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 5
Reputation: echoestroy is an unknown quantity at this point 
Solved Threads: 0
echoestroy's Avatar
echoestroy echoestroy is offline Offline
Newbie Poster

Re: Loop with stop at a certain number

 
0
  #5
Apr 27th, 2008
worked like a charm thanks so much
I Drink Your MILKSHAKE!!
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC