I created my own print function but it isn't as good as the C function

This is a discussion on I created my own print function but it isn't as good as the C function within the C++ Programming forums, part of the General Programming Boards category; I know this is stupid and pointless. But I'm testing playing around with functions. An C++ expert said the only ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    156

    I created my own print function but it isn't as good as the C function

    I know this is stupid and pointless. But I'm testing playing around with functions. An C++ expert said the only way I'll learn is if I play around with the function(Silent Strike, Okie, and FLIKM). So whats wrong with my print function?

    #include <iostream>
    #include <stdio.h>
    #include <conio.c>

    int printf(const char *str);

    void main()
    {
    printf("Hello World!!\n");
    cin.get();
    }

    int printf(const char *str)
    {
    for (int i=0;i<str;i++, str++) {
    putchar(*str);
    putchar('\n'); }

    return i;
    }
    Last edited by Golden Bunny; 04-30-2002 at 08:03 PM.
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    well for one chars are '\n' not "\n" so you are passing a wrong value.

    why don't you just make an overloaded function for each of the different variable you are going to pass to your printf() ?
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    right.......your mind is lost.......
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >So whats wrong with my print function?

    It uses other peoples print functions. Apart from that it should probably look something like -

    Code:
    #include <iostream> 
    #include <string> 
    
    using namespace std;
    
    void myprintf(string outprint, int integer, char character, float floatpoint); 
    
    int main() 
    { 
    myprintf("Hello World!!\n", 8, '\n', 9.70); 
    cin.get(); 
    return 0;
    } 
    
    void myprintf(string outprint, int integer, char character, float floatpoint) 
    { 
    outprint=""; 
    cout << outprint; 
    cout << integer; 
    cout << character; 
    cout << floatpoint; 
    }
    Not sure why you're erasing outprint before outputting it though.

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Wow great.. I though he had created his own header and defined printf..

  6. #6
    Intranasal Heroin User Xterria's Avatar
    Join Date
    Sep 2001
    Location
    Buffalo, NY
    Posts
    1,035
    if you use the parameters
    (const char *fmt, ...)
    doesn't that create the equivalent of printf?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >doesn't that create the equivalent of printf?
    Not unless you also create the intensive error checking and formatting options involved in printf as well. Setting up the variable length argument lists is just the beginning. printf is a rather involved function.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    eh... well, this is worthless... the purpose of there already being a printf is so that you don'thave to write your own.

    ...i'd suggest doing something more productif, aktif, rejuvanatif.
    (doubleantif.)

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >the purpose of there already being a printf is so that you
    >don'thave to write your own
    True, but it's excellent practice to rewrite the standard library functions in your spare time. You don't have to use them, but just implementing them is quite a learning experience.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    as long as you're doing that, why not just output through the real printf function?
    or you can output directly to memory

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    156
    I agree with Prelude 100%. thats why I did it
    Compiler: MingW(IDE: Bloodshed Dev-C++ 4.01)
    Web Site: Zoo Crew
    Forums: Zoo Boards
    E-mail: goldenbunny84@hotmail.com

    "Do you wanna go to jail or do you wanna go home?!?!" - Alonzo(Training Day)

  12. #12
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    well, anyway, if i was doing what you are adoing, GB, i would go as low level as possible with it instead of using a function on the same or higher level.

  13. #13
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    I agree with Prelude and Aran Elus. Writing the standard library functions are fun and can be worth learning. However, if you are going to do so, try and do it with functions that are on a different level. Like this:

    Code:
    int myPrint(const char *string, int size)
    {
       for(int i = 0; i < size; i++, string++)
          putchar(*string);
    
       putchar('\n');
    
       return i;
    }
    Instead of using cout << or printf(), I used the old C putchar(). Try something like that.

    Have fun

  14. #14
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    You dont learn anything by making a function which uses cout only.
    If you do it without any of the other output functions then thats another thing =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM

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