« first day (543 days earlier)      last day (4388 days later) » 

12:00 AM
If I couldn't spell its name at the time, it's not worth it.
 
user1174868
Hey gang I have a question about basic c++ stuff and functions
 
user1174868
I have absolutely no idea what I am doing really but if I am using a void function how does that really work? Do I need to declare variables in that are could an x input from the main() be used in my void function?
 
@Jordan void functions are generally used to group many small processes into one cohesive, larger one. You can declare and use variables inside a void function just as you would anywhere else, if you need to. You cannot access main's variables from another function, nor vice versa.
 
user1174868
I dont understand then, I have my void function set up and the user input in the main body so how do those two interact?
 
You can call your void function from inside main().
 
user1174868
12:05 AM
...
 
user1174868
what does call mean?
 
user1174868
I can declare it outside but call it inside correct?
 
I'll show you:
 
user1174868
Keep in mind I am really stupid and bad at programming
 
lol
I missed moving stuff.
 
12:06 AM
xD
#include <iostream>

// This is the definition.
void sayHello()
{
    std::cout << "Hello, world!" << std::endl;
}

int main()
{
    sayHello(); // This is the function call.
    return 0;
}
 
call = invoke
 
user1174868
ok but where do I define what the function does? for example after my return 0 I start a new { whatever and setup my function
 
void foo() { /* this is the definition */ }
 
@Jordan A function must be declared in a file before it is called. It may be defined anywhere.
In my example, I combined the declaration and definition.
 
It must be declared before it's used, 'a file' doesn't mean much
 
user1174868
12:09 AM
but how does that work? so C++ doesnt work left to right top to bottom?
 
This would have been equivalent:
 
user1174868
I mean I am doing all this stuff and then I am showing what the function actually does
 
It has to be declared left/above of where it's used
 
@Jordan mostly
 
you can declare it essentially anywhere
 
user1174868
12:10 AM
ok
 
user1174868
and it can include inputs from main?
 
What do you mean? It can recieve the inputs as its arguments.
 
@Jordan only those declared above it, or passed as arguments
 
#include <iostream>

// This is a declaration.
void sayHello();

int main()
{
    // sayHello() can be called here because it has already been declared.
    sayHello(); // This is the function call.
    return 0;
}

// This is the definition.
void sayHello()
{
    std::cout << "Hello, world!" << std::endl;
}
 
int square(int i); is a function of one argument.
 
user1174868
12:11 AM
I dont know what an argument is
 
user1174868
like in math?
 
@Jordan: Get a programming book or class, these are really basic questions
 
@Jordan Sort of. Mathematical functions are not the same as programming functions.
 
user1174868
here is my shitty code void hours(int h, int s, int m)
 
user1174868
h s and m are arguments?
 
12:12 AM
@Jordan yes
 
user1174868
I have a book and a class but they both suck
 
user1174868
also like I said I am stupid
 
Technically, they are parameters. Arguments are the actual values as they are seen from inside the function.
 
Aren't int h, etc parameters? Arguments are the values you give them.
Ninja :(
 
12:13 AM
But the terms can mostly be used interchangeably.
@Pubby ;)
 
@Maxpm The terminology depends. Better say /actual/ arguments/parameters versus /formal/ arguments/parameters.
Or operands.
For that matter.
 
user1174868
how do I do modulus on a calculator?
 
@Maxpm Sometimes they are!
 
With Windows calc, just 8 Mod 3 =
Easy!
 
user1174868
nope
 
user1174868
12:16 AM
I am trying to find a way to conver seconds into minutes and hours but I am stupid
 
@RMartinhoFernandes I considered mentioning Haskell, but decided against it.
 
That's called "sexagecimal". Casio calculators have that.
 
@Jordan what kind of calculator
 
user1174868
ti 36x
 
user1174868
pro
 
12:19 AM
@Jordan I don't think it can be done without integer truncation or modulus.
You can do integer truncation easy in your head if that's an option
 
user1174868
Does this look right? void hours(int h, int s, int m)
{
h = time/3600
m = time/60;
s = time%3600;
 
user1174868
no that isnt right
 
@Jordan nope
 
user1174868
12:20 AM
hours and correct
 
^ it's often a good idea to just google it
 
@Jordan What's time here?
 
@Jordan m = time%3600 / 60; and s = time % 60
 
user1174868
input from the main body as seconds
 
You should try to write hours standalone, as if there were was nothing else, including no main.
 
12:21 AM
The boxes, they should be black.
 
Also what's the purpose of hours? What does it operate on? What does it compute? And by that I don't mean in code, I mean in concept: what is it you want to achieve, regardless of C++.
 
@Jordan That function doesn't seem to have access to a time variable, and your three parameters are input parameters. Editing them doesn't make much sense.
 
user1174868
hours is the function that takes an input time (in seconds) and outputs hours minutes and seconds
 
Linux Mint is already pissing me off.
 
@Jordan it looks like you told it to take hours, minutes and seconds.
 
12:23 AM
@Jordan Alright! So that suggest that the parameter to that function should be something like int time, and the return type something like int. Putting it together: int hours(int time);.
 
user1174868
what do you mean?
 
user1174868
I thought I had to declare the return types in my parameters
 
@Jordan the return type is not a parameter. The return type goes to the left of the parameter name
 
Parameters are the inputs, return type is the output
 
user1174868
void hours(int time)
{
int h, s, m;

h = time/3600;
s = time % 60;
m = time%3600/60;

}
 
12:24 AM
return_type name(parameter_type0 param0, parameter_type1 param1, /* ... and so on */); is how a function declaration is read.
 
user1174868
what is a declaration?
 
user1174868
is that where the function is defined or just where it is called
 
A way of specifying something exists. int hours(int time); specifies that somewhere in the program there is a hours function that takes an int and returns an int.
Once something is declared, then you can use it. E.g. for a function you can call it.
 
user1174868
void hours(int time)
{
int h, s, m;

h = time/3600;
s = time % 60;
m = time%3600/60;

}
 
user1174868
is that a declaration or definition?
 
12:26 AM
it's a declaration that is also a definition
 
@Jordan both
 
it's not a pure declaration
 
user406009
(A book would probably be more thorough and consistent)
 
user1174868
ok so void hours(int time) is a declaration?
 
user1174868
and the rest is a definition
 
user1174868
12:26 AM
I read the book and it didnt make any sense
 
the whole shebang is a definition
 
I'm not sure, I think the semicolon is important. But maybe not.
 
which book are you using
 
@Jordan if you put void hours(int time); at the top of your code, then all the functions could see it and call it, even if it was the last function defined, at the bottom of the code.
 
user1174868
Introduction to C++ Programming by DS Malik
 
user1174868
12:27 AM
ok that is what I did
 
In any case a definition is always a declaration, so we don't need to worry about the semicolon tbh. As Alf said the whole is a definition and that should be enough.
 
user1174868
how do I post code?
 
user1174868
should I delete that?
 
@Jordan not like that
@Jordan yes please
 
12:28 AM
It would be nice if you delete it yes
And post the code at e.g. pastebin.com or ideone.com
 
user1174868
 
My TeX file is now 100kB long.
 
> Do not get this book, and if it is required for the course get another book to learn C++ from. Any book, have a 10 year old write one for you and it would be better than this.

Thankfully I had experience with C++ before I took the class but I ended up teaching the ENTIRE CLASS because our teacher thought this was the best C++ book he's read and he didn't even know the material.

D.S. Malik is a stupid stupidhead.
 
36 pages of output.
 
@CatPlusPlus hire Donald Knuth
 
12:29 AM
Still one chapter left.
 
@CatPlusPlus WTF are you writing?
 
Database project!
 
@Jordan short code may be posted here, with the "fixed font" button next to the "send" button to the right. More than 8 lines or so should be at ideone.com
 
@Pubby Huh? It's in Google books?
 
@CatPlusPlus A database project in TeX?
 
12:30 AM
@CheersandhthAlf ?? That was from some Amazon review
 
Shouldn't you be using some sort of SQL thingy?
 
user1174868
@Pubby is that the Malik book? Sounds like that guy might be in my class
 
Yeah. Documentationey.
 
He should change his name to malloc
 
Conceptual design or whatever.
 
user1174868
12:32 AM
Isn't Darth Malik a think?
 
@Luc hey, didn't you break your overload for member objects?
Or that was never intended to work?
 
My unit tests pass.
 
user1174868
Ok so with my code if I have a function that has 3 variables that I want to ouput in the main body I need to declare those functions in main as well? OR would I have the function output?
 
@Jordan Can you clarify what you mean by 'a function that has 3 variables'? Is that a function with 3 parameters?
 
user1174868
1 parameter but 3 variables for the output
 
user1174868
12:34 AM
return values
 
user1174868
void hours(int time)
{
int h, s, m;

h = time/3600;
s = time % 60;
m = time%3600/60;

}
 
Ah. C++ has either no return values (in the case of void) or only one return values, but not multiple values.
 
user1174868
I think those calculations are wrong too arent that
 
@LucDanton Your unit tests don't cover member objects though.
 
@Jordan That code doesn't return anything. The type of your function (void) means that your function doesn't return things
 
12:35 AM
Hence my second question.
 
user1174868
Can I have that function do the output then?
 
certainly could
Although it probably makes more sense to split hours minutes and seconds into separate functions
 
@RMartinhoFernandes What about here?
 
user1174868
but then I would have 3 global constants and that is bad?
 
you would have 3 functions, but functions are not constants
 
12:36 AM
Or if you want to do output, then you don't global constants to do output either.
 
so you'd have:
int hours(int time);
int minutes(int time);
int seconds(int time);

And then define them later on
 
user1174868
void hours(int h);
void minutes(int m);
void seconds(int s)
 
user1174868
like that|?
 
@LucDanton Oh, oops, I guess I wasn't clear. I mean member object pointers, i.e., things like int foo::*.
 
@Jordan Define a struct with 3 member variables called hours, minutes and seconds. Use that type as the function result type.
 
user1174868
12:37 AM
I dont know struct just void
 
the thing before the function name is the return type, the type of variable you want to get out
 
@RMartinhoFernandes No support. I have mem_fn.
 
@Jordan: also, check out the C++ book list FAQ here on Stack Overflow.
1032
Q: The Definitive C++ Book Guide and List

grepsedawkThis question attempts to collect the few pearls among the dozens of bad C++ books that are released every year. Unlike many other programming languages, which are often picked up on the go from tutorials found on the Internet, few are able to quickly pick up C++ without studying a good C++ book...

 
so void hours(int h); declares a function which takes one input, an int called h, and returns nothing
 
user1174868
void hours(int time);
void minutes(int time);
void seconds(int time);
 
12:38 AM
close, but your return types are still wrong
since you've declared those functions void, they can't return values
 
user1174868
return types?
 
user1174868
void means that it can just output?
 
all functions have a return type, the type of thing they send back to whoever is using the function
@Jordan No, void means "this is a function, which doesn't return anything to the caller"
 
user1174868
do I want to use void?
 
I don't think you have to for this function or functions.
 
12:40 AM
if you were to type int hours(int time) you would be saying "This is a function called hours. It takes one int, time and returns to you another int"
 
user1174868
My books says "VOid functions can use the return statement without any values"
 
that is true, when you want to exit a void function, you can just say return; without passing anything back
but in this case, you want to return the number of hours, so you'd better say something like return h;
 
So now you know how you can implement the mathematical function NN: x ↦ x mod 3600 as int hours(int time) { return time % 3600; } in C++. (No idea if you're okay with this sort of maths though.)
 
I think that the same as in programming, when using math notation that reader possibly is unfamiliar with, then use parentheses to make the parsing clear
 
12:46 AM
@RMartinhoFernandes Sure.
 
(N → N): (x ↦ (x mod 3600) )
 
This font has a weird glyph for this thing.
 
hello everyone
 
I remember the extra dash being vertical, to the left of the actual letter N.
This one has a double diagonal dash.
 
@CheersandhthAlf I'm not exactly sold on the colon thingy. Normally it's f: foo ↦ bar with the domain/codomain stuff appearing on the line above.
 
12:48 AM
someone know how to split this word 'num,' like 'num' and ',' in java ?
 
@LucDanton for that matter, "mod" is usually an operator on equations, not an operator on numbers...
 
like, "x = y mod m" means "x = y+K*m" for some integer K
 
user1174868
@Luc the { return time % 3600; } is the statement that I use in the definition part of the function correct?
 
@CheersandhthAlf I think I only ever used the x ≡ y [m] notation although that amounts to the same. I wouldn't say it's necessary about equations though.
 
12:51 AM
@LucDanton Woah...
 
@Jordan No, the whole is the definition (i.e. starting from the return type). The bit between braces is called the body of the definition.
 
Well, Knuth used "mod" as an operator on numbers, so it's not without precedent
For that matter, so did Pascal, and Basic
:)
 
user1174868
I dont understand all these words
 
user1174868
let me read my book
 
int hours(int time) { return time % 3600; } is a function definition.
 
12:52 AM
nevermind solver =]
 
Oh. Pascal could be Blaise Pascal, the religious mathematician, or the programming language Pascal. T'was just a little wordplay.
 
user1174868
ok I got that
 
In it, { return time % 3600; } is the body of the definition.
In it, return time % 3600; is a return statement.
 
user1174868
the function call is where I use the function in the main right?
 
Yes.
Have you got that figured out btw?
 
user1174868
12:53 AM
what is the part at the top of the main where I initialize the function?
 
C++ - Blaisingly faster than Pascal!
 
user1174868
int hours(int time)
{ return time % 3600; }
 
user1174868
oops
 
user1174868
that is wrong, but basically that will return a value of time?
 
My computer is acting up, brb in 5 min or so.
 
user1174868
12:57 AM
Why do I want to return something? Why not just have the function cout something
 
Build systems are pants.
Package managers are pants, too.
 
@Jordan for hours, just divide by the number of seconds per hour
 
*Flips table*
 
user1174868
?
 
user1174868
wouldnt that require another variable
 
12:58 AM
no
 
@Maxpm First time I see that use of the word "pants".
 
user1174868
there are 60 seconds per hour?
 
consider when you have 7201 seconds
 
user1174868
okay so my hours is for seconds isnt it
 
ok basics
a minute = 60 seconds
 
12:59 AM
@Jordan In this case, you could do either. But sometimes it doesn't make sense for a function to output what it calculates.
 
an hour = 60 minutes
 
user1174868
This is what I have
 
a day = 24 hours
ok?
so if you have 123 seconds, how many minutes and seconds is that?
 
user1174868
123\2 = 2 minutes
 
yep
so it's 2 minutes and 3 seconds.
now you can do the same with 123 minutes -- how many hours and minutes is that?
 
user1174868
1:03 AM
123 % 60 = 3
 
user1174868
for seconds
 
it very like how you found number of minutes
it's the same question really
just different time units
 
user1174868
123/60 = 2 hours, 3 minutes
 
yep
so that's one way to do the calculation
starting with seconds ...
calculate number of minutes, and ...
using number of minutes, calculate number of hours
 
1:08 AM
minutes → hours × minutes, m ↦ (m / 60, m % 60) if I may.
I think that's not totally gratuitous, it might give an insight into how types and values relate to one another.
 
user1174868
so if I have 123 seconds I divide by 60 to get minutes, then se subtract that to get seconds?
 
user1174868
err seconds- 60*minute
 
@LucDanton Some time right after I started as a teacher at a vocational school (it was about communication electronics), I wrote a few pages explaining general number systems to the pupils. It was a success only in the sense that the headmastress (word?) asked me if I had written that? Lots of sigma's and uppercase pi's...
@Jordan yes!
 
user1174868
is this wrong in any way? int hours(int time)
{ return time/3600; }

int minutes(int m)
{ return time%3600/60; }

int seconds(int s)
{ return time % 60; }
 
The minutes function looks a bit suspicious
You can just test it
 
1:13 AM
I also recommend testing. I don't think my head is working right at this hour.
 
But you're right on that it involves both a division and a mod operation
 
user1174868
so now that I am not using void do I want seperate variables?
 
Where? In main?
 
i recommend doing one thing at a time
 
user1174868
yes
 
1:15 AM
first get functions correct, working
then think about struct
 
user1174868
how do I test them?
 
user1174868
I dont know struct
 
For the time being displaying the result outright might be enough.
 
user1174868
so I have 3 functions all with return value for time
 
user1174868
wont it just use the late one for time?
 
If you have multiple definitions with the same function name you might be into trouble. Code?
 
^ Breaking out of the box. :)
 
user1174868
that pciture is way cooler
 
user1174868
 
Hm, some trouble with declaring and initializing variables
like this:
int const h = hours( time );
 
1:20 AM
Make the declarations at the top match the definitions at the bottom. (Or move the definitions where the declarations were. Your call.)
 
Note: cannot use same name for variable and function.
 
Aaaand after hacking through last chapter, the final result is 112.1kB of LaTeX code (2108 lines), resulting in a 173kB 46-page PDF.
 
user1174868
cant use same name for variable functions?
 
Just shoot me.
 
user1174868
int hours(int time)
{ return seconds/3600; }
 
user1174868
1:22 AM
is that correct?
 
It's a valid definition in any case.
Oh wait no, what's seconds here?
 
user1174868
defined in main as int, user input for seconds
 
1 hour ago, by Luc Danton
You should try to write hours standalone, as if there were was nothing else, including no main.
 
@Jordan No. main() is a function just like hours(), and functions do not see each other's variables.
The visibility of variables (and other things) is called scope.
 
user1174868
ok so the function will not change the value of that input ever?
 
1:26 AM
What function, and what input?
 
user1174868
ok so how do I print the output from the function?
 
user1174868
 
user1174868
I feel like a peice of shit asking all these questions but this book sucks, I should probably just watch online tutorials
 
I recommend you go back to Alf's advice right now.
7 mins ago, by Cheers and hth. - Alf
int const h = hours( time );
Can you make sense of that?
 
@Jordan What do you expect the function calls inside main() to do?
 
user1174868
1:30 AM
what I have at the bottom
 
@Jordan Right. So those functions are taking numbers that the user input and doing some calculations. The problem is that you're not doing anything with the result of those calculations.
 
user1174868
do I do the cout in the function statement?
 
Yes.
No.
Well, you can cout inside your time functions, but it's probably better to keep them as-is.
You need to store the values that the functions return in variables. For example:
int calculatedHours = hours(seconds);
 
user1174868
what is return for?
 
Then you cout << calculatedHours.
@Jordan Return is a way of saying that the function has finished with whatever it's doing, and is ready to "give back" the value to whoever called it.
 
user1174868
1:33 AM
so it would be h = seconds/3600 return h
 
@Jordan What would be?
 
Windows suddenly killed all processes, informed me that Visual Studio possibly had not run (?) correctly, started an upcount of "configuring Windows", rebooted, had me log in again, then warned me about the update, maybe it could be a security risk, and as it turned out, had disabled my "wireless capability" (no network). hurray.
 
user1174868
The function should be:
 
Which function should be?
 
user1174868
int hours(int time)
{ h = seconds/3600 return h; }
 
1:37 AM
It re-opened the calculator window, the yellow notes and visual studio.
 
@Jordan No.
 
user1174868
hold on
 
Okay.
 
user1174868
I edited, no idea how to make spaces
 
@Jordan: it's sheet. or skeet. jon skeet
 
1:39 AM
@Jordan You could make the body { int h = seconds / 3600; return h; } but that's no improvement over { return seconds / 3600; }. But really, make it { return time / 3600; }.
 
@Jordan should work fine if you add the missing semicolon, and add int const in front of the declaration.
 
user1174868
what does int const do?
 
user1174868
I know it makes a global variable?
 
It doesn't necessarily.
 
@Jordan the int says that it's an integer. essentially how much memory it uses, and what you're allowed to do with it.
the const is not strictly necessary but nice
it says that you don't intend to change the value of that variable
 
user1174868
1:42 AM
ohh
 
user1174868
so the functions can only change it in their function?
 
It means, it can't be changed after the declaration. Unless dirty tricks. :-)
 
Often, you store a value that you should not have to change later. const prevents you from accidentally doing just that.
 
A typical Windows compiler is 32-bit, and then int uses 32 bits = 4 bytes of memory
Even in 64-bits Windows you have 32-bit int.
 
What's typical Windows compiler?
 
1:45 AM
Visual C++ is the most typical. Then you have g++. And Intel.
 
user1174868
do you want to use the same compiler as other people working on the same project?
 
There is also a 64-bit version of Visual C++.
 
@Jordan Not necessarily.
 
@Jordan In practice you need to.
 
Testing code with more than one compiler is always good.
 
1:47 AM
Generally, teams only share source code, not binary object files. So compiler specifics should not matter.
Should it be sudo make && make install, or sudo make && sudo make install?
 
Large, in-house, self-contained projects might have environment that's tweaked for a particular compiler (including precompiled dependencies), but that's not a rule.
 
@Maxpm In these times the advice about testing with more than one compiler, is really important. But it has always been so for C++.
 
@Maxpm make && sudo make install.
Don't build with root.
 
user1174868
Ok I messed things up
 
user1174868
int hours(int time)
{int h seconds/3600
return h; }

int minutes(int time)
{ int m = seconds%3600/60
return m; }

int second(int time)
{ int s = seconds%60
return s; }
 
1:48 AM
@CatPlusPlus Right.
Thanks.
 
That's missing several semicolons.
 
int hours(int time) { return time/3600; } et cetera.
 
user1174868
Is this wrong? It feels wrong
 
user1174868
int hours(int time);
cout << "Hours: "<< h << endl ;
 
Meh, two hours to rollover in KoL.
 
user1174868
1:50 AM
I aplogize, I shouldnt be trouble shooting code here
 
Read a good intro level book.
It'll help.
Robot will provide a link in 14 seconds.
 
Yeah, you've been going in circles for quite some time already. Some of the advice has been going over your head.
 
user1174868
I will buy the book from the FAQ and read it a bit during the summer
 
Personally, I don't see the value of printed programming books.
 
Well, they have the 'everything in one place, instead of scattered around the net' thing.
That's good when you're learning the basics.
 
1:53 AM
Online "books" are lovely.
 
Especially in a dark, hairy place like C++.
 
Learn You a Haskell is pretty awesome.
 
Haskell is much simpler than C++.
 
@Maxpm: Yeah, but Haskell is pretty; C++ isn't.
 
Most languages are much simpler than C++.
 
1:54 AM
This is true.
But for basic imperative programming, an online tutorial shouldn't be bad at all.
 
user1174868
weird, my code has no errors but failed to run
 
@Maxpm: Shouldn't be, but... :) C++ is just too complex to be taught by anyone but people who already know it quite well, and those people want to write books, not online tutorials.
 
@GManNickG Good point.
@Jordan Failed to run how?
 
user1174868
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
It has errors.
 
1:57 AM
I don't think that counts as 'my code has no errors'.
 
user1174868
well there is nothing else in the box lol
 
Huh.
 
user1174868
How do I know where the errors are?
 
@CatPlusPlus Mwhahaha, your prediction failed!
I'm so evil.
 
@Jordan Depends on your IDE.
Looks like you're using Visual Studio.
 
user1174868
1:59 AM
yes
 
Look for red squiggly lines in the editor window.
 
user1174868
there are none
 

« first day (543 days earlier)      last day (4388 days later) »