Misused (while) for loop, and program no longer functions
Jan 31, 2018 at 8:10pm UTC
Hey, I tried writing a program to simulate D&D dice. It seemed to work fine until I tried to loop it using (while) and now my code doesn't work. The idea was the program would ask how many roles you were making and with what dice, then it would perform the roles, and after performing the roles it would go back to asking you how many roles, and so on. The program instead created an infinite loop of the same role set, so I commented it out but now it doesn't even display the roles. When you input a dice for it to role it just returns blank and ends the program. Ive been trying for awhile to figure out what happened but I haven't managed to figure it out. Can someone explain why it returns blank now?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int Roles;
char d4 = d4;
char d6 = d6;
char d8 = d8;
char d10 = d10;
char d12 = d12;
char d20 = d20;
char d100 = d100;
char Geoffry;
char again;
// while ( again == 'y' or 'Y' or 'yes' )
//{
cout << "Enter Number of Roles" << endl;
cout << " ________________________________________________ " << endl;
cin >> Roles;
cout << " ________________________________________________ " << endl;
cout << left << setw(50) << " ________________________________________________ " << endl;
cout << left << setw(1) << "| Enter a Dice |" << endl;
cout << left << setw(1) << "| |" << endl;
cout << left << setw(1) << "| d4 |" << endl;
cout << left << setw(1) << "| |" << endl;
cout << left << setw(1) << "| d6 |" << endl;
cout << left << setw(1) << "| |" << endl;
cout << left << setw(1) << "| d8 |" << endl;
cout << left << setw(1) << "| |" << endl;
cout << left << setw(1) << "| d10 |" << endl;
cout << left << setw(1) << "| |" << endl;
cout << left << setw(1) << "| d12 |" << endl;
cout << left << setw(1) << "| |" << endl;
cout << left << setw(1) << "| d20 |" << endl;
cout << left << setw(1) << "| |" << endl;
cout << left << setw(1) << "| d100 |" << endl;
cout << left << setw(50) << " ________________________________________________ " << endl;
cin >> Geoffry ;
if ( Geoffry == d4 )
{
srand((int )time(0));
int i = 0;
while (i++ < Roles) {
int r = (rand() % 4) + 1;
cout << r << " " ;
}}
else if ( Geoffry == d6)
{
srand((int )time(0));
int i = 0;
while (i++ < Roles) {
int r = (rand() % 6) + 1;
cout << r << " " ;
}}
else if ( Geoffry == d8)
{
srand((int )time(0));
int i = 0;
while (i++ < Roles) {
int r = (rand() % 8) + 1;
cout << r << " " ;
}}
else if ( Geoffry == d10)
{
srand((int )time(0));
int i = 0;
while (i++ < Roles) {
int r = (rand() % 10) + 1;
cout << r << " " ;
}}
else if ( Geoffry == d12)
{
srand((int )time(0));
int i = 0;
while (i++ < Roles) {
int r = (rand() % 12) + 1;
cout << r << " " ;
}}
else if ( Geoffry == d20)
{
srand((int )time(0));
int i = 0;
while (i++ < Roles) {
int r = (rand() % 20) + 1;
cout << r << " " ;
}}
else if ( Geoffry == d100)
{
srand((int )time(0));
int i = 0;
while (i++ < Roles) {
int r = (rand() % 100) + 1;
cout << r << " " ;
}}
cout << endl;
//cout << "Again? y/n" << endl;
//cin >> again ;
//cout << " ________________________________________________ " << endl;
// }
return 0;
}
Jan 31, 2018 at 9:01pm UTC
while ( again == 'y' or 'Y' or 'yes' )
This is three conditions.
again == 'y'
Might be true, might not be true.
'Y'
- this is just plain true. Always.
'yes'
- so is this. Probably. It's bad syntax. ' ' goes around ONE char, but you've put it around three, so what your compiler makes of this I don't know. It really should have told you this was a mistake.
So your condition is:
while ( again == 'y' or TRUE or TRUE )
Always true, so loops forever. Perhaps you meant this:
1 2 3
while ( again == 'y' ||
again == 'Y' ||
again == "yes" ) // note that a char goes inside ' ' , but a string goes inside " "
Last edited on Jan 31, 2018 at 9:05pm UTC
Jan 31, 2018 at 11:06pm UTC
thanks for this solution Repeater!
Feb 1, 2018 at 1:25am UTC
Thanks for the quick reply, I am unfortunately still having a problem. When I run the program using,
1 2 3 4 5 6 7 8 9 10
char Geoffry;
char again = 'y' ;
while (again == 'y' )
{
cin >> roles;
cin >> Geoffry;
//functions
cin >> again;
}
It is skipping the cin and ending the program. What am I doing wrong now?
Feb 1, 2018 at 7:28pm UTC
Hey, I managed to get the code functioning thanks for the help, still not sure what caused my last problem but here is the working code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char again;
string Geoffry;
int rolls;
do {
cout << endl;
cout << "Please enter your rolls bro." << endl;
cin >> rolls;
cout << "Enter your type of dice boi." << endl;
cout << "d4, d6, d8, d10, d12, d20, d100" << endl;
cout << endl;
cin >> Geoffry;
cout << endl;
cout << "Alright bro I'll see what can do! lets see carry the two ah ha there we go have some of these eh." << endl;
cout << endl;
if ( Geoffry == "d4" )
{
srand((int )time(0));
int i = 0;
while (i++ < rolls) {
int r = (rand() % 4) + 1;
cout << r << " " ;
}}
if ( Geoffry == "d6" )
{
srand((int )time(0));
int i = 0;
while (i++ < rolls) {
int r = (rand() % 6) + 1;
cout << r << " " ;
}}
if ( Geoffry == "d8" )
{
srand((int )time(0));
int i = 0;
while (i++ < rolls) {
int r = (rand() % 8) + 1;
cout << r << " " ;
}}
if ( Geoffry == "d10" )
{
srand((int )time(0));
int i = 0;
while (i++ < rolls) {
int r = (rand() % 10) + 1;
cout << r << " " ;
}}
if ( Geoffry == "d12" )
{
srand((int )time(0));
int i = 0;
while (i++ < rolls) {
int r = (rand() % 12) + 1;
cout << r << " " ;
}}
if ( Geoffry == "d20" )
{
srand((int )time(0));
int i = 0;
while (i++ < rolls) {
int r = (rand() % 20) + 1;
cout << r << " " ;
}}
if ( Geoffry == "d100" )
{
srand((int )time(0));
int i = 0;
while (i++ < rolls) {
int r = (rand() % 100) + 1;
cout << r << " " ;
}}
cout << endl;
cout << endl;
cout << "Don't lie, that was sexy right?" << endl;
cin >> again;
}
while (again >= 'y' );
if (again != 'y' )
{
return 0;
}
return 0;
}
Topic archived. No new replies allowed.