« first day (1703 days earlier)      last day (3256 days later) » 

11:12 PM
@ThomasW. Is that covered under acceptable use? I'ld want to purge that from the interwebs if I could.
 
11:22 PM
@NathanOsman You around?
 
@RPiAwesomeness Yup.
 
Can you give me some C++ help?
 
I sure can.
 
Do you see anything wrong with this code?
#include <iostream>

int find_area(int length, int width);	// function prototype

int main()
{
	int length;
	int width;
	int area;

	std::cout << "\nHow wide is your yard? ";
	std::cin >> width;
	std::cout << "\nHow long is your yard? ";
	std::cin >> length;

	area = findArea(length, width);

	std::cout << "\nYour yard is ";
	std::cout << area;
	std::cout << " square feet\n\n";

	return 0;
}

// function definition
int find_area(int 1, int w)
{
	return 1 * w;
}
It's one of the examples I'm supposed to compile/run from the book I'm using
 
You can't have a variable named "1" :)
 
11:24 PM
>.<
 
I assume that was supposed to be an "l".
 
Mono-spaced font is so often hard to see the difference between those
 
lol
@RPiAwesomeness Inconsolatas font
works wonders
 
So first of all, other than the "typo", it should do what you expect.
But there are a few things that could be changed a little bit.
 
11:25 PM
area.cpp: In function ‘int main()’:
area.cpp:16:31: error: ‘findArea’ was not declared in this scope
  area = findArea(length, width);
New issue
 
@ByteCommander They flagged it too.
 
Instead of "\n" literals, it's easier to do:
std::cout << "Line 1" << std::endl;
std::endl is the newline.
@RPiAwesomeness Huh...
That's odd.
 
@NathanOsman Yes. I actually asked which is better.
 
Sorry @ThomasW. I'm super busy today and on mobile.
 
@RPiAwesomeness Silly me.
 
11:28 PM
\n is supposed to be slightly faster, while std::endlis only really needed when dealing with data you have to have perfect. That's what I heard.
 
You've got findArea and find_area.
 
@RPiAwesomeness Fantastique Sans Mono ftw!
 
Two different symbols.
 
@Seth Yeah, I've got that set up for my IDE - the book has the crappy normal monospaced :)
 
@RPiAwesomeness Like I said, it's not really a big deal, and not really a huge difference. Just something a bit more technically correct. Don't worry about that.
 
11:29 PM
@NathanOsman Ah. Derp.
 
Did they tell you about namespaces yet?
 
So, std::endl is the proper method?
 
Both work. It's a really minor thing. Really.
 
@NathanOsman Using std:: instead of using namespace std;?
Okay.
 
@RPiAwesomeness Well, and you can also do:
using std::cout;
using std::cin;
IIRC.
 
11:30 PM
Ah.
I heard that it's bad practice to use the using statement because of the possibility of library conflicts.
\o/ It works! :)
 
std::etc is more readable IMO for large projects.
 
@Seth Yeah, it is. Harder to type though >.<
 
Maybe I'll show you how I would write/format it.
 
@NathanOsman What naming scheme do you use for your classes/functions?
lowercase, separated by underscores or camelCase?
The book I have said it doesn't matter, but Unix developers tend to use lowercase + underscore
 
C uses underscores. C++ typically uses camelCase.
That's what you'll find in 90% of the code out there.
 
11:33 PM
Ah. I see.
Then I will stick to that naming convention.
 
Here's how I would have done it (keeping the "\n"s in there):
#include <iostream>

int findArea(int length, int width)
{
	return length * width;
}

int main()
{
    int length, width;

    std::cout << "How long is your yard? ";
    std::cin >> length;
    std::cout << "How wide is your yard? ";
    std::cin >> width;
    std::cout << "\nYour yard is "
              << findArea(length, width)
              << " square feet.\n\n";

    return 0;
}
If you define the findArea() function before main(), you don't need to declare it.
 
Was just gonna ask you about that. You don't use function prototype/declarations?
 
I usually use classes, so the answer to that is yes and no :P
 
You won't learn about templates for a while, but let me show you a really neat trick:
template <typename T>
T findArea(T length, T width)
{
	return length * width;
}
Use that in place of the original definition for findArea() and it will appear to work exactly the same.
Now change "int length, width;" to "float length, width;".
Presto! Now you can calculate with floating-point numbers instead of integers!
And you didn't even need to change the function. At all!
 
11:38 PM
Cool! :)
 
Templates let you tell the compiler what a function does without telling it what types to use.
Then when you actually use the function, it "generates" a copy of the function for those types.
 
So, it won't actually use space for the function until it's called?
 
Nope. By space, I assume you mean it won't generate object code for the function.
 
No, I was thinking setting aside space in RAM - but that doesn't make sense
 
Have you learned about the stack vs. the heap yet?
 
11:42 PM
Err, no. But I sorta have an idea of it?
 
Don't feel bad if you haven't. I just want to make sure you learn that part right :P
 
You can Push, pull, and Pop stuff from the stack, right?
 
Oh, no. Not that kind of stack.
 
Or is that just ASM?
 
The "stack" in this case is where the variables that you use are stored in memory.
In the main() function, they are stored on the stack.
 
11:44 PM
Hi all
 
@Kris hi.
 
this is nice.. I never knew there was chat outside of freenode
 
Yeah :) It's really well done.
 
has it been active very long?
 
@Kris Since 2010.
 
11:46 PM
oh wow. man
 
@Seth no problem
 
<< rip van winkle
 
@NathanOsman Is there any reason that I should use templates over just declaring the functions?
 
Oh no.
I was just showing you how awesome they are :P
They are very complex, so don't worry about them for a while.
C++ is one of those languages with lots of esoteric features. Focus on the fundamentals for now.
 

« first day (1703 days earlier)      last day (3256 days later) »