Overloading Functions. I keep getting junk.

This is a discussion on Overloading Functions. I keep getting junk. within the C++ Programming forums, part of the General Programming Boards category; I have a very simple function using overloading function. It's just supposed to send an array to a function, and ...

  1. #1
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21

    Overloading Functions. I keep getting junk.

    I have a very simple function using overloading function. It's just supposed to send an array to a function, and the function prints out that array "size" times. It's printing it out the correct number of times, but it keeps printing junk. WHat's wrong?

    This is what I have so far.


    Code:
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    void print(const int data[], int size = 10)                        //int version
    {
         for(int i = 0; i < size; i++)
                 cout << "Triple int version: " << data << endl;
    }
     
    void print(const float data[], int size = 10)                          //float version
    {
         for(int i = 0; i < size; i++)                                                //loop 'size' times
                 cout << "Triple float version: " << data << endl;     //prints the triples-array
    }
    
    void print(const char data[], int size = 10)                           //char version
    {
         for(int i = 0; i < size; i++)                                                 //loop 'size' times
                 cout << "Triple char version: " << data << endl;      //prints the triples-array
    }
    
    int main()
    {
        void print(const int data[], int size = 10);                    //prototype for int
        void print(const float data[], int size = 10);                  //prototype for float
        void print(const char data[], int size = 10);                   //prototype for char
        
        //generic arrays specific to their respective types
             int array_int[5] = {1, 2, 3, 4, 5};
             float array_float[5] = {1.123, 2.234, 3.345, 4.456, 5.567};
             char array_char[5] = {6, 7, 8, 9, 10};
             
             print(array_int);                                                   //Send with int-specific function
             print(array_float, 5);                                              //Send to float-specific function
             print(array_char, 1);                                                  //Send to char-specific function
             
             system("pause");
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Let's try this...
    First, default arguments aren't specified in the definitions and only the declarations.
    Second, declarations should be in a header file or at the start of your source file, not inside some functions.
    Thirdly, size is a very important argument, so don't make it a default argument and you'll soon find out why you're getting junk.

    Code:
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    void print(const int data[], int size);                    //prototype for int
    void print(const float data[], int size);                  //prototype for float
    void print(const char data[], int size);                   //prototype for char
    
    void print(const int data[], int size)                        //int version
    {
         for(int i = 0; i < size; i++)
                 cout << "Triple int version: " << data << endl;
    }
     
    void print(const float data[], int size)                          //float version
    {
         for(int i = 0; i < size; i++)                                                //loop 'size' times
                 cout << "Triple float version: " << data << endl;     //prints the triples-array
    }
    
    void print(const char data[], int size)                           //char version
    {
         for(int i = 0; i < size; i++)                                                 //loop 'size' times
                 cout << "Triple char version: " << data << endl;      //prints the triples-array
    }
    
    int main()
    {
        //generic arrays specific to their respective types
             int array_int[5] = {1, 2, 3, 4, 5};
             float array_float[5] = {1.123, 2.234, 3.345, 4.456, 5.567};
             char array_char[5] = {6, 7, 8, 9, 10};
             
             print(array_int);                                                   //Send with int-specific function
             print(array_float, 5);                                              //Send to float-specific function
             print(array_char, 1);                                                  //Send to char-specific function
             
             system("pause");
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    The function needs to have size as default...it's part of the homework >_<...sorry I didn't mention that.

    And it's consistently still printing the same junk...0x22ff50 for int, 0x22ff30 for float and a bunch of wierd characters for char.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    My critique of your work:

    * Your prototypes are okay ones, I'd just be careful what you pass to them. You used the int version in this context:
    Code:
        void print(const int data[], int size = 10);                    //prototype for int
        void print(const float data[], int size = 10);                  //prototype for float
        void print(const char data[], int size = 10);                   //prototype for char
        
        //generic arrays specific to their respective types
             int array_int[5] = {1, 2, 3, 4, 5};
             // ...
             print(array_int);
    Which isn't necessarily correct because array_int doesn't have ten items, like your function assumed if it receives just one argument.

    * Prototypes are for the compiler's benefit (well it also functions as a short reference). You place the prototypes before function definitions in the source code so that the compiler can enforce type safety and make sure you're calling your functions with a correct argument count. You shouldn't write a prototype in a function like main because I can't really see a use for it there. If the function is already written before it's called, then the compiler knows its arguments and how to enforce type safety.

    * You do something like this a lot:

    cout << "Triple int version: " << data << endl;

    THis prints a pointer address. That's the "junk" you see. But if I understand you correctly, you should just need to print the size variable enough times.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    And the reason you're getting all that junk is because you're printing the pointer itself, and not the data:
    Code:
    cout << "Triple int version: " << data << endl;
    You never actually specify an element to print.

    Now if this homework didn't require default arguments, I'd say you should remember that "important" arguments such as size should never be default arguments, 'kay?
    It makes it less error prone.
    Last edited by Elysia; 01-31-2008 at 03:40 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    Ahhh I see. It's printing addresses instead of values. How do I make it print the values in the array?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    You know how to use arrays? I don't just mean creating them but also accessing the data inside them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    Quote Originally Posted by Elysia View Post
    You know how to use arrays? I don't just mean creating them but also accessing the data inside them.
    No.

    This is the second program I've ever written and the little I know about arrays I learned from googling it. I understand now that it's treating the values as addresses that hold values...how do I make it print out the literal values in the array?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    If it's homework, then you're supposed to learn it. I can't tell you exactly how to do it since it's homework. You'll have to manage that on your own. But you should learn a little more about arrays.
    I'm sure there are resources about that around here somewhere...
    Ah, here: http://www.cprogramming.com/tutorial/lesson8.html
    If you learn about arrays, you should be able to solve the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    Oh I see. Every time I looped I was just printing the same pointer over and over again. I put an [i] after the data and it printed what was in the array. Duh! Thanks for the help.

    Crap. One more problem. On the array with char, it's printing out characters (like smiley faces and stuff). Is it possible to have an array with values like
    Code:
    {a, b, c, d, e, f, g, h, i, j}
    ? I tried and it was treating the letters as variables and kept telling me their undeclared variables...

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, tell your teacher that giving a size argument a default value is a very bad idea.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Yes, of course. You're telling the compiler to store 5, 6, 7, etc in the char array. However, you're not telling it to store the character representation of it.
    Storing characters is done using '', so '1', gives the character 1. But '1' is not the same as 1. Now you should be able to complete the rest.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    Quote Originally Posted by cpjust View Post
    Also, tell your teacher that giving a size argument a default value is a very bad idea.
    I'm sure she'd love hearing that from me, the only kid in the class who doesn't know how to use arrays.

    EDIT: Thanks for all the advice Elysia. Everything worked!
    Last edited by Hexadakota; 01-31-2008 at 04:09 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Btw, tell her that the pros you sought out for some homework help told you so
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2009, 12:21 AM
  2. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM
  3. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. illegal function overloading????
    By Mr_Jack in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2003, 01:03 PM

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