|
I'm trying to do some homework that is covering overloaded functions with default arguments, and I'm a bit confused. My professor has me write two functionscode:
code:
code:
code:
Thanks!
|
# ? Jul 20, 2009 18:28 |
|
|
# ? Jun 7, 2024 10:38 |
|
I am currently learning C and I have a quick question. I want to use a system call to print the date command from within my program. My code looks like this: code:
code:
code:
|
# ? Jul 20, 2009 18:36 |
|
chips posted:I'm not sure how you'd generically copy the argument lists, though. code:
|
# ? Jul 20, 2009 18:42 |
|
xwonderboyx posted:I want to use a system call to print the date command from within my program. Get rid of the printf entirely. Just call system.
|
# ? Jul 20, 2009 18:44 |
|
system() just executes date which prints the time itself. system() then returns 0 which gets converted into a null pointer which printf prints as "(null)" You need to use ctime(time(NULL)) or something.
|
# ? Jul 20, 2009 18:45 |
|
xwonderboyx posted:Any suggestions? Use the standard date time functions available in <time.h>. system("date") prints a date, it doesn't return a date.
|
# ? Jul 20, 2009 18:45 |
|
EDIT: I messed up, the edit button and quote/reply button are not the same thing
Richard M Nixon fucked around with this message at 18:57 on Jul 20, 2009 |
# ? Jul 20, 2009 18:49 |
|
Now you've gone and done it. I'm just going to have to recommend Boost.Parameter.
|
# ? Jul 20, 2009 18:51 |
|
ShoulderDaemon posted:Get rid of the printf entirely. Just call system. This is the answer that works best for me! I appreciate all the other help as well, but for what my current assignment is I didn't want to use anything in <time.h> Thanks!
|
# ? Jul 20, 2009 18:52 |
|
livelikecode posted:thus reiterating (technical word for this?) the default argument? Usually people would say "providing". Obviously this is for a class, and you have to do what your professor wants, but as a quick note — you should really never use overloading in conjunction with default arguments in real code. xwonderboyx posted:But how do I get it to stop printing (null)? You should be compiling with warnings on. EDIT: removed redundant answers rjmccall fucked around with this message at 18:56 on Jul 20, 2009 |
# ? Jul 20, 2009 18:53 |
|
livelikecode posted:Am I able to call code:
PrintOutput(2,4,true); PrintOutput(3,1,2,false,fout); PrintOutput(3,2,3); etc... If his real true goal is to explore the world of overloading (which I think it is, because the 2-int / 3-int overloading is very questionable as is), and if he really wants you to use minimal output for each call (ie. you'll be graded on it), // short and cout(default) PrintOutput(1,2,true); // fout and long(default) PrintOutput(1,2,fout); But like I said it might serve as a good exercise for seeing what you can do with overloading, but in practice: readability >>>> # of arguments. EDIT: I'm dumb and forgot the compiler throws a fit with that level of ambiguity, so it's probably best to just reorder the arguments so that the defaults that get changed the most (if you can make that distinction) are closer to the front. EDIT2: oh ya, short is a keyword so you'll have to rename that bool Bhaal fucked around with this message at 22:49 on Jul 20, 2009 |
# ? Jul 20, 2009 20:00 |
|
Avenging Dentist posted:Now you've gone and done it. I'm just going to have to recommend Boost.Parameter. More like Boost.Parentheses (Boost.ParlorTrick?). If the example code for your library ends up being that ugly, just pat yourself on the back for being too clever by half and leave it at that.
|
# ? Jul 21, 2009 05:59 |
|
I'm trying to create a makefile which has targets for both a static and a shared library. My rule for source files is just:code:
|
# ? Jul 21, 2009 12:57 |
|
I've been using C++ pretty intensely for the last year and for some reason I don't hate it. Clearly this is because I don't know enough about it! So what are the more advanced areas of C++ that I probably don't know? I know basic templates, but I look at some of the boost libraries and I'm amazed at the things people have managed to create from within the language. What do I need to learn to go from being ok in C++ to being really good?
|
# ? Jul 23, 2009 03:59 |
|
Contero posted:I've been using C++ pretty intensely for the last year and for some reason I don't hate it. Clearly this is because I don't know enough about it! Read Herb Sutter's Exceptional C++.
|
# ? Jul 23, 2009 12:13 |
|
Contero posted:I've been using C++ pretty intensely for the last year and for some reason I don't hate it. Clearly this is because I don't know enough about it! Read Effective C++ by Scott Meyers. http://www.amazon.com/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889
|
# ? Jul 23, 2009 14:49 |
|
Also read: The C++ Standard Library - Josuttis C++ Gotchas - Dewhurst C++ Common Knowledge - Dewhurst C++ Coding Standards - Sutter & Alexandrescu C++ FAQ - Cline C++ Templates the Complete Guide - Vandervoorde & Josuttis And online: Guru of the Week - http://www.gotw.ca/gotw/
|
# ? Jul 23, 2009 16:31 |
|
Just FYI, I work on a C++ compiler in my spare time, and I have read precisely one "book" on C++: the ISO standard. All of those other books are completely unnecessary in my opinion. I learned everything by writing code and looking at other people's code.
|
# ? Jul 23, 2009 17:28 |
|
This is pretty silly and goes back to my question earlier about using system() to call date. I was tasked with writing a program that received data from a server that then computes an MD5 hash on the data so that the hash on the data I got matched the hash on the data that the server sent. I then had to format the output on my end to match the server end, which needed to look something like this: code:
code:
code:
However, upon looking over my code my supervisor told me there are better ways to do what I did even though system() is the easiest, yet most dangerous. He then told me to find out how the system function works and why it screwed up my output and then tell him by the end of the day. My google skills are apparently lovely because I can't find a real explanation as to why it was screwing with my output. I can imagine this doesn't really make sense, but there you have it.
|
# ? Jul 23, 2009 17:57 |
|
Avenging Dentist posted:I learned everything by writing code and looking at other people's code.
|
# ? Jul 23, 2009 17:58 |
|
xwonderboyx posted:My google skills are apparently lovely because I can't find a real explanation as to why it was screwing with my output. The printf functions "print" to an internal buffer in your process's memory space, which is only sent to the output stream occasionally. system runs a new process, which obviously has its own buffers and consequently appears to your program to output "immediately". So even though you had a printf between your system calls, it wasn't really taking effect until after both of them.
|
# ? Jul 23, 2009 18:12 |
|
ShoulderDaemon posted:The printf functions "print" to an internal buffer in your process's memory space, which is only sent to the output stream occasionally. system runs a new process, which obviously has its own buffers and consequently appears to your program to output "immediately". So even though you had a printf between your system calls, it wasn't really taking effect until after both of them. Thank you! This thread is too awesome.
|
# ? Jul 23, 2009 18:16 |
|
If you are using a bunch of system calls, why are you even writing this in C? Wouldn't a shell script make more sense?
|
# ? Jul 23, 2009 18:17 |
|
Avenging Dentist posted:If you are using a bunch of system calls, why are you even writing this in C? Wouldn't a shell script make more sense? I'm in the military and the way they want you to learn C is to go to a week long class and then hand you a sheet of paper with C COMPETENCY written on it. You then learn by doing. I did it, and that's what matters.
|
# ? Jul 23, 2009 18:35 |
|
xwonderboyx posted:I'm in the military and the way they want you to learn C is to go to a week long class and then hand you a sheet of paper with C COMPETENCY written on it. You then learn by doing. I did it, and that's what matters. Please tell me you have nothing to do with missiles.
|
# ? Jul 23, 2009 18:45 |
|
Zombywuf posted:Please tell me you have nothing to do with missiles. I,uh, have nothing to do with missiles.
|
# ? Jul 23, 2009 18:50 |
|
If you're hell-bent on using system calls, at least use popen instead and then read from the FILE* returned from it.
|
# ? Jul 23, 2009 19:02 |
|
Avenging Dentist posted:Just FYI, I work on a C++ compiler in my spare time, and I have read precisely one "book" on C++: the ISO standard. All of those other books are completely unnecessary in my opinion. I learned everything by writing code and looking at other people's code. A code reading thread with suggestions could actaully be interesting and enlightening.
|
# ? Jul 23, 2009 19:27 |
|
Dijkstracula posted:A code reading thread with suggestions could actaully be interesting and enlightening.
|
# ? Jul 23, 2009 19:59 |
|
Dijkstracula posted:I agree. It's too easy to fall into the trap of reading a lot about programming but never actaully doing any. The other side of this, though, is you need to know a priori what code is worth reading and worth learning from. However, reading code is usually pretty dull. The best code is boring, the hard part is making it boring.
|
# ? Jul 23, 2009 20:03 |
|
It might be problematic for the reason that it's easy to show examples of bad code but hard to show examples of good code (especially concise examples that aren't entire projects). That said, CoC Code Review could be interesting.
|
# ? Jul 23, 2009 20:04 |
|
chips posted:It might be problematic for the reason that it's easy to show examples of bad code but hard to show examples of good code (especially concise examples that aren't entire projects). That said, CoC Code Review could be interesting.
|
# ? Jul 23, 2009 20:06 |
|
What's the point? If you (the reader) can't tell when code is good, you're not going to gain anything by someone showing you code that they believe is good, unless you're seriously arguing that you should write code using the monkey-see-monkey-do pattern.
|
# ? Jul 23, 2009 20:25 |
|
Avenging Dentist posted:What's the point? If you (the reader) can't tell when code is good, you're not going to gain anything by someone showing you code that they believe is good, unless you're seriously arguing that you should write code using the monkey-see-monkey-do pattern.
|
# ? Jul 23, 2009 20:35 |
|
Avenging Dentist posted:What's the point? If you (the reader) can't tell when code is good, you're not going to gain anything by someone showing you code that they believe is good, unless you're seriously arguing that you should write code using the monkey-see-monkey-do pattern. *flips through GoF* I don't see the monkey-see-monkey-do pattern?
|
# ? Jul 24, 2009 16:30 |
|
Contero posted:*flips through GoF* I don't see the monkey-see-monkey-do pattern? It's the whole book.
|
# ? Jul 24, 2009 17:22 |
|
I for one would love to hear about why code is good or bad. Example: http://www.urubatan.info/2008/11/commenting-source-code-is-only-for-the-weak/ The first example looks perfectly fine to me, and the refactored version is more confusing. Anyway, my problem: I'm parsing a midi file, and I have a MidiFile class that has a member variable that is a list of Track objects, and each Track object has a list of TrackEvent objects ("Events"). When I do this: code:
When I try to iterate through the list over time it doesn't work: code:
|
# ? Jul 24, 2009 23:24 |
|
Battle Bott posted:I for one would love to hear about why code is good or bad. Pretty sure that article is a troll. If you write a function that is only called once (and you never intend to use it other places), you are an idiot. Doubly so if that function is a one-liner. Triply so if the function name is almost as long as the code inside the function. Quadruply so if you misspelled the function name. Also, what are you even talking about in your question?
|
# ? Jul 24, 2009 23:32 |
|
Is it okay to use one-shot functions if you want to avoid several 100+ line sections within a switch-case block? If not then I'm clearly an idiot. That article rules though.
|
# ? Jul 24, 2009 23:37 |
|
|
# ? Jun 7, 2024 10:38 |
|
Hah, forgot to finish it. Reading a perl tutorial, sorry. When I try to call CurrentEvent() I get a runtime error "list iterator not dereferancable" This line would fail: code:
|
# ? Jul 24, 2009 23:44 |