If you only have a hammer...or [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 13 years ago.
I need to download some csv files over http from the internet, parse it and convert it to a more useful fomat. Eventually a C++ program will consume the data. A few years ago, I would be pulling out my Perl books and start writing Perl scripts to do the downloading and parsing. But now with Boost and Qt I can do the download, parsing, and throw in a GUI front end in C++ with very little effort. Last time I wrote Perl/Python was about 6 months ago. It will probably take me longer to do it in Perl/Python and my Perl/Python code will be crap. If the only tool I have is a hammer, everything looks like a nail? Or time has changed and C++ can be productive in area traditionally dominated by script languages such as Perl or Python?

What does it matter what other people might usually expect the solution to be? If you can get the work done better and faster in C++, do it in C++, end of story.

For your example it seems that you will be more productive and able to solve your problem more easily using C++ as that is what you know.
But generally I would expect that more people would use python/perl for this kind of task.

Why not?
If you're code is going to be hacky because of lack of libraries/features, then C++ might not be best.
If it needs to change often, then C++ might not be best.
If others who know Perl/Python will maintain it, then C++ might not be best.
etc.
Do you really need a GUI? Do you need the speed? Nothing has changed to make C++ a scripting language, but that doesn't mean you shouldn't use it.
If it passes common sense tests like this, than why not?

Well, if you have to build a house, a hammer will work just fine. But building a house using a pneumatic framing nailer is much easier.
C++ is a fine language and you can be productive in areas that are dominated by scripting languages, but all things equal, you will be more productive using perl/python for text parsing.

The issue of productivity of C/C++ vs. python/perl seems irrelevant to me. If you want to write your parser using Qt, Boost, and any other off the shelf tool sets, do it. You didn't actually specify if the speed of parsing is an issue, but even if it was, would it even matter for your case.
Even if you think that it might be easier in language X, write it in language Y if you want to try and learn something new about that language. It sounds like a pretty easy task so just write it how you want to write it.
You also need to think about future use. If this program will need to be enhanced and extended in some way, that might dictate a specific language choice.

If you're more productive in C++ then by all means use C++. It's still a good idea to learn other languages, but sometimes you need to go with what you know to get things done.
BTW, you probably already know this, but the Boost.Tokenizer library has CSV parsing capabilities built-in through the escaped list separator.

From your description, it sounds like you have other tools besides a hammer. The more tools you have, the more efficiently you can get things done. Use whatever tools you have at your disposal to get the job done, and occasionally buy (learn) more tools.

I like C++. I hate the compile, run, test, close cycle - specifically when I need to test data. My solution: I made a small programming environment [powered by Lua] which I embed in my C++ applications and I open it at run-time and do stuff interactively. Why to stick at one programming environment or another? Use the best of both worlds.

C++ was not specially designed to handle long strings of text. (Just look at the old C-Strings... they were a nightmare for anything serious.) By contrast, Perl was made for this sort of use.
You can probably cruft together something in C++, but a Perl solution will probably be more robust and maintainable.

Related

New to C++, need useful examples [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am new to Stack Overflow, and new to programming.
I am learning how to program in C++.
My question is not related to specific code, but is about research and learning the language.
What I have learned so far relates to narrow examples of syntax and simple programs which use variables, functions, arrays, etc.
I am wondering if people have or can link to example programs so I can study them.
I'm looking for console programs which:
use variables (int, double, string, etc)
use functions
use arrays use classes with
attributes and methods use objects of that class
reads and writes to a file
validates user inputs, displaying appropriate error messages
and is basically a useful program.
Through Google, I have mostly only been able to find C++ tutorial pages (cplusplus, cprogramming, etc) which deal with each of the above separately, usually in a bare-bones way to show the syntax. I'm looking for something more complex (but not overly so) so I can learn how to combine these things in a meaningful way with the intent of eventually writing programs of my own at the same level.
I've already coded a calculator (though not one that has all of these features; namely it was missing file i/o and I was able to make a basic one which didn't need objects), so I'm looking for something different. I understand console programs are text based and lend themselves well to these kind of programs, so it can be a calculator of another type, as long as it isn't a basic arithmetic one.
People here won't teach you C++. In fact, even a book or Google by themselves won't exactly teach you any language, they are just tools to make your life easier and the studying curve smoother.
My suggestion is to use Google or a good C++ book and write code.. especially write code, otherwise you won't learn anything, you must get your hands dirty in order to learn C++.
cplusplus.com has a few examples.
As #GMan said, you'd be better off reading a book.
Possibly Effective C++ by Scott Meyers, or maybe one in the Beginner\Introductory section.
The best way to improve is to give yourself a task and code it. Use different techniques/paradigms (OOP, modular, etc). Instead of studying programs, try to create them yourself - you'll learn a lot better this way.
The book can guide you, but you must make the journey.
Here are some exercises. You can try solving puzzles, too. CodeGolf.SE is good if you want to have some fun.
I hope you've got an excellent book. :)
That said, I understand the desire to find source code you can read that's larger than toys, but not giant cathedrals of code; perhaps the AppArmor policy parser can be of service to you. It's a little bit involved, because it's a small-language compiler that builds a DFA of a security policy for 'execution' in the kernel when confined programs perform file operations.
You can clone it with bzr: http://bazaar.launchpad.net/~apparmor-dev/apparmor/master/ or you can browse the source code: http://bazaar.launchpad.net/~apparmor-dev/apparmor/master/files/head:/parser/libapparmor_re/
Supplementing the other posts directing you to various reading, if you are new to programming, I highly recommend starting your venture into programming with Python.
Python is an easy-to-learn programming language that has a LOT built-in and will allow you to start making useful "programs" very quickly.
For example, you can read the entire contents of a text file with a single line of code:
file_contents = open('example.txt','r').read()
I feel it keeps people more encouraged when they can see significant results as they learn. If you are interested, Dive Into Python is a very popular tutorial.

Start with some C++ framework or python [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I am thinking about to learn new language or framework. Now I deal with C# and WPF, WCF, Winforms. I have some free time so I would like get new skills.
But I have dilema, start with some C++ framework (such as Platinum, Reason, Evocosm, ACF)
or try Python / python framework.
I you are on my place which possibility you choose?
I am 17 years student, I have basic skills in C++ (OOP, little with STL), with Python I haven’t any experience.
What would be your choice and why?
Python is incredibly easy to learn, very powerful, and very useful (especially for web development IMO).
C++ is very widespread and is similar to other languages like C# and Java. Python's syntax is quite unique and AFAIK is not very similar to any other languages. Nonetheless, Python syntax is far easier to read and cleaner to write than C++.
Python is also fairly widespread and is used by huge companies like Google.
C++ has some wonderful libraries that can really help development, like Boost and Qt. Python also has excellent libraries like Django (for web development) and Twisted (for networking stuff). I find that C++ libraries tend to be broader in scope than Python libraries do, but the Python standard library is far better than the STL, IMO.
Python recently had a backwards-incompatible release (Python 3), and most libraries haven't caught up yet, so you might have to use Python 2 to take advantage of some of Python's libraries.
C++ is a lot faster than Python in terms of execution speed, but Python more than makes up for it in terms of development speed. Plus, Python allows you to write performance-critical parts of your application in C or C++.
Overall, I'd say go with Python, mostly because it's easy to learn and use, and makes programming simple and fun.
I recommend you to keep learning C++. Before you started looking for framework learn some popular search algorithm and try implement them. After that try implement some structures, like queues, list, stack, binary trees and some operation on them. Meanwhile play with I/O (for example, try write your stack to file and read it back to stack - in plain text and binary).
It was my university tour on programming class. C++ is good choice because it is hard and multiparadigmats language so in future u will find much in common with other languages and you will be familiar with memory management system.
Personally I prefer Python, but profesionally, that is if you want a good job C++ is a better choice.
I'd pick C++ for the sole reason that it's nothing like the languages you already know, even though it shares some syntax.
I strongly recommend you at least learn a bit of Python first, even if you decide stay in C++.
One may say C++ is the most powerful program language.
However, for a starter, there are too many traps and pitfalls.
In your situation, Python is something that can let you know more about programming.
Since you have no experience in Python, It's very easy to learn at the beginning.
And it will show its effect very quickly.
Plus, learning it will help you understand C++ more, like STL and Boost, they all have similarities with Python. Qt even has PyQt as a Python bindings. They have the same API, while the syntax in Python is clean and elegant.
Here is a remarkable tutorial for Python: http://diveintopython3.org/
I suggest you just try it a bit, it wouldn't hurt.
Well. Learning Python basics will take a week, and you will save the time spent in a year since it is such a good language for small hacks and scripts. So I suggest you learn it first.
Learning C++ well will take you five to ten years, so there is not the same immediate benefit :)

Learning from open-source projects / Browsing already written code [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I was wondering how valuable open-source projects are to learn from?
I still consider myself "beginner" due moreso to lack of experience than lack of knowledge- I've read many C/C++ tutorials and taken classes and such, but I just really lack experience. Therefore, even though I understand the major data types and coding techniques, I don't have a solid grasp on what approach to take to use them in my own programming. Therefore, while I continue to read/practice/learn, I have been downloading lots of open-source code (random applications, emulators, games). It is worthwhile looking at them to help learn? I find it extremely interesting, but more often than not just get lost.
Second question, where does one usually start when doing this? Do you hunt down a main() function somewhere? Do you look at headers to see what functions will be available throughout the code and get an idea of what is around to work with?
Please let me know!
R
I personally wouldn't recommend the reading of the source code of open-source projects to a beginner, especially if they're mature projects. It may be overwhelming for a beginner since they tend to be rather large projects with thousands of lines of code, most likely of a non-trivial design.
If you lack experience, then the best way to gain experience is by writing your own programs and taking on your own projects that are of interest to you, in my opinion. You can certainly read other people's code to see "how it's done", but actually trying to implement those ideas yourself in practice does more to help you understand how to write code than just passively reading code. In turn, the gained understanding and experience will allow you to make better sense of other people's code.
It's sort of like math; you may know the formulae, and you can see how mathematicians/teachers/professors/etc. use those formulae, but you won't really understand them until you try them out yourself. Once you do understand them, then the kinds of things mathematicians write will make much more sense.
Try to focus on things you want to do, there's not a lot of point in looking at code for an application that you have no reference point for.
The best place to start would probably be to look at a project like Boost
But formulate a series of application tasks that you'd like to investigate, perhaps graphics, text editing or socket programming... and then work from there.
Getting a good IDE or programmers editor that will help you navigate the code is a major plus.
For example, Emacs + ECTAGS/CEDET/Semantic will help you browse all the functions / classes in a C / C++ project.
I'm agree with #In silico. It's very useful to see other's code, but only when it's a little bit over your level, so that you can learn something. I've seen quite a few projects that were too "over-engineered", so that learning from them when you can't really tell the good from the bad will be a bad idea.
Another thing is to learn from another programmer, when you could ask why he did one way and not another. In this case the difference in levels does not matter.
So I'd suggest programming by yourself, and looking on the other people's code for the same thing after you've tried it. In this way you'll be able to compare the choices you've seen and the decision you've made with someone else (when you don't know a problem in depth, any suggested solution would seem right). You know, In theory, theory and practice are the same. In practice, they are not.

What C++ projects should I start? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
I'm a high school student, and I have a decent amount of programming experience (HTML, Javascript, PHP, Actionscript 3.0). I know C++, but unlike the other languages I know, I have never actually made any decent sized projects with it. I am puzzled at what kind of project I should start, as there are so many things you can do with C++.
I just want some experience making something - but right now I don't know what I want to do!
HELP ME!
Find some open source projects and offer your help. You get to see other's code and have a goal on what to develop.
Find something that interests you. That will keep your attention the longest.
Pick something you're either passionate in, or something that might help you at home.
If you do that, then you have a vested interest in it rather than it being just another application.
For me, I wrote a small system to keep track of my customers. Now I know there are any number of applications that do that but I just wanted something small and custom so it really worked for me.
After that, when it was working, I then went back and refactored etc.
I see this question quite often. My reply is always the same. Programming is about scratching an itch. First find something that interests you and doesn't have a solution (or at least a good one). That's the itch.
Next sort out the best solution. It may be C++, .Net, Java or any one of a hundred other languages. The key is to pick the right tool for the job. If you try and push the square language in the round problem you will fail.
Programming is not about any one language. It's about finding the best solution to a problem and implementing it well using the right tools. This is what makes the difference between someone who knows a programming language, and someone who solve problems. The first are a dime a dozen, the second and worth their weight in gold.
Write a bitmap parser and viewer entirely from scratch, don't use any existing libraries. If you want an even bigger challenge, write a jpeg parser, that will require writing a huffman decompression algorithm and all sorts of other goodies.
I did this in java recently for a challenge, the program was rubbish but the amount of things I learnt made it worth while.
You could geek it up and create a complicated RPG (Maybe start with a simple form or console based game to get logic than maybe move on to directx or something).
I made an RPG myself but really never got to directx... yet. It can be fun because the game can be about whatever you want, your imagination is the only limitation. Figuring out the logic is a great way to stimulate your brain and learn new techniques in the process.
Try and find a copy of The OpenGL Programming Guide and have fun with some graphics programming. Old editions can usually be had cheaply.
The examples are all in C, not C++, but its good to get some experience in C too.
Also, see if your school or community hosts programming contests. One of my first large programs was a game for a junior-high level contest (though that was a long, long time ago).

What does C++ add to C? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
What does C++ add to C?
What features of the language are the Clang/LLVM projects, the parts of GCC that are being written in C++, chromium, and any others all taking advantage of? What features are they avoiding?
Like all sophisticated and powerful things there is a price to be paid to succeed in C++.
You have to be incredibly careful with memory management.
Multi-paradigm capability means you have to be really good at design to avoid making a mess.
Extreme performance requires careful planning and selection of features used.
The ability to circumvent most every language policy requires monumental self discipline.
So if you're sloppy with memory, poor at design, don't need fast programs, or have no self discipline, then please don't learn C++. There is always Java or C#.
meta programming? templates?
like with C you get performance, but the code looks horrible.
with the high level languages you get nice code but there is less flexibility to make the fastest possible code.
with c++ you can do both? you can freely make anything as fast as it could be made in C, but native object orientation, and templates/operator overloading ect makes it so you can write fairly nice looking code too. indeed, you can make it so it is neat and fast.
I have never really found it more of a pain to write stuff in c++ than in a higher level language. the trick is having good libraries.
Because despite academic efforts such as Singularity, there's not a single mainstream OS where drivers can be written in a high-level language.
Note that anything that can be done in C++ can also be done in C, but some things are a lot easier in C++.
Not? I would say it's not worth if you performance is not an issue for you. (Follow the double negatives.)
My two cents:
Although I don't program in Python, I would have to say that Python is probably the best programming language for getting real work done. It's an elegant language, and it has an enormous collection of libraries for doing various things. However, my experience as a user has shown me over and over again that Python is slow (take yum, for example).
I do know Haskell pretty well, and I have to say that it's a friggin' awesome language. Better yet, it is compiled, and its speed is competitive with Java and C++ (though you have to put forth extra effort to get this speed in some cases). However, libraries for things like database access don't always match the elegance of Haskell's base libraries (I'm probably way wrong about this), and they're harder to install on Ubuntu. In my opinion, that's why it's more challenging to get real work done in Haskell than in Python.
Ruby's good for web applications. Other than that, it's slow (though I speculate jRuby or something might be faster).
C++ is far from elegant, and in many cases, elegance is frowned apon. Anyone ever told you to use static_cast instead of C-style casting? Anyone ever told you not to use namespace std;? C++ has a lot of features, but doesn't tend to have many important language features (such as closures, which are formally proven to be the best thing since sliced bread).
Why do people use C++, then? Well, it's performance-focused, making it a good choice when you need speed. It has classes, namespaces, and templates, so it's a good choice when you want better code organization, but still need to use "C" for some reason. Also, it has the Boost library, which I've heard is really good for getting work done.