finding chars in dynamic arrays 
Author Message
 finding chars in dynamic arrays

I am getting some errors trying to make this piece of code work.  When
attempting to compile I get the following error:

dyn_storage.cpp
D:\C\dyn_storage\dyn_storage.cpp(49) : error C2660: 'fgets' : function does
not take 1 parameters
D:\C\dyn_storage\dyn_storage.cpp(49) : fatal error C1903: unable to recover
from previous error(s); stopping compilation
Error executing cl.exe.

dyn_storage.exe - 2 error(s), 0 warning(s)

Here is the code I am using to create a dynamic array of names and find the
number of times that letters a,e,i,o,u are found.

/*      Build a dynamic array of names.
           Written by:
           Date:
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#define FLUSH while (getchar() != '\n')

int main (void)
{
/* Local Definitions */
        char  input[81];
        char **pNames;      /* array of pointers to char */

        int size;
        int curCh;
        int namesIndex;
        int verb_count = 0;

/* Statements */
        printf("How many names do you plan to input? ");
        scanf ("%d", &size);
        FLUSH;

        /* Allocate array in heap.
           One extra element added for loop control */
        pNames = (char **) calloc (size + 1, sizeof (char *));
        printf("Enter names. <EOF> to stop\n");

        namesIndex = 0;
        while (namesIndex < size
            && fgets(input, sizeof(input), stdin))
            {
             *(pNames + namesIndex)  = (char *)
                  calloc (strlen(input) + 1, sizeof(char));
             strcpy (*(pNames + namesIndex), input);
             namesIndex++;
           } /* while */

        *(pNames + namesIndex) = NULL;
        printf("\nYour names are: \n");
        namesIndex = 0;
        //while (*(pNames + namesIndex))
          // {
         while ((curCh = fgets(*(pNames + namesIndex))))
        {
                  if(curCh == 'a' || curCh == 'e' || curCh == 'i' || curCh ==  
                       'o' || curCh == 'u' || \
                        curCh == 'A' || curCh == 'E' || curCh == 'I' || curCh
                        == 'O' || curCh == 'U')
                        verb_count++;

                //printf("%3d: %s",
                   // namesIndex, *(pNames + namesIndex) );
            namesIndex++;
           } /* while */

    printf("Number of verbs: %d\n", verb_count);

        return 0;

Quote:
}       /* main */



Sun, 03 Aug 2003 06:19:41 GMT  
 finding chars in dynamic arrays

Quote:

> dyn_storage.cpp

DO NOT call your source file foo.cpp because compilers will think
it's C++ source.

Quote:
> Here is the code I am using to create a dynamic array of names and find the
> number of times that letters a,e,i,o,u are found.

> /*      Build a dynamic array of names.
>            Written by:
>            Date:
> */
> #include "stdio.h"
> #include "stdlib.h"
> #include "string.h"

Use the superior <> form

Quote:
> #define FLUSH while (getchar() != '\n')

A test for EOF might not be amiss, but adding it to the macro would
make it even uglier, see below.

Quote:
> int main (void)
> {
> /* Local Definitions */
>         char  input[81];
>         char **pNames;      /* array of pointers to char */

>         int size;
>         int curCh;
>         int namesIndex;
>         int verb_count = 0;

> /* Statements */
>         printf("How many names do you plan to input? ");
>         scanf ("%d", &size);

Check the return value, limit the number of significant digits. Or,
even better, don't use scanf().

Quote:
>         FLUSH;

{ /* note the brace */
    int c;
    while((c = getchar()) != '\n' && c != EOF)
        ;

Quote:
}
>         /* Allocate array in heap.
>            One extra element added for loop control */
>         pNames = (char **) calloc (size + 1, sizeof (char *));

Don't cast the return value of malloc() and friends, calloc() is
useless except for arrays of unsigned char, the size member could be
written sizeof *pNames. Check for NULL.

Quote:
>         printf("Enter names. <EOF> to stop\n");

>         namesIndex = 0;
>         while (namesIndex < size
>             && fgets(input, sizeof(input), stdin))
>             {
>              *(pNames + namesIndex)  = (char *)

*(long + ugly) can also be written like[this].

Quote:
>                   calloc (strlen(input) + 1, sizeof(char));
>              strcpy (*(pNames + namesIndex), input);
>              namesIndex++;
>            } /* while */

>         *(pNames + namesIndex) = NULL;
>         printf("\nYour names are: \n");
>         namesIndex = 0;
>         //while (*(pNames + namesIndex))
>           // {
>          while ((curCh = fgets(*(pNames + namesIndex))))
>         {
>                   if(curCh == 'a' || curCh == 'e' || curCh == 'i' || curCh ==  
>                        'o' || curCh == 'u' || \
>                         curCh == 'A' || curCh == 'E' || curCh == 'I' || curCh
>                         == 'O' || curCh == 'U')
>                         verb_count++;

How about a switch?

switch(curCh)
{
    /* multiple fall-throughs ahead */
    case 'a':   case 'e':   case 'i':   case 'o':   case 'u':
    case 'A':   case 'E':   case 'I':   case 'O':   case 'U':
        verb_count++;

Quote:
}
>                 //printf("%3d: %s",
>                    // namesIndex, *(pNames + namesIndex) );
>             namesIndex++;
>            } /* while */

>     printf("Number of verbs: %d\n", verb_count);

>         return 0;
> }       /* main */

Gergo

--
Reality is nothing but a collective hunch.
        -- Lily Tomlin



Sun, 03 Aug 2003 06:55:55 GMT  
 finding chars in dynamic arrays

[...]

Quote:
>>                   if(curCh == 'a' || curCh == 'e' || curCh == 'i' || curCh ==  
>>                        'o' || curCh == 'u' || \
>>                         curCh == 'A' || curCh == 'E' || curCh == 'I' || curCh
>>                         == 'O' || curCh == 'U')
>>                         verb_count++;

>How about a switch?

>switch(curCh)
>{
>    /* multiple fall-throughs ahead */
>    case 'a':   case 'e':   case 'i':   case 'o':   case 'u':
>    case 'A':   case 'E':   case 'I':   case 'O':   case 'U':
>        verb_count++;
>}

Or

switch(tolower((unsigned char)curCh))
{
    case 'a':   case 'e':   case 'i':   case 'o':   case 'u':
        verb_count++;

Quote:
}

or even

if (curCh != '\0' && strchr("aeiouAEIOU", curCh) != NULL)
    verb_count++;

Stefan



Sun, 03 Aug 2003 19:55:24 GMT  
 finding chars in dynamic arrays

Quote:



> [...]

> >>                         verb_count++;

> >        verb_count++;
>     verb_count++;

I thought you guys were talking about vowels? :-)

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Sun, 03 Aug 2003 19:59:15 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. help with function to create dynamic two-dimensional char array

2. How to populate a dynamic array of strings (char*)

3. HELP! dynamic array of char*

4. Dynamic array source - email if you find useful

5. finding characters using dynamic arrays

6. Differences between char array[SIZE] and char *array

7. Converting char array of literals chars to escape chars

8. A char pointer (char *) vs. char array question

9. HELP -> dynamic array of char** in dynamic array of mystruct**

10. char array (array of array...) question

11. Dynamic arrays not really dynamic in managec C++?

12. find the error: char *c and char c[]

 

 
Powered by phpBB® Forum Software