Function Prototyping

This is a discussion on Function Prototyping within the C Programming forums, part of the General Programming Boards category; I'm looking to make a function for my menus. I want the first section to be a different color then ...

  1. #1
    Registered User MiLo's Avatar
    Join Date
    Jun 2004
    Posts
    17

    Function Prototyping

    I'm looking to make a function for my menus. I want the first section to be a different color then the second section. So what they push to be one color and the explination another. I got that working great, by passing 2 strings to the function and printing them accordingly.

    My problem is i would like to be able to have interger variable in the string. Using printf i would just put a %d and stick it in. Is there a way i can write my function to accept a string with a conversion character in it. My code for the menu printing looks like this:

    Code:
    void menuprn(char number[], char str[])            //Menu Printing
    {
        textcolor(WHITE);
        putchar('\t');
        cprintf("%s ", number);
        textcolor(LIGHTGRAY);
        cprintf("%s", str);
        putchar('\n');
        return;
    }
    And my code of that i want to use this function is this:
    Code:
    menuprn("(1)", "\tAces\t%d\t", aces[playerNum]);
    And it would be nice if someone were to ask some questions on this board i know the answers to. Im starting to feel bad for not asking all these questions and never having an answer to anyone else's posts. If there is a place where there is really newbie questions being asked point me there so i can help some people out.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    There's a very cool and simple way to do this thing, and it only takes a little bit of code.

    Here we go:

    Code:
       void myPrint(char *text, ...)
       {
       	if(!text)
       		return;
       
       	char buff[512];
       
       	va_start(ap, text);
       	vsprintf(buff, text, ap);
       	va_end(ap);
       
       // Do whatever with buff, as now all of the %d's and %s's and %c's etc have been replaced correctly
       }
    in your case the function declaration would be:
    Code:
      void menuprn(char number[], char str[],...)
    So just apply that to your function. Basically all you have to do is add a ... to the end of your function and then those 3 functions (the va_start, vsprintf, and va_end) in there, and it'll parse your buffer for you.

    And don't feel bad about not being able to answer too many questions, we all had to start somewhere, so just keep learning and eventually you'll be able to help more and more from your experience!

    -Hope that helps!
    -Good luck with coding!
    Last edited by jverkoey; 06-21-2004 at 12:34 AM.

  3. #3
    Registered User MiLo's Avatar
    Join Date
    Jun 2004
    Posts
    17
    I'm getting an error
    `ap' undeclared (first use this function)
    so i am assuming that i don't have a header file or i need to declar ap as some sort of varable. What would that be? I changed text to str guessing that it is supposed to be that. Is that correct?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,852
    You also need

    #include <stdarg.h>
    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.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    jverkoey just forgot to declare the variable
    Code:
    return_type func_name ( first_parameter_type first_parameter_name, ...)
    {
      va_list ap;
      va_start(ap, first_parameter_name);
      /* do something with it */
      va_end(ap);
    }

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    oops, sorry, that's my fault

    you have to add one variable in there:

    Code:
      va_list ap;
    sorry, I missed that when I pulled it from my code library

    -edit-
    meh, looks like you beat me to it thantos, heh

  7. #7
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by MiLo
    Code:
    void menuprn(char number[], char str[])            //Menu Printing
    {
        textcolor(WHITE);
        putchar('\t');
        cprintf("%s ", number);
        textcolor(LIGHTGRAY);
        cprintf("%s", str);
        putchar('\n');
        return;
    }
    Beside the good answers you already received, I will add that you should be more consistent with your outputs.

    The standard output is a stream called 'stdout'. It's used by standard functions like putchar() or printf() etc. (Details belong to your C-book)

    You have taken the decision to use non standard functions provided originally by Borland (the 'conio' library) that deal almost directly with the screen hardware. (textcolor(), cprintf() etc.)

    It is technically possible to mix the output and it makes sense when the meaning is different. For example.

    printf ("Clear the screen\n");
    clrscr();

    If you execute this "as it", it will put a comment to the standard output (probably the screen) and clear the screen afterwards.

    As long as the standard output is the screen, it doesn't make sense. But you must know that the standard output can be 'redirected', either by the command line

    [system prompt] command > out.txt
    [system prompt] command > com1

    etc.

    or by a standard function call (freopen()). Once redirected, the stdout outputs will be stored sequencially in a text file or some serial device (console, line printer etc.). It can help debugging. This technique is known as 'tracing'. Its useful when you are programming full screen (like yours) or graphics applications (GUI).

    To come back to your code, you should replace the putchar() calls (stdout) by putch() (conio).
    Emmanuel Delahaye

    "C is a sharp tool"

  8. #8
    Registered User MiLo's Avatar
    Join Date
    Jun 2004
    Posts
    17
    I put it all in the way i understood it, and it will run for me. Its not doing what its supposed to though. It prints the %d as a %d. This is what i have done:
    Code:
    void menuprn(char number[], char str[], ...)            //Menu Printing
    {
       char buff[512];
       va_list ap;
         if(!str)
       		return;
     		va_start(ap, str);
       	vsprintf(buff, str, ap);
       	va_end(ap);
       	
        textcolor(WHITE);
        putchar('\t');
        cprintf("%s ", number);
        textcolor(LIGHTGRAY);
        printf("%s", str);
        putchar('\n');
        return;
    }
    Code:
    menuprn("(1)", "\tAces\t%d\t", aces[playerNum]);
    menuprn("(7)", "\t3 of a Kind\t%d\n", tOfKind[playerNum]);
    Did i not stick it in properly? or is it something else.

  9. #9
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by MiLo
    I put it all in the way i understood it, and it will run for me. Its not doing what its supposed to though. It prints the %d as a %d. This is what i have done:
    <...>
    Did i not stick it in properly? or is it something else.
    You used the wrong buffer (it's buff and not str)

    This probably what you want. conio and stdout have a different behaviour. Once again, be consistent and don't mix them unless you have a good reason to do it..

    Code:
    #include <conio.h>
    #include <stdarg.h>
    #include <stdio.h>
    
    #define EOL "\n\r"
    
    /* Menu Printing */
    void menuprn (char number[],
                  char str[],
                  ...)
    {
       if (str != NULL)
       {
          char    buff[512];
          va_list ap;
    
          va_start (ap, str);
          vsprintf (buff, str, ap);
          va_end (ap);
    
          textcolor (WHITE);
          cprintf ("%-8s ", number);
          textcolor (LIGHTGRAY);
          cprintf ("%s" EOL, buff);
       }
    }
    
    int main (void)
    {
       int     aces[] = { 1, 2, 3, 4 };
       int     tOfKind[] = { 5, 6, 7, 8, 9 };
       int     playerNum = 2;
    
       menuprn ("(1)", "%-16s%8d", "Aces", aces[playerNum]);
       menuprn ("(7)", "%-16s%8d", "3 of a Kind", tOfKind[playerNum]);
       return 0;
    }
    Code:
    D:\C-BOARD\M\MILO>bc proj.prj
    (1)      Aces                   3
    (7)      3 of a Kind            7
    Last edited by Emmanuel Delaha; 06-22-2004 at 05:10 AM. Reason: Wording, Typos
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #10
    Registered User MiLo's Avatar
    Join Date
    Jun 2004
    Posts
    17
    Ok, i haven't had a chance to try this out, but im wondering what you ment by i used the wrong buffer?

    Also what are the -16 and 8s. How do those work? (I'd like to understand what the code is doing so i can apply it to other stuff in the future)

    Thanks for the help

  11. #11
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by MiLo
    Ok, i haven't had a chance to try this out, but im wondering what you ment by i used the wrong buffer?
    You have build a string in 'buff', but you have printed 'str' instead of 'buff'. I have fixed your code by printing 'buff' instead of 'str'.
    Also what are the -16 and 8s. How do those work? (I'd like to understand what the code is doing so i can apply it to other stuff in the future)
    This is a very basic question. Please open your C-book at search 'printf()' or the like. You will find an explanation about 'formatters'.

    "%s" displays a string
    "%10s" displays a string with 10 leading spaces or less
    "%-10s" displays a string with 10 trailing spaces or less

    I have done that to replace the '\t' because they are not interpreted by the conio functions.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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