Well, i have just bought Accelerated c++.
atm im at chapter 2(not that it matters )
I have just rewritten a program and i have gotten this code:
Code:
#include <iostream>
#include <string>
// say waht standard library names we will use
using std::cin; using std::endl;
using std::cout; using std::string;
int main ()
{
// ask for the persons name
cout << "Please enter your first name: ";
// read the name
string name;
cin >> name;
// build the message we intend to write
const string greeting = "Hello, " + name + "!";
// ask for how many lines from the greeting to the top and the bottom.
cout << "How many lines do you want from the greeting to the frame?: ";
// read the lines
string::size_type lol;
cin >> lol;
const int pad = lol;
// ask for how many spaces from greeting left to right
cout << "How many spaces between the greeting and the frame?: ";
//read the spaces
string::size_type omg;
cin >> omg;
const int cols = greeting.size() + omg * 2 + 2;
// the number of rows and columns to write
const int rows = pad * 2 + 3;
//write a blank line to seperate the output from the input
cout << endl;
string::size_type c = 0;
//write a blank line to seperate the output from the input
cout << endl;
// write rows rows of output
// invariant: we have written r rows so far.
for (int r = 0; r != rows; ++r) {
string::size_type c = 0;
// invariant: we have written c characters so far in the current row.
while (c!= cols) {
//it is time to write the greeting?
if (r == pad + 1 && c == pad + 1) {
cout << greeting;
c += greeting.size();
} else {
// are we on the border?
if (r == 0 || r == rows - 1 || c == 0 || c == cols -1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
}
return 0;
}
Then this exercise came up:
The framing program writes the mostly blank lines separate the borders from the greeting one character at a time. change the program so that it writes all the spaces needed in a single output expression.
Im not sure what I should do right now.
I need some explanation..