help with a exercise of Accelerated C++

This is a discussion on help with a exercise of Accelerated C++ within the C++ Programming forums, part of the General Programming Boards category; Hello, I have this exercise : The framing programm writes the mostly blank lines that seperates the borders from the ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    help with a exercise of Accelerated C++

    Hello,

    I have this exercise :

    The framing programm writes the mostly blank lines that seperates the borders from the greeting one character at the time. Change the programm so it writes all the spaces at one time.

    So i thought this solution will work.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    	// ask for the person's name
    	cout << "Please enter your first name: ";
    
    	// read the name
    	string name;
    	cin >> name;
    
    	// build the message that we intend to write
    	const string greeting = "Hello, " + name + "!";
    
    	// the number of blanks surrounding the greeting
    	int pad ;
    	cout << "hoeveel spaties tussen de rand en de tekst :";
    	cin >> pad ;
    	const int pad2 = 2 ;
    
    	// the number of rows and columns to write
    	const int rows = pad2 * 2 + 3;
    	const string::size_type cols = greeting.size() + pad * 2 + 2;
    
    	// write a blank line to separate 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 == pad2 + 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
    					string z(pad," ");
    					cout << z ;
                    c=+pad ;
    			}
    		}
    
    		cout << endl;
    	}
    
    	return 0;
    }
    But I see now that z is not declared in this scope.

    What is my thinking error ?

    Roelof

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Within your loop, you have this
    Code:
                                    if (r == 0 || r == rows - 1 ||
    				    c == 0 || c == cols - 1)
    					cout << "*";
    				else
    					string z(pad," ");
    					cout << z ;
    The last line is not executed as part of the "else" clause, even though - from your indenting - you apparently think it is.

    So, as part of the "else" a string named z is created, and immediately destroyed before you attempt to print it.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I changed that part to :
    Code:
    else {
    					string z(pad," ");
    					cout << z ;
    }
    But now I get this message : C:\Users\wobben\Desktop\borland-source\chapter02\frame.cpp|53|error: invalid conversion from 'const char*' to 'char'|

    Roelof

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Then look at the types of your variables. You're doing something where the a pointer is expected, and supplying a char.

    If you look up the constructors of the string type (or, more accurately, basic_string) there are none that accept an integer as the first argument and a pointer as the second argument. The string literal " " is being implicitly interpreted as a pointer to char.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I thought this could work by this text :

    string z(n, c)
    Defines z as a type string that initially contains n copies of the caracter c.
    Here C must be a char, nor a string or a string literal.

    Roelof

    Edit : I found it . If I change it to z(pad1, char(32)) it works.'
    Last edited by roelof; 06-11-2010 at 04:54 AM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I have not this code :
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    
    int main()
    {
    	// ask for the person's name
    	cout << "Please enter your first name: ";
    
    	// read the name
    	string name;
    	cin >> name;
    
    	// build the message that we intend to write
    	const string greeting = "Hello, " + name + "!";
    
    	// the number of blanks surrounding the greeting
    	int pad ;
    	cout << "hoeveel spaties tussen de rand en de tekst :";
    	cin >> pad ;
    	const int pad2 = 2 ;
    
    	// the number of rows and columns to write
    	const int rows = pad2 * 2 + 3;
    	const string::size_type cols = greeting.size() + pad * 2 + 2;
    
    
    	// write a blank line to separate 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 == pad2 + 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 << "*";
    					c++ ; }
    				else
    				  if ( r==pad2-1 || r==pad2 ||
                           r==rows-1 || r==rows-2) // is this a blank line with only * and blanks ?
                         {
                             string z(cols-2, char(32));
                             cout << z ;
                             c+= cols -3 ;
                         }
                        else   // is this the line for the greeting. If so print the padding
    				{
    					string z(pad,char(32));
    					cout << z ;
                        c +=pad ;
                        }
    			}
    		}
    
    		cout << endl;
    	}
    
    	return 0;
    }
    But now I see only the blank lines.

    Roelof
    Last edited by roelof; 06-11-2010 at 05:36 AM.

  7. #7
    rvr
    rvr is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    try using:
    cout << greeting.c_str()

    enjoy,
    r

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

    I don't think that's the problem.
    I have tested further and I think c+=-3 is the problem.
    If I change it then on different numbers I see the problem.

    I have now this : c += cols - pad2 ; and then it's going wrong if de padding is bigger then 3.

    Roelof

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    [EDIT] This is incorrect. Evidently not enough coffee at this point in the day

    Think:
    Code:
    			if (r == pad2 + 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 << "*";
    					c++ ; }
    				else
    				  if ( r==pad2-1 || r==pad2 ||
    You only have two real possibilities for each iteration, the first if and the first else, because the else has no condition. That means if the if is not true, the else will happen. So the other else/else if's you have can never happen.

    Here's how an else-if sequence works:
    Code:
    if (condition1) ...
    else if (condition2) ...
    else if (condition3) ...
    else if (condition4) ....
    else {  // if none of the above conditions are met
    What do you think would happen if I swapped line 5 for line 2 or line 3 here?
    Last edited by MK27; 06-11-2010 at 10:22 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    rvr
    rvr is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    Do you still have
    c += pad
    inside the "else"-clause?

    In your first code-snippet it wasn't inside...

    r.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    230
    hello,

    I don't follow your suggestion.
    Code:
    if (r == pad2 + 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 << "*";
    					c++ ; }
    				else
    				  if ( r==pad2-1 || r==pad2 ||
    If (r==pad2 and on is False then if (r=0) is carried out. If this is False for me if (r==pad2-1) is carried out.

    Or im a wrong here.

    Roelof

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I'm going to start all over again.
    This is a big mess I believe.

    Roelof

  13. #13
    rvr
    rvr is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    3
    maybe you'd try something like this:

    Code:
    	std::vector< std::string *> screen;
    	std::string vertical_line; vertical_line.resize(cols);
    	std::string empty_line;	empty_line.resize(cols);
    	std::string greet_line;	greet_line.resize(cols);
    
    	std::fill( vertical_line.begin(), vertical_line.end(), '*' );
    	std::fill( empty_line.begin(), empty_line.end(), ' ' );
    	
    	empty_line[0] = '*'; empty_line[ empty_line.size() - 1 ] = '*'; 
    
    	greet_line.assign( empty_line );
    	greet_line.replace( pad, greeting.size() , greeting );
    
    	screen.push_back( &vertical_line );
    	for (int i=0; i!=pad2; ++i)
    	{
    		screen.push_back ( &empty_line );
    	}
    	screen.push_back( &greet_line );
    	for (int i=0; i!=pad2; ++i)
    	{
    		screen.push_back ( &empty_line );
    	}
    	screen.push_back( &vertical_line );
    
    	for ( std::vector<std::string*>::const_iterator it = screen.begin(); it!=screen.end(); ++it )
    	{
    		cout << (**it) << endl;
    	}
    Have fun!

    r.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roelof View Post

    Or im a wrong here.

    Roelof
    No, you're not wrong. Sorry about, I must have been writing with my eyes closed.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    230
    oke,

    I have one problem.

    I trying to solve this and try to make the problem visible.

    If I not mistaken.

    If a user wants 2 lines between the border above and the greeting then rule nr. 2 is the rule with a * as start , then a lot of blanks and then a "*"

    If a user wants 3 lines between the border and the greeting the rule nr. 2 and 3 are the rule with * , a lot a blanks and a *

    So the nr. of rules with a *, a lot of spaces and a * are between 2 and the number the user inputs.

    So i thought this would work.
    Code:
    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 == rules + 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 << "*";
                            c++ ;
    				    }
    				else
    				    {
                                                                           for (teller=2; teller=rules; teller++
                                                                           { //write the spaces }
    				    }
    
    
    			}
    		}
    
    		cout << endl;
    	}
    
    	return 0;
    But it don't work.
    Where am i thinking wrong.

    rvr : your solution can be right but you use things that I have to learn in chapters to come.
    So no solution for me

    Roelof

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exercise 5.10 (ACCELERATED C++)
    By pantera in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2010, 09:46 AM
  2. Replies: 2
    Last Post: 05-02-2010, 02:49 AM
  3. Help with Accelerated C++ exercise
    By s_siouris in forum C++ Programming
    Replies: 5
    Last Post: 01-24-2010, 03:10 PM
  4. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:34 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21