# Flag-Controlled Loops

• 03-21-2010
CPPN0OB
Flag-Controlled Loops
Hi, I'm new to the board and to C++!

I am trying to design a program that will basically read in 8 numbers and then indicate whether any of them were larger than 50 or smaller than 10.

I need to write the program so that it reads in 8 numbers and echoes them to the screen using a loop.
Then I need to add two flags, one to detect if the numbers are larger than 50 and one to detect if the numbers are larger than 10.

I've been trying to understand loops for about 3 hours and I'm having an extremely hard time grasping it.

Any suggestions would be greatly appreciated!
• 03-21-2010
MK27
Well, try writing some code as best you can. If it doesn't work -- compiles with errors, or behaves unexpectedly, etc, -- just post the code with an explanation and someone will help you with it.

Here's a loop with a flag:
Code:

```int i; bool flag = false; for(i=0;i<10;i++) {     if (i == 666) flag = true; } if (flag) cout << "Inconceivable!";```
• 03-21-2010
CPPN0OB
Ok, so this is pretty much what I want to do. However, I think I'm defeating the purpose of a loop.

I can't grasp the semantics of reiteration. But here's the general idea.

Also, I noticed I was a bit lazy with the indentation so forgive me!

Code:

``` #include <iostream> using namespace std; int main () { bool greater = true; int one, two, three, four, five, six, seven, eight; int num1, num2, num3, num4, num5, num6, num7, num8; one = 0; two = 0; three = 0; four = 0; five = 0; six = 0; seven = 0; eight = 0; cout <<"Please enter eight numbers." << endl; cin >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7 >> num8; while (greater) {         if (num1 > 50)                 one ++;         else                 greater = false; if (num2 > 50) two ++; else greater = false; if (num3 >50) three ++; else greater = false; if (num4 >50) four ++; else greater = false; if (num5 >50) five ++; else greater = false; if (num6 >50) six ++; else greater = false; if (num7 > 50 ) seven ++; else greater = false; if (num8 > 50 ) eight ++; else greater = false; } if (one == 1) cout << "Number 1 is greater than 50!" << endl; if (two == 1) cout << "Number 2 is greater than 50!" << endl; if (three == 1) cout << "Number 3 is greater than 50!" << endl; if (four == 1) cout << "Number 4 is greater than 50!" << endl; if (five == 1) cout << "Number 5 is greater than 50!" << endl; if (six == 1) cout << "Number 6 is greater than 50!" << endl; if (seven == 1) cout << "Number 7 is greater than 50!" << endl; if (eight == 1) cout << "Number 8 is greater than 50!" << endl; return 0; }```
• 03-21-2010
King Mir
Yeah, good indentation would be nice. . .

Here you want to use an arrays instead of variables that include a number in their name.

Instead of int num1, num2 ect, use int num[8].

Then you can do the same operation for each index.
• 03-22-2010
CPPN0OB
Ok, I did what you said. Although I still feel as if I am defeating the purpose of looping.

What can I do to make this loop work and shorten the amount of code? Again I'm trying to create a flag controlled loop.

Thanks.

Code:

``` #include <iostream> using namespace std; int main () { bool greater = true; int num[8]; int one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0; cout <<"Please enter three numbers." << endl; cin >> num [0] >> num[1] >> num[2] >> num[3] >> num[4] >> num[5] >> num[6] >> num [7];   while (greater)   {         if (num[0] > 50)                 one ++;                         else                                 greater = false;         if (num[1] > 50)                 two ++;                                                 else                                         greater = false;         if (num[2] >50)                 three ++;                                 else                                         greater = false;         if (num[3] >50)                 four ++;                                 else                                         greater = false;         if (num[4] >50)                 five ++;                                 else                                         greater = false;         if (num[5] >50)                 six ++;                                 else                                         greater = false;         if (num[6] > 50 )                 seven ++;                                 else                                         greater = false;         if (num[7]> 50 )                 eight ++;                                 else                                                greater = false;         while (greater)                                 if (num[0] < 10)                         one --;                                 else                                 greater = false;                         if (num[1] <10)                         two --;                                 else                                 greater = false;                 if (num[2] < 10)                         three --;                                 else                                         greater = false;                 if (num[3] < 10)                         four --;                                 else                                         greater = false;                 if (num[4] < 10)                         five --;                                 else                                         greater = false;                 if (num[5] < 10)                         six --;                                 else                                         greater = false;                 if (num[6] < 10 )                         seven --;                                 else                                         greater = false;                 if (num[7] < 10 )                         eight --;                                 else                                         greater = false;                         if (one == 1)                         cout << "Number 1 is greater than 50!" << endl;                 else if (one == -1)                         cout << "Number 1 is less than 10!" << endl;                 if (two == 1)                         cout << "Number 2 is greater than 50!" << endl;                 else if (two == -1)                         cout <<"Number 2 is less than 10!" << endl;                 if (three == 1)                         cout << "Number 3 is greater than 50!" << endl;                 else if (three == -1)                         cout << "Number 3 is less than 10!" << endl;                 if (four == 1)                         cout << "Number 4 is greater than 50!" << endl;                 else if (four == -1)                         cout << "Number 4 is less than 10!" << endl;                 if (five == 1)                         cout << "Number 5 is greater than 50!" << endl;                 else if (five == -1)                         cout << "Number 5 is less than 10!" << endl;                 if (six == 1)                         cout << "Number 6 is greater than 50!" << endl;                 else if (six == -1)                         cout << "Number 6 is less than 10!" << endl;                 if (seven == 1)                         cout << "Number 7 is greater than 50!" << endl;                 else if (seven == -1)                         cout << "Number 7 is less than 10!" << endl;                 if (eight == 1)                         cout << "Number 8 is greater than 50!" << endl;                 else if (eight == -1)                         cout << "Number 8 is less than 10!" << endl; } return 0;```
• 03-22-2010
tabstop
You should still not have variables called one, two, three, etc. I'm not sure what your assignment is, exactly, but I see no reason for their existence. Also there is no reason to not put your input in the loop. Basically any time you find yourself doing the same thing but changing 1 to 2, 2 to 3, etc, that means you need a loop.

A hint, just for input:
Code:

```for (i = 0; i < 8; i++) {     cin >> num[i]; }```
• 03-22-2010
MK27
Quote:

Originally Posted by King Mir
Here you want to use an arrays instead of variables that include a number in their name.

Instead of int num1, num2 ect, use int num[8].

Then you can do the same operation for each index.

Quote:

Originally Posted by CPPN0OB
Ok, I did what you said. Although I still feel as if I am defeating the purpose of looping.

AHHHH! What?? :p This is for the purpose of looping...

Quote:

Originally Posted by tabstop
A hint, just for input:

Hopefully that will put the pieces together for you. ;)
• 03-22-2010
DavidP
Just a small correction on tabstop's code he posted:

Code:

```for (i = 0; i < 8; i++) {     cin >> num[i]; }```
• 03-22-2010
CPPN0OB
Ok, I'm not sure I'm getting anywhere and I'm getting really frustrated with this! I've read the chapter in my book on loops like 10 times, and I've been looking all over the web. It's driving me insane because it's such a simple task in my head. I just keep drawing blanks when I go to write the code.

So here's what I'm shooting for:

Use a loop to echo the 8 numbers input

VV

Use a flag-controlled loop to check if any of the numbers are greater than 50

VV

Use another flag-controlled loop to check if any of the numbers are less than 10.

VV

After both loops are run I want to output which numbers were greater than 50, which were less than 10 or if they were neither.

The problem I'm having is understanding how to use a loop to do the same thing 8 times. I know that's what loops are for but for some reason I just can't grasp it. It would be much easier for me to write my own functions and then call them but we aren't allowed to do that for this lab. I'm so confused!
• 03-22-2010
DavidP
So let's get to the core of the problem: what is it that you do not understand about loops?

Do you understand how to write a loop?
Do you understand why you need a loop?
Do you understand what loops are used for?
Do you understand when you need to use a loop?

A loop is essentially meant to achieve your goals when you have a repetitive behavior that needs to be done (such as inputting several numbers).

Code:

```cin >> x1; cin >> x2; cin >> x3; cin >> x4; cin >> x5; cin >> x6; cin >> x7; cin >> x8;```
I can write a for loop:
Code:

```int x[8]; for (int i = 0; i < 8; i++ ) {         cin >> x[i]; }```
Or I can write a while loop:
Code:

```int x[8]; int i = 0; while (i < 8) {         cin >> x[i];         i++; }```
Or I can write a do-while loop: (the least common type of loop)
Code:

```int x[8]; int i = 0; do {         cin >> i;         i++; } while(i < 8);```

Apparently, your teacher is requiring you to use several loops (although you can do this with only 1 or 2 loops, your teacher wants you to use 4 or 5 loops, which is okay...because your teacher wants you to learn about loops).

So here is a hint. Let's say we are finished inputting our 8 numbers. Now we want to see which are greater than 50. We could do something like this:

Code:

```bool greater[8]; //Use a loop to check if any numbers are greater than 50 for (int i = 0; i < 8; i++) {         if (input[i] > 50) greater[i] = true;         else greater[i] = false; }```
You could do something very very similar to check which numbers are less than 10.
• 03-22-2010
MK27
The point right now is that all this:
Code:

```int num[8]; int one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0; while (greater)   {         if (num[0] > 50)                 one ++;```
Can be reduced to something like:
Code:

```int input[8];  // formerly "num" int numbers[8] = {0};  // all zero  int i; while (greater) {       for (i=0;i<8;i++) {             if (input[i] > 50) numbers[i]++;             else greater = false;       }```
Please note I changed the name of "num" to "input", for clarity.

I am not promising your code will now do exactly what you want, but this will help get it under control* and hopefully also help you to see the "use value" and potential of loops.

* in the sense that if it does not do what you want, you now have a single 2 line block to debug rather than the same thing repeated eight times.