Do/While Loop not working
Oct 27, 2012 at 7:57pm UTC
Can someone help me understand why this do/while loop isn't working. I want it to stop when at least one random number in each one of the vectors is the same as another random number selected.
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
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <ctime>
#include <algorithm>
using namespace std;
int main(void )
{
vector<int > vectorJ(4);
vector<int > vectorK(4);
vector<int > vectorL(4);
vector<int > vectorM(4);
vector<int > vectorN(4);
for (int i=0;i<5;i++)
{
vectorJ[i] = rand() % 15 + 1; //random number from 1 to 15
vectorK[i] = rand() % 15 + 16; //random number from 16 to 30
vectorL[i] = rand() % 15 + 31; //random number from 31 to 45
vectorM[i] = rand() % 15 + 45; //random number from 46 to 60
vectorN[i] = rand() % 15 + 61; //random number from 60 to 75
}
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int xyz = 0;
do
{
char question;
cout << "Type 'A' to select random number... " ;
cin >> question;
if (question == 'a' || question == 'A' )
{
int randomN = rand() % 75 + 1; //draws random number from 1 -75
for (int i=0;i<5;i++) //if random number drawn is the same as the random numbers first inserted into the vectors, it changes that vectors random number to 99
{
if (randomN == vectorJ[i])
{
vectorJ[i] = 99;
}
if (randomN == vectorK[i])
{
vectorK[i] = 99;
}
if (randomN == vectorL[i])
{
vectorL[i] = 99;
}
if (randomN == vectorM[i])
{
vectorM[i] = 99;
}
if (randomN == vectorN[i])
{
vectorN[i] = 99;
}
}
}
if ((vectorJ[0] || vectorJ[1] || vectorJ[2] || vectorJ[3] || vectorJ[4]) == 99)
{
a = 1;
}
if ((vectorK[0] || vectorK[1] || vectorK[2] || vectorK[3] || vectorK[4]) == 99)
{
b = 1;
}
if ((vectorL[0] || vectorL[1] || vectorL[2] || vectorL[3] || vectorL[4]) == 99)
{
c = 1;
}
if ((vectorM[0] || vectorM[1] || vectorM[2] || vectorM[3] || vectorM[4]) == 99)
{
d = 1;
}
if ((vectorN[0] || vectorN[1] || vectorN[2] || vectorN[3] || vectorN[4]) == 99)
{
e = 1;
}
if ((a == 1) && (b == 1) && (c == 1) && (d == 1) && (e == 1))
{
xyz = 1;
}
}
while (xyz == 0);
}
Last edited on Oct 27, 2012 at 7:58pm UTC
Oct 27, 2012 at 8:07pm UTC
I'd say it's more than likely your if conditions.
They'd need to look more like this:
1 2 3
if ( vectorJ[0] == 99 ||
vectorJ[1] == 99 ||
// etc.
Or, more simply...
1 2 3 4 5 6 7 8 9 10
for ( int i=0; i < 5; i++ )
{
if ( vectorJ[i] == 99 )
a = 1;
if ( vectorK[i] == 99 )
b = 1;
// etc.
}
Oct 27, 2012 at 8:35pm UTC
Worked! Thanks a lot
Topic archived. No new replies allowed.