function

This is a discussion on function within the C Programming forums, part of the General Programming Boards category; This is a program that asks teh user to input first char line, second a digit line, and third a ...

  1. #1
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    function

    This is a program that asks teh user to input first char line, second a digit line, and third a printable line.

    I want to check if the user typed teh correct info by using isalpha, isdigit, isprint.

    I want to use one function that can test for all.

    My problem is I am nor sure whether my function prototype is correct and whether I can pass isalpha or isdigit in the function.

    I want "field" to be replaced by either fielda,b,c; and check to be replaced by isalpha, isdigit or isprint.

    Thanks for the help!!!


    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    #define chances 3
    
    void field (int field[],int check);
    int main (void)
    {
    	char fielda[20];
    	char fieldb[20];
    	char fieldc[20];
    	int yesno;
    	
    	printf("Enter text: ");
    	fgets(fielda,sizeof fielda,stdin);
    
    	field(fielda,isalpha);
    
    	printf("\nEnter numbers: ");
    	gets(fieldb);
    
    
    	printf("\nEnter printable: ");
        gets(fieldc);
    	
    
    	puts(fielda);
    	printf("\n");
    
    	puts(fieldb);
    	printf("\n");
    
    	puts(fieldc);
    	printf("\n");
    
    	puts("Is the information correct? (y/n)\n");
    	scanf("%c/n",&yesno);
    		
    	return 0;
    }
    
    void field (int field[],int check)
    {
    	int i,k,time;
    
    	for (k=0;k<chances;k++)
    	{
    		strtok(field,"\n");
    		time=0;
    		for (i=0;i<field[i]!='\0';i++)
    		{
    			if (check(field[i]))	continue;
    				else
    				{
    				printf("\nTry again!\n\n");
    				time=1;
    				break;
    				}
    		}
    		if (time==0) break;
    	}
    	if (time==1)
    	{
    		fprintf(stderr,"\nYour 3 retries are up!\n");
    		exit(1);
    	}
    
    	return;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Your tabs got a little crazy there but I think you are asking: "How to I pass a function as a parameter?" If that is your question you can do the following.

    //prototype
    void field(int [], int(*)(int));

    and for the function:

    void field(int whatever[], int (*function)(int)) { }

    Since that is ugly you may also do something like this

    Code:
    typedef int (*param_func)(int);
    
    void field(int[], param_func);
    ...
    void field(int something[], param_func function) { }

  3. #3
    Max
    Max is offline
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Is this correct?.....does not compile! something is wrong with my function!!

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdlib.h>
    #define chances 3
    
    void field(int [], int(*)(int));
    int main (void)
    {
    	char fielda[20];
    	char fieldb[20];
    	char fieldc[20];
    	int yesno;
    	
    	printf("Enter text: ");
    	fgets(fielda,sizeof fielda,stdin);
    
    	field(fielda,isalpha(fielda);
    
    	printf("\nEnter numbers: ");
    	gets(fieldb);
    
    
    	printf("\nEnter printable: ");
        gets(fieldc);
    	
    
    	puts(fielda);
    	printf("\n");
    
    	puts(fieldb);
    	printf("\n");
    
    	puts(fieldc);
    	printf("\n");
    
    	puts("Is the information correct? (y/n)\n");
    	scanf("%c/n",&yesno);
    		
    	return 0;
    }
    
    void field(int array[], int (*function)(int)) 
    {
    	int i,k,time;
    
    	for (k=0;k<chances;k++)
    	{
    		strtok(array,"\n");
    		time=0;
    		for (i=0;i<array[i]!='\0';i++)
    		{
    			if (function(array[i])) continue;
    				else
    				{
    				printf("\nTry again!\n\n");
    				time=1;
    				break;
    				}
    		}
    		if (time==0) break;
    	}
    	if (time==1)
    	{
    		fprintf(stderr,"\nYour 3 retries are up!\n");
    		exit(1);
    	}
    
    	return;
    }

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Post start writing from scratch

    Hello Max,

    The program you are trying is somewhat crazy. Better u start working it right from scratch, because ur program lacks some regularity.
    Or perfectly explain what ur program needs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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