Please help with little C++ program...

Started by
11 comments, last by TheUnbeliever 16 years, 2 months ago
Quote:Original post by musafir2007
^^^ Thanks a lot man!! but can you please tell me a little bit more about.. string line(length, ch);?
thanks!!


Sure... it creates a string containing 'length' characters 'ch'.
So one of the constructors of a string is something like this:

string::string(int n, char ch);


Which means you can give two arguments, the first is the length of the string and the second is the character used to fill the string.
Advertisement
Quote:Original post by Gage64
First, this is probably a homework question, so you are not allowed to just post a solution. It's also more educational for the OP if he figures it out for himself.

Second, the main point of this exercise is to practice loops, and by using std::string, you're bypassing that completely.


Sorry, I didn't know about that rule, thanks for pointing it out. I can see your point about being it a homework question. About your second point, if it isn't a homework question my solution would be nicer.

Anyway, if it is a homework question about loops, then the topic starter can figure out for himself how the innerloop should be constructed.
Quote:Original post by TheFlyingDutchman
This way you also avoid the innerloop, which is just a bit faster and it gives nicer (easier to read) code.


Well, you avoid writing the inner loop. It's still there, in the std::string constructor (or, more likely I suppose, in a function it calls – probably std::fill), so the performance (which is something of a non-issue here) won't be significantly different.
[TheUnbeliever]

This topic is closed to new replies.

Advertisement