While loop

I want to my while loop execute more than one line of code inside it, and I have problem. In case while is true, it just executes the first outcome or sentence or statement. How can I code to do whatever cout I want to benig executed?
Are you looking for the use of braces { } to form a compound statement?
From the tutorial:

1
2
if (x == 100)
  cout << "x is 100";


If you want to include more than a single statement to be executed when the condition is fulfilled, these statements shall be enclosed in braces ({}), forming a block:
1
2
3
4
5
if (x == 100)
{
   cout << "x is ";
   cout << x;
}


http://www.cplusplus.com/doc/tutorial/control/


I'm doing it, but still don't work.

while (numberOftriangles < 0 || numberOftriangles>4) {
cout << "number must be between 1 and 4\n" << endl;
cout << "enter number and size of triangles\n" << endl;
cin >> numberOftriangles;
}
That code seems to work, though the condition
 
(numberOftriangles < 0 || numberOftriangles>4)

doesn't agree with the text:
 
"number must be between 1 and 4\n" 


(The code allows 0 to 4, not 1 to 4 as stated).
Last edited on
This is a notice that is saying the number you are entering is wrong. So as along as user put the wrong number, and the condition is right, the notice execute.

but, for me the second line: cout << "enter number and size of triangles\n" << endl; is not executing.

this is the whole code.

//04/01/2017

#include <iostream>
using namespace std;

int main() {

int numberOfstars, numberOftriangles;
cout << "enter number and size of triangles\n";
cin >> numberOftriangles;

while (numberOftriangles < 0 || numberOftriangles>4) {
cout << "number must be between 1 and 4\n" << endl;
cout << "enter number and size of triangles\n" << endl;
}
cin >> numberOftriangles;

}
cin >> numberOfstars;
while (numberOfstars <0) {
cout << "size must be greater than 0\n";
cin >> numberOfstars;
}

for (int i = 1; i <= numberOfstars; i++) {

for (int j = 1; j <= i; j++)
{
cout << "*";

}
if (numberOftriangles >= 2) {
for (int k = numberOfstars; k >= i; k--) {
cout << " ";
}

for (int j = numberOfstars + 1 - i; j >= 1; j--)
cout << "*";
if (numberOftriangles >= 3) {

for (int m = 0; m <= i - 2; m++)
cout << " ";

for (int n = 1; n <= i; n++)
cout << " ";

for (int z = numberOfstars; z >= i; z--)
cout << "*";
if (numberOftriangles == 4) {

for (int d = numberOfstars + 1 - i; d >= 1; d--)
cout << " ";

for (int y = 1; y <= i; y++)
cout << "*";
}
}
}

cout << "\n";
}
}
Last edited on
That code doesn't compile.
Function main() looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() 
{
    int numberOfstars, numberOftriangles;
    cout << "enter number and size of triangles\n";
    cin >> numberOftriangles;

    while (numberOftriangles < 0 || numberOftriangles>4)
    {
        cout << "number must be between 1 and 4\n" << endl;
        cout << "enter number and size of triangles\n" << endl;
    }
    
    
    cin >> numberOftriangles;

}


and then there is a whole lot of other code which is not part of any function.

Probably you have some misplaced or mismatched braces somewhere.


Also, could you use code tags please, to make the code easier to read, and the indentation show properly (see article):

http://www.cplusplus.com/articles/jEywvCM9/


Topic archived. No new replies allowed.