using a driver function to test my code??

This is a discussion on using a driver function to test my code?? within the C Programming forums, part of the General Programming Boards category; I need to create a function called writeNumber() using the switch statement. The input argument is ch of type char. ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    using a driver function to test my code??

    I need to create a function called writeNumber() using the switch statement. The input argument is ch of type char. Now, if ch is not a digit between '0' and '9', then the function returns the value 0 (I can use the isidigit() function). Oherwise, the function writes to the screen the correct English word of the digit charatcer followed by a newline charatcer and has a return value of 1. Once I write the function, I have to use the following driver to test my function:
    [CODE]
    #include <stdio.h>
    #include <ctype>
    #include(conio.h>

    void main()
    {
    char c;
    int num;

    printf("hit any number key>>");
    c=(char) getch();
    printf("\nYou entered %c>>",c);

    num=WriteNumber(c);
    if (num==0)
    printf("\nThat wasn't a number key\n");
    }
    [\CODE]
    What is a driver function? Do I create my function and then place it below the last } and then try to compile it? I have no idea how to start this. if anybody wants to give me a hint, I would greatly appreciate it. I dont want you to write it for me, I just want to know where to begin. Do I just ignore the driver function and concentrate on my function? Thanks much for your help. I appreciate it... Tommy

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    Sorry for the capital code. Here it is again:

    Code:
    #include <stdio.h>
    #include <ctype>
    #include(conio.h>
    
    void main()
    {
    char c;
    int num;
    
    printf("hit any number key>>");
    c=(char) getch();
    printf("\nYou entered %c>>",c);
    
    num=WriteNumber(c);
    if (num==0)
         printf("\nThat wasn't a number key\n");
    }
    mod-fixed

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Use the PERVIEW before the submit. That way you can correct your post! And didn't you notice the problem before submitting the second post?

    Yes, you can put your function after the last brace in the driver. Put a prototype at the top, above main() too.

    Or you can put your function above main()

    Either way.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > Use the PERVIEW before the submit.
    Was that an intentional mis-spelling of PREVIEW just to emphasis the point?
    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
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I need to create a function called writeNumber() using the switch statement.

    Here's the function prototype:
    Code:
    int WriteNumber(char ch);
    Now use a switch statement inside the function to check for each possible digit.

    > Use the PERVIEW before the submit.
    Is that like PAYPERVIEW?

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    What does this mean: "The function returns a value of 0 if ch is not between 0-9?" Does this mean that when a user types in a letter, a 0 will be displayed?

    Also, after the program writes the digit in English, the program is suppose to follow by a "newline character and has a return value of 1?" Is the newline charatcer \n? What's a return value of 0" Thanks again....Tommy

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    How do you use the isdigit function? So far, I have:
    Code:
    while isdigit(num) {
    //loop

    But I think this is incorrect. The while isdigit is probably correct, but you have to have something like while isdigit(char ch) or something close tp that effect. I decided to use num because this is the number that the user ill be typing. But what I really need to have is the input from the user, regardless if it's a number or letter.

    Tommy

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >while isdigit(num) {
    while ( isdigit ( num ) )

    Here is a more complete example:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int valid ( const char *s );
    
    int main ( void ) 
    { 
      char buff[BUFSIZ];
    
      while ( fgets ( buff, sizeof buff, stdin ) != NULL ) {
        char *newline = strrchr ( buff, '\n' );
        if ( newline != NULL )
          *newline = '\0';
        if ( valid ( buff ) )
          printf ( "%d\n", atoi ( buff ) );
        else
          printf ( "Not valid\n" );
      }
    
      return 0; 
    }
    
    int valid ( const char *s )
    {
      while ( *s != '\0' && isdigit ( *s ) )
        s++;
      return *s == '\0'; /* Valid if end of string */
    }
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Make the distinction between strings (character arrays) and characters.

    Since characters are just fancy numbers you can use a switch statement.

    First check if the character is a digit.
    Code:
    if isdigit(c)
      print it out
      end up returning 1
    else
      print "That wasn't a number"
      end up returning 0
    end
    Once you know it's a digit then use a switch statement (this is the 'print it out' bit)
    Code:
    switch c
      case '0': print "Zero"
      case '1': print "One"
      case '2': print "Two"
        etc
      otherwise: never be an otherwise
    end
    btw you'll have to translate into C code.

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    78

    can someone check my code

    Can someone please see if this program does what it's supposed to do. Earlier, I wrote down what this program is supposed to do. I am not not sure about the returning the value to 0 and the newline character (I did \n as newline character) which has a value of 1????? Thanks for any input. I know this code is sort of mundane, but I have a lot to learn:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    
    int writeNumber(int);
    
    void main()			/*DRIVER FUNCTION TO TEST THE FUNCTION*/
    {
    	char ch;
    	int num;
    
    	printf("Hit any number key>>");
    	ch=(char) getch();
    	printf("\nYou entered %c>>",ch);
    
    	num=writeNumber(ch);
    	if (num==0)
    		printf("\nThat wasn't a number key!\n");
    }
    
    int writeNumber(int x)
    {
    int num;
    
     if (isdigit (x));	/* while input equals a number */
    
    	
    	switch (x)
    	{
    	case '0':
    		printf("Zero\n");
    		break;
    	case '1':
    		printf("One\n");
    		break;
    	case '2':
    		printf("Two\n");
    		break;
    	case '3':
    		printf("Three\n");
    		break;
    	case '4':
    		printf("Four\n");
    		break;
    	case '5':
    		printf("Five\n");
    		break;
    	case '6':
    		printf("Six\n");
    		break;
    	case '7':
    		printf("Seven\n");
    		break;
    	case '8':
    		printf("Eight\n");
    		break;
    	case '9':
    		printf("Nine\n");
    		break;
    	default:	/* If user types anything but a number, x = 0 */
    		x=0;
    	
    	 num=x;	               /*assigns user input to num*/
    	return (num);	/*num is = to the function, returning num back to function */
    	}
    }

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    It does what it is supposed to do; the only bad thing I see here is the
    Code:
    void main(){
    always write
    Code:
    int main(void){
    or the arguments for main...argc,argv, and that inconspicuos other environment one(envp)

    [edit] wait a second... you don't need num defined in the funtion at all, also don't name two variable the same, it is a little misleading even though they are both local. just return x, set x=1 in the top of that function, and then change it if it isn't a number.[/edit]
    Last edited by linuxdude; 03-16-2004 at 08:30 PM.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    78
    The driver function is exactly the way I copied it from the sheet of paper my instructor gave me.

    I am lost. I did what you said and it works the same, but why didnt I have to declare num. Is it because Num is equal to my function and the scope of num is valid from Main to the end of function? Just curios as to why it still works after deleting num from my function and then returning x. Where exactly in the function should I let x=1 and why would I even do this? Thanks for the help and tips. Tommy

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > The driver function is exactly the way I copied it from the sheet of paper my instructor gave me.
    Join the club!
    You simply wouldn't believe the number of teachers out there who apparently don't know C at all, or know enough to be a danger to others.
    If they ever mention gets() or fflush(stdin), be afraid, for they are truly clueless and you will learn little of lasting value from them.

    > int num;
    Initialise it to a value. I would suggest using 0 as the default 'wrong' answer, and set num to 1 for all of the correct answers in the switch/case.

    > if (isdigit (x)); /* while input equals a number */
    1. The ; at the end makes this a null-statement
    2. the code and the comment dis-agree - which should it be?

    > return (num);
    Look carefully, the return is just part of the default case only.
    The return result from all the other cases is random, because all you get is what happens to be around when you fall off the end of the function.

    I would suggest you use a decent compiler with lots of diagnostics, but from your tutors example, it looks like you're stuck in the stone age using some DOS fossil compiler.
    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
    Join Date
    Mar 2004
    Posts
    78
    I am actually using Microsoft Visual C++ 6.0 compiler, but I only know how to compile and that's about it. I dont know what this compiler can really do, such as diagnostics.

  15. #15
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Originally posted by Salem
    If they ever mention gets() or fflush(stdin), be afraid, for they are truly clueless and you will learn little of lasting value from them.
    To gets() and fflush(stdin) I would add void(main) - somebody said somewhere that it a good litmus test as to whether or not somebody knows C is how they deal with these three.

    ~/

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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