Tuk

This simple program takes a users name and a padding value; and creates a frame containing a greeting(see console output). The padding value is used to decide how many spaces exist between the frame and the greeting.

My question is: There are two ways the program could check to see if its time to write the blank space on the left/right side of the greeting. Note: this check only occurs if its known that an '*' or the 'Greeting!' will not be written.

Which Method is best

Code Snippet

1) IF on the same row as greeting AND in the second column OR in the column directly after the greeting Then write blank space.

if (r == pad + 1 && (c == 1 || c == (greeting.size() +

(pad +1 ))) )

2) IF on the same row as the greeting then write blank space

if (r == pad + 1)

Console Output:

Please enter your first name: Brizo
Enter value for padding: 5

*************************

* * # r == pad + 1 == 6
* * # c == (greeting.size() + (pad +1) == 20
* *
* *
*# Hello, Brizo!# *
* *
* *
* *
* *
* *
*************************

Full Program (code in question highlighted in RED)

// Framed Greeting with 'blank spaces' being written in a 'single output expression'

#include

#include

#include

using std::cin; using std::endl;

using std::cout; using std::string;

int main()

{

// ask for persons name

cout <<"Please enter your first name: ";

// read the name

string name;

cin >> name;

// ask user for padding value

cout <<"Enter value for padding: ";

// read the padding value

int pad;

cin >> pad;

// build the message that we intend to write

const string greeting = "Hello, " + name + "!";

// the dimensions of the frame defined as the number of 'rows' and 'cols' to write

const int rows = pad * 2 + 3;

const string::size_type cols = greeting.size() + pad * 2 + 2;

// Calculate and builds the 'blank space'between the borders and the 'blank space'on either side of the greeting

const string blankLine((greeting.size() + pad * 2), ' ');

const string greeting_padding(pad, ' ');

// write a blank line to seperate the output from the input

cout << endl;

// write 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) {

// is it time to write the greeting

if (r == pad + 1 && c == pad + 1) {

cout << greeting;

c += greeting.size();

}

else {

// are we on the border , is it time to write a '*'

if (r == 0 || r == rows -1 ||

c == 0 || c == cols -1) {

cout << "*";

++c;

}

// is it time to write the greeting pad

else if ( r == pad + 1 && (c == 1 || c == (greeting.size() + (pad + 1))) ) {

cout << greeting_padding;

c += greeting_padding.size();

}

// it must be time to write the blank line

else {

cout << blankLine;

c += blankLine.size();

}

}

} // close while()

cout << endl;

} // close for()

getch();

return 0;

} // close main()



Re: Visual C++ General Simple code: Which way is best practise?

Tuk

I decided to edit my post in an attempt to make my question more clear and concise.



Re: Visual C++ General Simple code: Which way is best practise?

Tuk

Bump!...is there a reason why nobdy has replied

Is there too much to read ...
Is the question unclear ...
Is it not very interesting ....
Other




Re: Visual C++ General Simple code: Which way is best practise?

crescens2k

I think it's because this is just a style question with no best answer. In cases like these you should excercise your own judgment and use what you think is the better choice.




Re: Visual C++ General Simple code: Which way is best practise?

Simple Samples

Tuk wrote:

Is there too much to read ...
Is the question unclear ...
Is it not very interesting ....
Other

All of the above, sorry.