Need help with fuctions

This is a discussion on Need help with fuctions within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make a function that makes the number of endlines put into the integer, but it gives me ...

  1. #1
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80

    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;
        
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, you aren't passing any value to nl().

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Try passing a parameter to your function.

    As in
    Code:
    nl(i);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    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?
    Last edited by stillwell; 01-23-2005 at 06:41 AM.

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Make the function return a void if there is nothing to return. If it returns a value then let it do so.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    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:

    Code:
    void nl(int i);
    nl_test.cpp:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include "simplestuff.h"
    
    using namespace std;
    
    int main()
    {
        
    cout << "Hey";
    nl(5);
    cout << "Der";    
        
        return 0;
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Put both your .cpp files in the project.
    Then do "build all"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    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?
    Last edited by stillwell; 01-25-2005 at 09:37 AM.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    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?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vectors and fuctions
    By mixalissen in forum C++ Programming
    Replies: 13
    Last Post: 05-05-2008, 12:48 PM
  2. Problem with cos, sin fuctions
    By xxxhunter in forum C Programming
    Replies: 7
    Last Post: 11-16-2007, 03:33 AM
  3. Quad equation using fuctions problem
    By godfrey270487 in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2006, 11:23 AM
  4. 2 fuctions please
    By dac in forum C++ Programming
    Replies: 3
    Last Post: 02-13-2006, 05:55 AM
  5. Replies: 8
    Last Post: 04-12-2004, 12:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21