Циклы while, которые я написал во время работы с примером книги, правильно выводят первую строку вывода, но затем цикл заканчивается. Как бы вы занялись «отладкой» этой программы? На других языках я засорю свой код с помощью console.log () или аналогичных выходных данных, но я не уверен, как это сделать с помощью консольного проекта C ++, который я настроил, чтобы начать изучение 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;
Я включаю полную программу, но цикл while, где, как мне кажется, проблема кроется ниже.
// 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;
}
Я подумал, что это может быть std :: cout << std :: endl; что предшествует ++ r. Если он сбрасывает какой-либо другой вход? Итак, я удалил его и обнаружил, что программа написала полную первую строку, затем написала std :: endl и затем завершила выполнение. Я также запускал циклы while в моей голове, и мне кажется, что логика звучит здраво, может, я что-то упускаю из виду? </p>
Подводя итог моему вопросу, почему выполнение моей программы останавливается после первой итерации?