| | |
Loop with stop at a certain number
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
i use dev C++ compiler
C++ Syntax (Toggle Plain Text)
//Damage Roller #include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> using namespace std; int main() { srand(time(0)); do{ int randomNumber = rand(); int die = (randomNumber%6)+1; cout<<"You hit for "<<die<<" damage"<<endl; getch(); }while(1); return 0; }
I Drink Your MILKSHAKE!!
The simplest method is to break when randomNumber is one of those values:
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:
C++ Syntax (Toggle Plain Text)
if ( randomNumber == 5 ) break;
C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <ctime> #include <iostream> #include <set> int main() { int init[] = { 1, 2, 5, 7, 9, 14, 18 }; std::set<int> ignore ( init, init + 7 ); srand ( (unsigned)time ( 0 ) ); int r; do { r = rand(); std::cout<< r <<'\n'; } while ( ignore.find ( r ) == ignore.end() ); }
I'm here to prove you wrong.
![]() |
Similar Threads
- Loop counting odd and even numbers (C++)
- filestream && multidimensional arrays (C++)
- Help - While loop won't work (Java)
- bank system problem !!!! (C++ programming) (C++)
- a for loop problem (Java)
- display the number of capitals letters in the sentence (Assembly)
- Need some help (Java)
- code seems to stop executing (C++)
- problem creating a for loop for an array (C++)
Other Threads in the C++ Forum
- Previous Thread: Help needed badly: Problem reading from file
- Next Thread: Ports?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline 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 node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






