using a lot of variables

1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;
int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;

int main(){
    1 <= a1&&a2&&a3&&a4&&a5&&a6&&a7&&a8&&a9&&a10&&a11&&a12 <= 6;
   
}  

Is it ok to do this?
I'm not sure if it's bad programming practice, but I guess it's O.K to do...
How about using an array?
1 <= a1&&a2&&a3&&a4&&a5&&a6&&a7&&a8&&a9&&a10&&a11&&a12 <= 6;


I don't know what you're trying to do here, but this doesen't do what you expect. It actually doesn't do anything at all.


Also if you find yourself naming things like a1, a2, etc, you're doing it wrong. For clusters of variables like that you're better off with arrays as shacktar suggested.
You probably meant this:
 
if(1 <= a1 && 1 <= a2 && 1 <= a3 ... && a10 <= 6 && a11 <= 6 && a12 <= 6) ...

You should use an array, as shacktar said.
maybe he's not try to do exactly like that, but something like that, for example:

1
2
3
int age, height, weight, date_of_birth /*etc*/;
//so it would be like this...
1 <= age && height && weight && date_of_birth /*etc*/<= 6;
1 <= age && height && weight && date_of_birth /*etc*/<= 6;

Just like the OP's code, that won't compile. Also, age && height && weight && date_of_birth will be true if each value is non-zero and false otherwise. Probably not what was intended.

Here is a function that does what I think the OP wants.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int ARRAY_SIZE = 12;
int a[ARRAY_SIZE];

//you can pass in "a" as the first parameter, and pass in 1 as min and 6 as max
bool AllValuesInRange(int myArray[], int min, int max)
{
    bool allInRange = true;

    for(int i = 0; i < ARRAY_SIZE && allInRange; i++)
    {
        allInRange = allInRange && ( (myArray[i] >= min) && (myArray[i] <= max) );
    }

    return allInRange;
}
Topic archived. No new replies allowed.