-
Need help with fuctions
I'm trying to make a function that makes the number of endlines put into the integer, but it gives me these weird errors. What's wrong with my code?
Code:
#include <cstdlib>
#include <iostream>
using namespace std;
int nl(int i)
{
int t = 0;
while (i >= t)
{
cout << endl;
t++;
}
}
int main()
{
int i;
cout << "Rock?";
cin >> i;
nl();
cout << "Pluck?";
cin >> i;
return 0;
}
-
Well for starters, you aren't passing any value to nl().
Quzah.
-
Try passing a parameter to your function.
As in
-
Yea, I see it clearly now. Thanks guys
btw, is it best to make the function void if it doesn't return a value, or deosn't it matter?
-
Make the function return a void if there is nothing to return. If it returns a value then let it do so.
-
When deciding that, keep error checking in mind. The reason main() returns an int is so that the operating system can get a status message about the execution of the program. A lot of functions will return a code to help the user figure out how things went. For example, an input function might take the number of bytes the user wants read in as a parameter, and might return the number of bytes that actually DID get read in.
-
Yes, if it doesn't need to return anything, then don't return anything; make it void. No sense in returning a value which has no meaning whatsoever.
And be careful about adopting a policy in which functions return errors. In some cases, all possible return values could be validly returned. An example: you have a function that does some number crunching and returns an integer, but any integer it could return is valid (so, you can't say, "well, if it returns -1 (for example), then it failed"). There are ways to get around this, and get multiple output values from a function (not return multiple values, but there are other ways to have values output). Additionally, there are other mechanisms for error handling (exceptions are one example built right into the language; other user defined error reporting mechanisms can also be devised).
-
I need some more help.
I'm trying to make my own header file, but I'm having some trouble. I have placed the .cpp file and the .h file in different directories, and have linked to those directories in my project....but when I compile it, it gives the message: Undefined reference to `nl(int)'
Here are my 3 files.
simplestuff.ccp:
Code:
#include <cstdlib>
#include <iostream>
#include "simplestuff.h"
using namespace std;
void nl(int i)
{
int t = 1;
while (i >= t)
{
cout << t << endl;
t++;
}
}
simplestuff.h:
nl_test.cpp:
Code:
#include <cstdlib>
#include <iostream>
#include "simplestuff.h"
using namespace std;
int main()
{
cout << "Hey";
nl(5);
cout << "Der";
return 0;
}
-
Put both your .cpp files in the project.
Then do "build all"
-
Hmm, I added it to the project and pressed 'Rebuild all', but then it gives two error messages. Now it says: In file included from mine rescourser/simplestuff.cpp
Is it because I have placed the files in the wrong directory? Does it matter where simplestuff.cpp is placed? Which of the files should be linked in 'Resource directories', and which should be linked 'Included directories' ?
Oh, and it says: [Warning] no newline at end of file about the simplestuff.h file. Could that be the problem?
-
Well putting all three files in the same directory for now is the easy thing to do.
Separate directories later on are a good idea, especially for larger projects.
-
I just did that and I have the same problem. Which of the files should be linked in 'Resource directories', and which should be linked 'Included directories' ? Do I only need to link in of the catagories?
And why does it say [Warning] no newline at end of file about the simplestuff.h file? Have I constructed it wrong?
-
Which compiler are you using?
If all 3 files are in the same place, all you need do is make sure the two .cpp files are in the project (you did choose a console project didn't you).
> [Warning] no newline at end of file
Just edit the file, go to the very end, press return and save
-
Arg, this is killing me. I did as you said with the simplestuff.h file, and the error went away...but was replaced by a new one :(
C:\Documents and Settings\Kim\Dokumenter\Datamatiker\Dev-C++\nltest.cpp In function `void nl(int)':
C:\Documents and Settings\Kim\Dokumenter\Datamatiker\Dev-C++\nltest.cpp syntax error before `using'
I'm using Dev-C++ and I chose console project.