|
![]() |
|
Thread Tools | Display Modes |
|
![]() |
#1 | ||
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2006
Location: ,
Posts: 4,615
|
![]() Don't worry about that, first I'm not fluent at programming since I use it so little, second C is what I grew up with and although I pushed myself to learn ++, I have used C++ not at all instead of little so it's not natural in me. (And actually see no comparative advantage for small one-person projects. It may be nice to use + instead of strcat() though. LOL And C isn't certainly obsolete nor redundant, I'm not the only one to think so, whereas C++ could very well be and sure is a messy language.)
Good job, I don't think there's much more ellegant code to accomplish the same. The only thing I can think of is why you bother to declare the namespace once and again, instead of declaring once "using namespace std" at the start of the code. And why you declare main's arguments that you aren't using instead of declaring it (void)--or () which is the same. But maybe you have reasons. And sorry if I have just pointed what you know yourself after you warned me not to. Very well done.
__________________
Life starts every day anew. Prospects not so good... |
||
![]() ![]() |
|
![]() |
#2 | ||
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
![]() Thank you for kind remarks.
I use using namespace std; quite often, but as I published the code I made a bit more robust for copy and paste work by including the correct namespace. I tend to forget the possibility to omit the arguments of main. Returning a value is good practice, especially if the utility can be used in shell scripts. My Cism were (as far as I'm concerned) Code:
templine = ""; Code:
templine.clear() Code:
templine != "" Code:
!templine.empty()
__________________
Flowing with the stream of life |
||
![]() ![]() |
|
![]() |
#3 | ||
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2006
Location: ,
Posts: 4,615
|
![]() Quote:
Usually you can't be sure if you can really get away with "C-isms" and the like. Something may not be in the standard and yet your compiler may support it and others won't. (It's not the case and your code is robust.) And you're not supposed to know, because information hiding is a sought effect of object orientation. As a matter of fact using an overloaded assignment operator with a string is not a C-ism at all... Actually the robust C code to clear a string or check if it's not empty is very brief and immediately recognizable: Code:
*templine = '\0'; Code:
while ( *templine != '\0' ); But I'm rambling...
__________________
Life starts every day anew. Prospects not so good... Last edited by Japo; 10-10-2008 at 10:57 PM. |
||
![]() ![]() |
|
![]() |
#4 | ||
![]() Quote:
I have to take C and C++ classes here on the university, and it is a constant pain in the aß, as to which compiler to use. For many years, the uni used Microsoft's Visual Studio for C programming, but the license expired, and they didn't renew it. They started using Eclipse because it's free, but the course's codes, books and learning material were optimized for Visual Studio, and a great many things, what worked in VS, didn't work in Eclipse and vice versa. Just think about it, you make a project for class in C or C++, you run it and works fine, you bring it in for class and it won't work there. Who's to blame for that? Ususally us, students.:notrust:
__________________
The Master of Light and Darkness "Don't fight the bad things in life! Find the good one! They are everywhere! Don't spend your life fighting for goals you can never reach! Live for the moment!" BEWARE: I'm using the forums as a personal blog! |
|||
![]() ![]() |
|
![]() |
#5 | ||
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
![]() Quote:
Maybe i should use string.append instead of the += as well.... but it doesn't make the code look more beautiful in my opinion. In my code not intended for newbies you will find lot's of classes in which I overloaded the operators in order to keep their usage intuitive. (So you don't need to learn an interface before you can use it) You could use 0 instead of '\0' I always use 0., but this comes mainly from the fact that in C++ NULL and 0 are the same, so I can stick with one type of 0 for everything. I could ramble on about objects and stuff and as well, but that has fairly little to do with the solved problem. Thank you for your discussion so far. I really appreciate it.
__________________
Flowing with the stream of life |
||
![]() ![]() |
|
![]() |
#6 | ||
![]() I gotta get a C book... I feel left out...
__________________
The Master of Light and Darkness "Don't fight the bad things in life! Find the good one! They are everywhere! Don't spend your life fighting for goals you can never reach! Live for the moment!" BEWARE: I'm using the forums as a personal blog! |
|||
![]() ![]() |
|
![]() |
#7 | ||
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
![]() sorry play.
__________________
Flowing with the stream of life |
||
![]() ![]() |
|
![]() |
#8 | ||
![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2006
Location: ,
Posts: 4,615
|
![]() Sorry. I guess there's no point in splitting this topic in two now that one of them would be solved... :P
There are dozens of free tutorials, even whole books (tutorials are better to start). This kind of code isn't difficult so you should get the hang of it after a while. The only special thing here is that instead of taking the trouble to call specific functions to handle files, Data took advantage of the fact that C/C++ handles files and input/output devices in the same way, as "streams". However once opened files would have been interfaced with the same kind of code. The "<" in the command line call told the OS to use one file as standard input stream, instead of the keyboard by default, and the ">" to use another file as standard output stream, instead of the console. So if you for example used the former but not the latter, you should get the intended result, but printed on the screen instead of saved to a file. Other than that, this code is a good example for beginners wanting to start to learn input/output. BTW Data, there was another reason to use string.empty() and .clear() instead of == and =, the same reason why they exist at all, since you've shown that you wouldn't need the former if you used the latter instead. Not that the code would be more robust, but that it would be more efficient. I know that's not an issue here, but it's good not to let that get out of sight as a good practice, so that it will come naturally when it be important. I haven't looked inside the string object source code, but I'd bet the implementations of .empty() and .clear() are similar to what I typed, and will likely be inline functions; whereas the overloaded == and = will call functions devised to operate with whole strings so they will include a loop (even when it doesn't iterate more than once).
__________________
Life starts every day anew. Prospects not so good... |
||
![]() ![]() |
|
![]() |
#9 | ||
![]() ![]() ![]() ![]() ![]() Join Date: Jun 2004
Location: Jan Mayen, Svalbard and Jan Mayen
Posts: 2,167
|
![]() Japofran,
Yes you are right. empty and clear operations are most likely to be more efficient.
__________________
Flowing with the stream of life |
||
![]() ![]() |
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Castlevania movie script revealed | TheChosen | Blah, blah, blah... | 16 | 18-07-2008 06:41 PM |
Abandonia Script? | jourdan | Old Suggestions | 4 | 09-07-2007 09:24 AM |
Script Not Working After Migrate | Reup | Old Suggestions | 1 | 25-08-2006 11:58 AM |
Annoying Script Prompt -RESOLVED | guesst | Old Suggestions | 7 | 28-07-2006 01:00 AM |
|
|
||
  |