Hi I'm doing an exercise on Accelerated C++ book chapter 2.. I have to create a square using the for loop by executing row by row (i think?) anyway here's the code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
usingnamespace std;
inlinevoid keep_window_open() {char ch;cin>>ch;}
int main()
{
cout << "how many padding would you like? ";
int pad = 1;
cin >> pad;
cout << endl;
cout << "Hello, enter your name! ";
string name;
cin >> name;
const string greeting = "Enjoy the show " + name + "!";
// defining the rows and columns
constint rows = pad * 2+3 ;
const string::size_type cols = greeting.size() + pad * 4 + 2;
cout << endl;
// write rows, invariant = r so we know what we've written so far
for (int r = 0; r != rows; ++r) {
string::size_type c = 0; // invariant to know how many characters we have
while (c != cols) { // is it time to write the greeting?
if (r == pad + 1 && c == pad + 1) {
cout << greeting ;
c += greeting.size();
}else { //are we on borders?
if (r == 0 || r == rows - 1 ||
c == 0 || c == cols - 1)
cout << "*" ;
else
cout << " ";
++c;-
}
}
cout << endl;
}
constint sqrrows = 4; // rows to build
const string sqrwidth = "\t"; // using tab as space between the square?
const string::size_type cols = sqrwidth.size(); // tabs' size
for (int r=0; r!=sqrrows; ++r)
{
string::size_type cm = 0;
while (cm != cols){
if ( r == 0 || r == sqrrows -1 || cm == 0 || cm == cols -1)
{
cout << "*";
}
}
if (r == 1 || r == sqrrows - 2)
{
cout << sqrwidth;
++r;
}
cout << endl;
}
keep_window_open();
return 0;
}