Could anybody please help me with my Intro to C++ programing homework? - by lizardfuel55
Al_B on 19/9/2010 at 06:58
Quote Posted by Brian The Dog
is there any reason to use "\n" at the end of a line rather than std::endl?
(
www.google.com/search?q="\n"+vs+std::endl) Using '\n' doesn't flush the output buffer.
Brian The Dog on 19/9/2010 at 07:16
Ah, of course. Thanks!
Mortal Monkey on 19/9/2010 at 13:34
Quote Posted by Brian The Dog
I never bother using namespace std - I always like to know which are my functions/classes (in their own namespace) and which are the ANSI/ISO standard ones.
That's exactly what namespaces are for. "using namespace" globally is just lazy programming.
Quote Posted by lizardfuel55
I have to make a basic prompt that asks the user to enter numbers.
Code:
#define S(x) {using namespace std; x}
#define G(x) cin>>x;
#define P(x) cout<<x<<endl;
#define F(x,y) (y==2?(x)+5:y==1?(x)*2:(x)*(x));
#include <iostream>
template <class A>
struct I {A b,c,d;};
int main()
{
I<int> a;
for (int p=sizeof(I<int>)/sizeof(int)-1; p>=0; --p)
{
S(P("Please enter another number."))
S(G(p[&a.b]))
p[&a.b]=F(p[&a.b],p)
}
S(P("Results: "<<a.d<<" "<<a.c<<" "<<a.b))
return false;
}
You can thank me later.
Queue on 19/9/2010 at 13:44
Quote Posted by Xorak
In the time it took the OP to even wait for the first reply he could have easily written that program: three cins, three ints, the math formula and a cout. Good luck with the harder stuff, I really mean it. Programming is tough if you aren't interested at all in it.
That's what gets me-- this is introductory stuff, if you don't get the "basics" down, on your own, then how do you expect to do well from this point on?
lizardfuel55, in all seriousness, why are you taking a class in C++ when, as you said,
Quote:
...programming is my weakness
and doing even the basic stuff is causing you headaches? Please tell me you're not going into any sort of computer field--because if you are, maybe you should reconsider your career choice.
It's like blatant idiots becoming teachers because, well, teachers have good benefits and you get your summers off. They're still idiots, in the end, and are now helping to mold young minds in their image.
Brian The Dog on 19/9/2010 at 14:36
To be fair, often you are forced to take subjects as part of a degree or similar course that you're not interested in. I had to learn C++ as part of my Physics degree, even though I wasn't too interested in the subject. Of course, it was probably the most useful thing I did at undergrad level :)
That's some pretty impressive code, MM :cool: I've never seen #define with a function before!
Queue on 19/9/2010 at 15:17
Quote Posted by Brian The Dog
To be fair, often you are forced to take subjects as part of a degree or similar course that you're not interested in. I had to learn C++ as part of my Physics degree...
Ah, true. I am thinking of college twenty-some years ago when classes in things like C were not part of the curriculum. So that's probably it.
You know....
From a personal experience, I had a neighbor who one day decided he wanted to become a computer programmer (he was tired of working the Hi-Lo all the time), and relentlessly called me up to help with his C++ homework because he didn't know the first thing about computers (or programming) and was, in all honesty, incapable of learning it himself. Yet, he was relentless in his pursuit of a degree in computer programming.
Last I heard, he dropped out after three wasted years.
It's not a bad thing to better yourself, just know your limitations and work with them--and if you have to rely on someone else's help all the time, then need to change your goal. Truly, you don't have to be a SUCCESS to be a "success".
Mortal Monkey on 19/9/2010 at 17:38
Quote Posted by Brian The Dog
That's some pretty impressive code, MM :cool: I've never seen #define with a function before!
Thanks, though I feel the template has a lot of unused potential. The "functions" are macros, i.e. replaced at compile time like regular #defines, but the parameters are interpolated. "G(q)" would actually be compiled as though it was "cin>>q;".
demagogue on 19/9/2010 at 17:40
Well while we're reflecting ... I taught myself C++ years ago just because I had already taken a lot of predicate logic for my Philosophy degree and was very good at it, so I thought I may as well learn the practical side of it.
Then I've never had much chance to use it (though it's interesting how similar programming code is to legal code sometimes). But I like to program little things every now and then because I like that kind of thinking. My last project was for Dark Mod. I didn't like how there wasn't any zone-based ambient sounds, you had to literally line up speakers in the map. I got so frustrated I thought, screw it, I'll make a system myself (Doom3 scripts use C++), and I got out a sheet of paper and wrote out the logic for it, and about 2 months and 65 script drafts later (lol), it worked! I did it just for myself, but it worked so well that the whole mod uses it now.
I kind of stunned myself. But that's what I really like about programming. It doesn't care who you are or how you do it. If you get the logic right, it works. And I really loved that feeling of total freedom. I didn't have to go whining to the mod people "How do I do X!? This sucks! Why can't I do X!" I just got out a piece of paper and did it myself. That's an incredibly empowering feeling.
After that experience, now I don't have much sympathy for people whining about X like it's an entitlement and expecting other people to do the hard work (I'm thinking about people asking for features on the mod, but also generally I guess), when they have the power to do it themselves. I'd rather put a positive spin on it though. I think they'd feel empowered too if they understood what they're capable of, and everybody is better off, so I try to encourage more than criticize. This really isn't that hard; you just need to spend some time thinking it through, but the payoff is awesome. It's worth it.
I do have to acknowledge one thing though. It's given me a great appreciation of what it means for a project to be open source... None of that freedom or power means anything unless you have the ability to use it.
zombe on 19/9/2010 at 18:11
Quote Posted by Mortal Monkey
Code:
#define S(x) {using namespace std; x}
#define G(x) cin>>x;
#define P(x) cout<<x<<endl;
#define F(x,y) (y==2?(x)+5:y==1?(x)*2:(x)*(x)); /.../
You can thank me later.
Macros are evil - use them only when necessary. This usage, IMHO, is just horrible ... every one of it. F is especially bad.
Sulphur on 19/9/2010 at 18:31
Pfft. MM's code is structured, concise, economical and brilliant. It'd get lizardfuel a straight A.