Need help calling functions 
Author Message
 Need help calling functions

I am taking a class on C.  I am having problems declaring a
function and calling it from the main.  Below is the script
I am currently working on.  I need to write a function that is
called from the main.  This function needs to have three parameters:
str, substr, and len.  The parameters str and substr are pointers to
char.  While the parameter len is an int.  

What I have written below won't even compile.  So, if there is someone
out there that can help me to declare my functions and understand the
WHY behind it, I would really appreciate it.

Please, either reply back to this newsgroup or send me email.

Thanks,

Ian Harisay

----------------------------------------------------------------------

#include <stdio.h>
#include <string.h>
#define LENGTH 81

main(){
        FILE *fp;
        char string[LENGTH], sub[LENGTH-1], *str, *substr, ans;
        int len, i, j, flag;
        len = 0;
        flag = 0;
        fp = fopen( "substring.dat","w" );
        ans = 'y';
        void myprint(void);
        void substring( *str, *substr, len );
        while( ans == 'y' || ans == 'Y' ){
                printf( "Enter a string no longer than %d characters long\n", LENGTH-1 );
                gets(string);
                j = strlen(string);
                do{
                        flag = 1;
                        printf( "Enter character # to start substring.\n" );
                        scanf( "%d", &i );
                        if( i < 1 || i > j ){
                                printf( "You must enter a number from 1 to %d\n", j-1 );
                                flag = 0;}
                        i--;
                }while( flag != 1 );
                do{
                        flag = 1;
                        printf( "How long do you want the substring to be?\n" );
                        scanf( "%d", &len );
                        if( len < 1 || i+len > j ){
                                printf( "Substring length needs to be from 1 to %d\n", j-i );
                                flag = 0;}
                }while( flag != 1 );
                sub = substring( str, substr, len  );
                fprintf( fp, "%s\n\nis a substring of:\n\n%s\n\n\n", sub, string );
                printf( "\n\nDo you want to enter another string? Y/N\n" );
                ans = getc( stdin );}
        fclose( fp );
        myprint();

Quote:
}

void myprint(void){
        int ans;
        ans = 0;
        while( ans != 1 || ans != 2 || ans != 3 ){
                printf( "Do you want a:\n\t(1) Hard Copy?\n\t(2) Soft Copy?\n\t(3) Both?" );
                scanf( "%d", &ans );
                if( ans != 1 || ans != 2 || ans != 3 )
                        printf( "You must choose one:\n\t(1) Hard Copy?\n\t(2) Soft Copy?\n\t(3) Both?" );
                if( ans != 1 || ans != 2 || ans != 3 )
                        printf( "You must choose one:\n\t(1) Hard Copy?\n\t(2) Soft Copy?\n\t(3) Both?\n" );
                if( ans == 1 )
                        system( "/usr/lib/ps/enscript -B -f Courier10 substring.dat" );
                if( ans == 2 )
                        system( "/usr/bin/more substring.dat" );
                if( ans == 3 ){
                        system( "/usr/bin/more substring.dat" );
                        system( "/usr/lib/ps/enscript -B -f Courier10 substring.dat" );}}

Quote:
}

substring( char *str, char *substr, int len ){
Quote:
}



Sat, 01 Nov 1997 03:00:00 GMT  
 Need help calling functions

: #include <stdio.h>
: #include <string.h>
: #define LENGTH 81

: main(){
:         FILE *fp;
:         char string[LENGTH], sub[LENGTH-1], *str, *substr, ans;
:         int len, i, j, flag;
:         len = 0;
:         flag = 0;
:         fp = fopen( "substring.dat","w" );
:         ans = 'y';
!         void myprint(void);
!         void substring( *str, *substr, len );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1.) if this should be function-declarations, move them out of main()
2.) void substring( *str, *substr,len) is not a fununction-declaration
    nor is it a call to the function substring defined below

:         while( ans == 'y' || ans == 'Y' ){
:                 printf( "Enter a string no longer than %d characters long\n", LENGTH-1 );
:                 gets(string);
:                 j = strlen(string);
:                 do{
:                         flag = 1;
:                         printf( "Enter character # to start substring.\n" );
:                         scanf( "%d", &i );
:                         if( i < 1 || i > j ){
:                                 printf( "You must enter a number from 1 to %d\n", j-1 );
:                                 flag = 0;}
:                         i--;
:                 }while( flag != 1 );
:                 do{
:                         flag = 1;
:                         printf( "How long do you want the substring to be?\n" );
:                         scanf( "%d", &len );
:                         if( len < 1 || i+len > j ){
:                                 printf( "Substring length needs to be from 1 to %d\n", j-i );
:                                 flag = 0;}
:                 }while( flag != 1 );
:                 sub = substring( str, substr, len  );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3.) if this should work, then substring should be a function returning a pointer
    to a char

:                 fprintf( fp, "%s\n\nis a substring of:\n\n%s\n\n\n", sub, string );
:                 printf( "\n\nDo you want to enter another string? Y/N\n" );
:                 ans = getc( stdin );}
:         fclose( fp );
:         myprint();
: }

: void myprint(void){
:         int ans;
:         ans = 0;
:         while( ans != 1 || ans != 2 || ans != 3 ){
:                 printf( "Do you want a:\n\t(1) Hard Copy?\n\t(2) Soft Copy?\n\t(3) Both?" );
:                 scanf( "%d", &ans );
:                 if( ans != 1 || ans != 2 || ans != 3 )
:                         printf( "You must choose one:\n\t(1) Hard Copy?\n\t(2) Soft Copy?\n\t(3) Both?" );
:                 if( ans != 1 || ans != 2 || ans != 3 )
:                         printf( "You must choose one:\n\t(1) Hard Copy?\n\t(2) Soft Copy?\n\t(3) Both?\n" );
:                 if( ans == 1 )
:                         system( "/usr/lib/ps/enscript -B -f Courier10 substring.dat" );
:                 if( ans == 2 )
:                         system( "/usr/bin/more substring.dat" );
:                 if( ans == 3 ){
:                         system( "/usr/bin/more substring.dat" );
:                         system( "/usr/lib/ps/enscript -B -f Courier10 substring.dat" );}}
: }

: substring( char *str, char *substr, int len ){

: }

4.) to match your previously declaration this has to be void substring(...)

I do not really know what your program is supposed to do, but here is my guess what
you should do

...
void myprint(void);
void substring(char *,char *,int);

main(){

        ...
        substring(string,sub,len);
        ...

Quote:
}

void myprint(void){...}
void substring(char *str,char *substring,int len){...}

I guess inside of substring you want initialise the variable substring with the
desired substring of str.

I hope this helps,

Cheers,
        Chris



Mon, 03 Nov 1997 03:00:00 GMT  
 Need help calling functions
Here, try this:

--

#include <stdio.h>
#include <string.h>
#define LENGTH 81

main()
{
  FILE *fp;
  char str[LENGTH], sub[LENGTH-1], ans;
  int len, i, j, flag;

  void myprint(void);
  void substring(char *str, char *substr, int i, int len);

  len = 0;
  flag = 0;
  fp = fopen( "substring.dat","w" );
  ans = 'y';

  while( ans == 'y' || ans == 'Y' ) {

    printf( "Enter a string no longer than %d characters long\n", LENGTH-1 );
    gets(str);
    j = strlen(str);

    do {
      flag = 1;
      printf( "Enter character # to start substring.\n" );
      scanf( "%d", &i );
      if( i < 1 || i > j ){
        printf( "You must enter a number from 1 to %d\n", j-1 );
        flag = 0;
      }
      i--;
    } while( flag != 1 );

    do {
      flag = 1;
      printf( "How long do you want the substring to be?\n" );
      scanf( "%d", &len );
      if( len < 1 || i+len > j ){
        printf( "Substring length needs to be from 1 to %d\n", j-i );
        flag = 0;
      }
    } while( flag != 1 );

  substring(str, sub, i, len);
  fprintf( fp, "%s\n\nis a substring of:\n\n%s\n\n\n", sub, str );
  printf( "\n\nDo you want to enter another string? Y/N\n" );
  ans = getc( stdin );}
  fclose( fp );

  myprint();

Quote:
}

void myprint(void)
{
  int ans;

  ans = 0;

  while( ans != 1 && ans != 2 && ans != 3 ) {

    printf( "Do you want a:\n\t(1) Hard Copy?\n\t(2) Soft Copy?\n\t(3) Both?" );
    scanf( "%d", &ans );

    if( ans == 1 )
      system( "/usr/lib/ps/enscript -B -f Courier10 substring.dat" );
    if( ans == 2 )
      system( "/usr/bin/more substring.dat" );
    if( ans == 3 ) {
      system( "/usr/bin/more substring.dat" );
      system( "/usr/lib/ps/enscript -B -f Courier10 substring.dat" );
    }
  }

Quote:
}

void substring( char *str, char *substr, int i, int len )
{
  strncpy(substr, &str[i], len);

Quote:
}

--

Sample execution:

Enter a string no longer than 80 characters long
abcdefghijklmnopqrstuvwxyz
Enter character # to start substring.
3
How long do you want the substring to be?
10

Do you want to enter another string? Y/N
Do you want a:
        (1) Hard Copy?
        (2) Soft Copy?
        (3) Both?2
cdefghijkl

is a substring of:

abcdefghijklmnopqrstuvwxyz

--

Comments:

I wouldn't necessarily do this the way you did, I just perturbed
the code enough to get it working.

Learn about separating declaration and execution.

Read a book on programming style.  I think one (or both) of the
"Programming Pearls" books might help, as well as a book (whose
title escapes me) by (I believe) Kernighan & Plauger, that taught
good style by poking fun at silly code..

Pat



Wed, 05 Nov 1997 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. C code for a function called from a 4GL- need help

2. Need help with _int86 function call in win95

3. HELP NEEDED : value of ESP was not properly saved across a function call

4. (HELP NEEDED!)calling conventions / function pointers

5. (HELP NEEDED!)calling conventions / function pointers

6. Help: Need to export VC 5.0 function in DLL to be called from VB 5.0

7. Help needed calling redirected vtable function

8. (HELP NEEDED!)calling conventions / function pointers

9. Need help terminating a function call

10. Help : Cross reference/Call structure of C function calls

11. Condition breakpoints and function calls and function calls in Immediate window

12. Urgent Help Needed-Calling the help file.

 

 
Powered by phpBB® Forum Software