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:
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;
}
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 << " ";
}
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):