0

The while loops I wrote while working through a book example produce the first line of output correctly, but then the loop ends. How would you go about "deugging" this program? In other languages I would litter my code with console.log() or similar outputs, but I am not sure how to accomplish this with a C++ console project I set up just to begin learning C++.

#include <iostream>
#include <string>

int main()

{

    // ask for the name
    std::cout << "Please enter your first and last name: ";

    // read the name 
    std::string name; // define name
    std::string last;
    std::cin >> name; // read into name
    std::cin >> last;

    // build our message 
    const std::string greeting = "Hello, " + name + " " + last + "!";
    const int pad = 1;
    const int rows = pad * 2 + 3;
    const std::string::size_type cols = greeting.size() + pad * 2 + 2;
    std::string::size_type c = 0;


    // separate the output from the input by one line
    std::cout << std::endl;

    // write rows of output
    int r = 0;

I'm including the full program, but the while loop where I think the problem lies is below.

    // invariant: we have written r rows so far 
    while (r != rows) {
        // write a row of output
        // invariant: we have written c characters so far 
        while (c != cols) {
            if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1) {
                // this is the only line that gets written to the console 
                std::cout << "*";
                ++c;
            }
            else {
                // write non border characters
                //adjust the value of c to maintain the invariant
                if (r == pad + 1 && c == pad + 1) {
                    std::cout << greeting;
                    // account for the characters written to maintain the c invariant
                    c += greeting.size();
                }
                else {
                    std::cout << " ";
                    ++c;
                }
            }
        }

        std::cout << std::endl;
        ++r;
    }

    return 0;
}

I thought it might be the std::cout << std::endl; that comes before the ++r. If it is flushing any of the other input? So, I removed it and found the program wrote the full first line then wrote the std::endl and then ended execution. I have also ran the while loops through my head and the logic seems sound to me, maybe I'm missing something glaring though?

To sum up my question, why does the execution of my program stop after the first iteration?

Pfrex
  • 159
  • 2
  • 13
  • 2
    _"In other languages I would litter my code with console.log() or similar outputs"_ So why not simply using `std::cout` to do so? – πάντα ῥεῖ Apr 06 '19 at 20:17
  • Huh, yea you can do that... awkward. But, that allowed me to see what my issue was and I never set c = 0 after incrementing r! I'll accept your answer, or maybe this question should get deleted? – Pfrex Apr 06 '19 at 20:23
  • See [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), or consult a [good book](https://stackoverflow.com/q/388242/9254539). – eesiraed Apr 06 '19 at 23:44

0 Answers0