So, I'm sure this has been done to death, but I'm doing a Win32 Console Application that will let the user enter the number of sides on their dice. It then rolls the dices and displays the roll. I've got this working fine, but what I would like for it to do is let me do this process several times without having to restart the application. I know there's a fairly crude way of doing this, just by writing more declarations and i/o streams with more calculations. But is there a bit of code I could write at the end of the program that would just reset roll and sides to 0, and then run the i/o and calc again? Here is my code so far...
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int sides = 0;
int roll = 0;
int main()
{
cout << "Enter the number of sides on the dice: ";
cin >> sides;
roll = rand() % sides + 1;
cout << "You rolled a " << roll << "." << endl;
return 0;
}
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int sides = 0;
int roll = 0;
int mult = 0;
int main()
{
while (mult < 20)
{
mult++;
cout << "Enter the number of sides on the dice: ";
cin >> sides;
roll = rand() % sides + 1;
cout << "You rolled a " << roll << "." << endl;
}
return 0;
}
Edit: After looking through that page, I may switch to a "do while" loop. Seems a bit more practical for my needs.
int main()
{
int sides, roll;
for (int i = 0; i<=x; i++)
{
cout<<"Enter the number of sides you want: ";
cin >> sides;
cin.ignore(256,'/n'); // get used to putting this after pretty much every cin>> statement
roll = rand() % sides + 1;
cout<< "You rolled a " << roll << "." <<endl;
}
just the basic idea of for loops.. replace the letter x with how many loops you want
haha too late i guess XD. generally you use a while loop for 'event' controlled looping.... for loops for 'count' controlled loops
Okay, so I tried making this using a do-while loop. But, whenever I try to end it by inputting a 0, it brings up a "This program has stopped working" error. Click close the program, but then the program continues on to the "Press any key to continue..." thing. Any idea what's wrong?
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int sides = 0;
int roll = 0;
int main()
{
cout << "Enter a 0 at any time to terminate the program." << endl;
do
{
cout << "Enter the number of sides on the dice: ";
cin >> sides;
roll = rand() % sides + 1;
cout << "You rolled a " << roll << "." << endl;
}
while (sides != 0);
return 0;
}
it might be that it doesn't like to % something by 0
you can add
1 2
if (sides == 0)
break;
to jump out of the loop before that line. Note then that you could have it loop while true, which would be an infinite loop, but it's okay because it can exit from the break.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int sides = 0;
int roll = 0;
int main()
{
cout << "Enter a 0 at any time to terminate the program." << endl;
do
{
cout << "Enter the number of sides on the dice: ";
cin >> sides;
if (sides == 0)
break;
else
roll = rand() % sides + 1;
cout << "You rolled a " << roll << "." << endl;
}
while (sides != 0);
return 0;
}