-1

Why does the emission of braces in the following infamous example produce the desired greeting output? However, when adding braces on row 37 & 40 under the else, for the two statements, an infinite loop is produced? The first code example produces an infinite loop of asterisks. The second produces the desired output of a returned bordered greeting.

#include<iostream>
#include<string>

using std::cin;     using std::string;
using std::cout;    using std::endl;
int main()
{
    cout << "Please enter your first Name: ";
    
    string name;
    cin >> name;

    const string greeting = "Hello, " + name + "!";
        
    const int pad  = 1;
    const int rows = pad*2 + 3;
    const string::size_type cols = greeting.size() + pad*2 + 2;

    cout << endl;

    for(int r = 0; r != rows; ++r)
    {
        string::size_type c = 0;
        while(c != cols)
        {
            if( r == pad + 1 && c == pad +1)
            {
                cout << greeting;
                c += greeting.size();
            }
            else
            {
                if(r == 0 || r == rows - 1 || 
                c == 0 || c == cols - 1)
                {   
                    cout << "*";
                }
                else
                {
                    cout << " ";
                    ++c;
                }
            }
        }
        cout << endl;   
    }   
    return 0;
}

/*Example 2*/

#include<iostream>
#include<string>

using std::cin;     using std::string;
using std::cout;    using std::endl;
int main()
{
    cout << "Please enter your first Name: ";
    
    string name;
    cin >> name;

    const string greeting = "Hello, " + name + "!";
        
    const int pad  = 1;
    const int rows = pad*2 + 3;
    const string::size_type cols = greeting.size() + pad*2 + 2;

    cout << endl;

    for(int r = 0; r != rows; ++r)
    {
        string::size_type c = 0;
        while(c != cols)
        {
            if( r == pad + 1 && c == pad +1)
            {
                cout << greeting;
                c += greeting.size();
            }
            else
            {
                if(r == 0 || r == rows - 1 || 
                c == 0 || c == cols - 1)    
                    cout << "*";
                else
                    cout << " ";
                    ++c;
            }
        }
        cout << endl;   
    }   
    return 0;
}
Gerard R
  • 1
  • 1
  • 3
    And where are lines 37 & 40? – i486 Apr 09 '21 at 10:19
  • So what debugging did you do and what is eluding you? – dedObed Apr 09 '21 at 10:19
  • Did you check in the debugger how the behavior changes? Without the braces, the `++c;` is excluded from the inner `else` and performed always (whatever it means). – Scheff's Cat Apr 09 '21 at 10:20
  • 2
    Do you know what is the function of those braces? Without them, `++c` is not a part of `else` block. – Algirdas Preidžius Apr 09 '21 at 10:21
  • Do you know how many commands belong to the `else` branch with or without the braces? – Devolus Apr 09 '21 at 10:21
  • Maybe, you should consider that (in opposition to e.g. Python), the indentation is for readers eyes only but not considered by the C++ compiler. So, to group statements together, you must use `{ }`. – Scheff's Cat Apr 09 '21 at 10:22
  • @Devolus It's actually always one. – dedObed Apr 09 '21 at 10:31
  • Using Notepad, is nice for me. I still prefer to write the code on paper before putting it on notepad and before compiling. The reason I prefer this is practice and for long term memory retention through repetition. – Gerard R Apr 09 '21 at 12:30

1 Answers1

2

This:

if(r == 0 || r == rows - 1 || 
             c == 0 || c == cols - 1)    
         cout << "*";
else
         cout << " ";
         ++c;

Is the same as

if(r == 0 || r == rows - 1 || 
             c == 0 || c == cols - 1) {
          cout << "*";
} else {
          cout << " ";
}
++c;

Is not the same as:

if(r == 0 || r == rows - 1 || 
             c == 0 || c == cols - 1) {
         cout << "*";
} else {
         cout << " ";
         ++c;
}

The block of the if-body is not ended by intendation (C++ isnt Python). The body is either a single statement or a block enclosed by {}. Hence always using bracktes helps to avoid getting mislead by intendation.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • I didn't think clearly about where my incrementation was taking place. I see now that if I incriment after the else statement then the scope applies to the if as well. – Gerard R Apr 09 '21 at 10:28