CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2013
    Posts
    3

    Question Cant understand some of this simple program. PLS HELP!

    Hey guys the stuff I am going to ask about is from http://www.cs.bgu.ac.il/~sagieg/book...erated.C._.pdf page 48. Here is the complete code:

    #include <iostream>
    #include <string>
    using std::cout;
    using std::string;
    using std::endl;
    using std::cin;
    using namespace std;

    int main()
    {
    cout << "Please enter your first name: ";
    string name;
    cin >> name;
    const string greeting = "Hello, " + name + "!";
    // the number of blanks surrounding the greeting
    const int pad = 1;
    // the number of rows and columns to write
    const int rows = pad*2 +3;
    const string::size_type cols = greeting.size() + pad*2 +2;
    // write a blank line to separete 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) {
    // 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?
    if (r==0 || r ==rows - 1||
    c==0 || c == cols - 1)
    cout << "*";
    else
    cout << " ";
    ++c;
    }
    }
    cout << endl;
    }
    return 0;
    }
    things I dont understand:
    // the number of blanks surrounding the greeting
    const string::SIZE_TYPE cols = greeting.size() + pad*2 +2;
    //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) {
    // 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?
    if (r==0 || r ==rows - 1||
    c==0 || c == cols - 1)
    cout << "*";
    else
    cout << " ";
    ++c;
    }
    The semantics is clear to me just need to understand the point of it. In other words, the way the porgram works!
    Will be thankful for spending your time explaining me those stuffs.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: Cant understand some of this simple program. PLS HELP!

    Quote Originally Posted by iv0o View Post
    The semantics is clear to me just need to understand the point of it. In other words, the way the porgram works!
    Will be thankful for spending your time explaining me those stuffs.
    All the explanations were already done in the comments within this code snippet.
    If you need something else to understand how the code works then start the debugging and execute this code step-by-step looking at the watch window(s) to see what happens with the variables...
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Cant understand some of this simple program. PLS HELP!

    When posting code, please use code tags. Go advanced, select the code and click '#'

    What exactly don't you understand as Victor stated in post #2, all the explanations are already there as comments within the code.

    As chapter 2 of the book you are using states, the purpose of the program is to draw a frame around a line of text. What the book doesn't describe is the program design in English from which the program can be coded into the c++ programming language. As you say, you understand the syntax but not how it works. This is the design of the program. I would suggest first that you forget about the code for a moment. Think about the purpose of the program and write in English exactly what is required to achieve the required output. How do you work out how many stars are required to be output for the first and last lines? Once you have the program design and know how it is supposed to work, then you can compare the design to the code and use the debugger to trace through the code looking at the variables etc too see how the program accomplishes its task compared to the design. Being able to understand already writtten code is a skill that needs to be learnt as part of learning to program - like being able to use the debugger. If you still don't understand the code, try coding it yourself from scratch from your program design and once you get your program to work correctly, compare your code to the code from the book and see how it is different and why.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured