Home > Blockchain >  I needed some help clarifying the behaviour of a while loop within a for loop within a function that
I needed some help clarifying the behaviour of a while loop within a for loop within a function that

Time:05-03

In the function below, I don't understand the while loop's behavior. If a while loop continues until the condition is false, why does it break out of the loop and drop to the for loop below after 1 iteration?

Inside the while brackets only contains the std::cout << " " and --num_spaces, so shouldn't it print one long line of 4 " ", 3 " ", 2 " ", 1 " " before moving on?

However, when I run the program, it prints 4 " ", moves on to the remaining program and ends the line, comes back in the while loop prints 3 " ", and so on, creating the pyramid shape.

I have yet to be taught the debugger, and would appreciate some help explaining the flow.

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>

void fullStringPyramid1(std::string);

int main() {
    std::string str{"abcd"};
    fullStringPyramid1(str);

    return 0;
}    

void fullStringPyramid1(std::string letters)
{
    int position{};
    size_t num_letters = letters.length();

    for (char c : letters) 
    {
        size_t num_spaces = num_letters - position;

        while (num_spaces > 0) 
        {
            std::cout << " ";
            --num_spaces;
        }

        for (size_t j = 0; j < position;   j) 
        {
            std::cout << letters.at(j);
        }

        std::cout << c;

        for (int j = position - 1; j >= 0; --j) 
        {
           std::cout << letters.at(j);
        }
    }
    std::cout << std::endl;

      position;    
}

Output:

     a
    aba
   abcba
  abcdcba
Program ended with exit code: 0

CodePudding user response:

That while loop does not stop after one iteration, it outputs spaces, as your output shows:

while (num_spaces > 0) 
{
    std::cout << " ";  <<<<=====
    --num_spaces;
}

Output:

     a
^^^^^      <<spaces output by that loop
    aba
   abcba
  abcdcba

CodePudding user response:

There is a mistake in your code. The last two lines should be inside for (char c : letters) to achieve the output you paste.

Corrected code:

void fullStringPyramid1(std::string letters)
{
    int position{};
    size_t num_letters = letters.length();

    for (char c : letters) //{1}
    {
        size_t num_spaces = num_letters - position; //{2}

        while (num_spaces > 0) //{3}
        {
            std::cout << " ";
            --num_spaces;
        }

        for (size_t j = 0; j < position;   j) //{4}
        {
            std::cout << letters.at(j);
        }

        std::cout << c; //{5}

        for (int j = position - 1; j >= 0; --j) //{6}
        {
           std::cout << letters.at(j);
        }
        
        std::cout << std::endl; //{7}
          position;
    }
}

Now:
{1} for every letter in the given string there are the following actions
{2} calculate the number of leading spaces, at the beginning it starts from 4 spaces (num_letters=4; position=0) and decreases by 1 with every new line. Because your code position is outside the loop, position stays 0 for every letter and as the output you get one line with all letters separated by 4 spaces.
{3} the loop typing leading spaces according to the position in the string, it types 1 space, decreases num_spaces and checkes if num_spaces is greater than 0, if so it repeats adding more spaces
{4} the loop typing all letters before the current position, at the beginning it is skipped because position is 0, for next letters it types all previous letter
{5} type letter at current position
{6} the loop typing all previous letters before the current position but backward, inverted loop from step {4}
{7} go to a new line and the next letter in the string if exists

You definitely should learn debugging. It simplifies a lot of understanding of what the code does.

  • Related