Home > Back-end >  My variable is not declared in the function. c language
My variable is not declared in the function. c language

Time:04-30

I'm making a code in c language to search some names in a string. The task is that i can search the name even when it has lowercase and uppercase.

Example:

How many names do you want to input? 3

Input Names:

John Harrow

Dean 

Michael Jackson

Input the name you searching : john harrOW

john harrOW found in index-0

I already make the code but there is some errors:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void lowercase_words(str)
{
    int i;
    for(i=0;i<=strlen(str);i  ){
      if(str[i]>=65&&str[i]<=90)
         str[i]=str[i] 32;
   }
}

int main()
{
    int a;
    char b[255];
    int c = 0;
    char str[];
    
    printf("How many names do you want to input? ");
    scanf("%d", &a);
    
    char ignore;
    scanf("%c", &ignore);
    
    char names [a][255];
    
    int j;
    for(j = 0; j < a; j  ){
        scanf("%[^\n]", names[j]);
        lowercase(names[j]);
        scanf("%c", &ignore);
    }
    
    printf("Input the name you searching: ");
    scanf("%[^\n]", b)
    lowercase(b[])
    
    
    int i;
    for(i = 0; i < a; i  )
    {
        if(strcmp(b, names[i]) == 0)
        {
            c = 1;
            printf("%[^\n] found in index-%d", b, i);
            break;
        }
    }
    
    if (c == 0)
    {
        printf("we cant found %[^\n]", b);
    }
    
    return 0;
}

and the Errors are:

  1. Variable or field 'lowercase_words' declared void
  2. 'str' was not declared in this scope

Thank you.

CodePudding user response:

You wrote:

void lowercase_words(str)

You must provide a type for each argument to the function:

void lowercase_words(char* str)  /* Added type "pointer-to-characters" */

I would recommend changing the function to:

void lowercase_words(char* str)
{
    for(int i=0;i<strlen(str);i  ){
      str[i] = tolower(str[i]);
    }
}

CodePudding user response:

I'll try to help you learn.

Regarding your errors, I'll reprint your original code here with line numbers so its easier to address:

  1 #include <stdio.h>
  2 #include <ctype.h>
  3 #include <string.h>
  4 
  5 void lowercase_words(str)
  6 {
  7     int i;
  8     for(i=0;i<=strlen(str);i  ){
  9       if(str[i]>=65&&str[i]<=90)
 10          str[i]=str[i] 32;
 11    }
 12 }
 13 
 14 int main()
 15 {
 16     int a;
 17     char b[255];
 18     int c = 0;
 19     char str[];
 20     
 21     printf("How many names do you want to input? ");
 22     scanf("%d", &a);
 23     
 24     char ignore;
 25     scanf("%c", &ignore);
 26     
 27     char names [a][255];
 28     
 29     int j;
 30     for(j = 0; j < a; j  ){
 31         scanf("%[^\n]", names[j]);
 32         lowercase(names[j]);
 33         scanf("%c", &ignore);
 34     }
 35     
 36     printf("Input the name you searching: ");
 37     scanf("%[^\n]", b)
 38     lowercase(b[])
 39     
 40     
 41     int i;
 42     for(i = 0; i < a; i  )
 43     {
 44         if(strcmp(b, names[i]) == 0)
 45         {
 46             c = 1;
 47             printf("%[^\n] found in index-%d", b, i);
 48             break;
 49         }
 50     }
 51     
 52     if (c == 0)
 53     {
 54         printf("we cant found %[^\n]", b);
 55     }
 56     
 57     return 0;
 58 }

And your original stated errors:

Variable or field 'lowercase_words' declared void

and

'str' was not declared in this scope

This looks like it was an error prior to the code shown. Perhaps your compiler generated this error on the code in line number five as is. Never the less, the problem on line 5 is that you are specifying a function named lowercase_words and you are saying this particular function has a parameter supplied by the caller named str. However a function parameter requires a variable type and name. Consider the type to specify a box that holds a type of variable and the name is a name for the box. In this case, you want a variable of name str which is of type char array or string.

So, rather than:

void lowercase_words(str)

Do

void lowercase_words(char str[255])

Note, I am using the char array to be 255 characters to since you have line 17 which has b to be 255 and names to be 255.

As an aside, its preferred to use a constant rather a hard coded number. This is what the other posters are saying when they say 'magic number'. So, you could do something like this:

const int mylen = 255;
void lowercase_words(char str[mylen])

However this will not work for you, because in the function, you are modifying the value of str. ie. on line 10, you do str[i]=str[i] 32; Functions by default pass the function parameters by value. If you want to modify a functions parameters value you need to pass by reference rather pass by value. In that case you need to use a pointer.

void lowercase_words(char *pstr)

In this case, I am changing your variable name to use a p prefix to specify its a pointer. This way, the function parameter performs and input/output variable.

The next fix is on lines 37 and 38 where you need to include a closing semi colon. The b[] variable used as a parameter is also modified to be simply b. Also on line 19, the str char array needs a size. For simplicity I will use your 255 size again.

On lines 32 and 38, you are using a function named lowercase. I believe you intended to use lowercase_words.

So, that gets rid of your compile errors. However, you have some logic errors which prevent this code from running correctly. Before we address that, lets look at the modifications for now with line numbers again.

  1 #include <stdio.h>
  2 #include <ctype.h>
  3 #include <string.h>
  4 
  5 void lowercase_words(char *pstr)
  6 {
  7     int i;
  8     for(i=0;i<=strlen(pstr);i  ){
  9       if(pstr[i]>=65&&pstr[i]<=90)
 10          pstr[i]=pstr[i] 32;
 11    }
 12 }
 13 
 14 int main()
 15 {
 16     int a;
 17     char b[255];
 18     int c = 0;
 19     char str[255];
 20     
 21     printf("How many names do you want to input? ");
 22     scanf("%d", &a);
 23     
 24     char ignore;
 25     scanf("%c", &ignore);
 26     
 27     char names [a][255];
 28     
 29     int j;
 30     for(j = 0; j < a; j  ){
 31         scanf("%[^\n]", names[j]);
 32         lowercase_words(names[j]);
 33         scanf("%c", &ignore);
 34     }
 35     
 36     printf("Input the name you searching: ");
 37     scanf("%[^\n]", b);
 38     lowercase_words(b);
 39     
 40     
 41     int i;
 42     for(i = 0; i < a; i  )
 43     {
 44         if(strcmp(b, names[i]) == 0)
 45         {
 46             c = 1;
 47             printf("%[^\n] found in index-%d", b, i);
 48             break;
 49         }
 50     }
 51     
 52     if (c == 0)
 53     {
 54         printf("we cant found %[^\n]", b);
 55     }
 56     
 57     return 0;
 58 }

So, I think this is what you want the output to look like:

myhost:~$ gcc so9229.c 
myhost:~$ ./a.out 
How many names do you want to input? 2

You specified 2 names
 JOHN
DAVIS
names[0] = john
names[1] = davis
Input the name you searching: JOHN

You wish to search for john
 Found at index:0, the name john
myhost:~$ ./a.out 
How many names do you want to input? 2

You specified 2 names
 JOHN
DAVIS
names[0] = john
names[1] = davis
Input the name you searching: TOM

You wish to search for tom
 Cant find tommyhost:~$ 

With that said, here is the final result of modifying your code. I hope this helps and good luck as you continue to learn. That's awesome and I know you will do well!

#include <stdio.h>
#include <ctype.h>
#include <string.h>

void lowercase_words(char *pstr)
{
    int i;
    for(i=0;i<=strlen(pstr);i  ){
      if(pstr[i]>=65&&pstr[i]<=90)
         pstr[i]=pstr[i] 32;
   }
}

int main()
{
    int a;
    char b[255];
    int c = 0;
    char str[255];
    
    printf("How many names do you want to input? ");
    scanf("%d", &a);
    // dump the number of names
    printf("\nYou specified %d names\n ", a);
    
    char ignore;
    scanf("%c", &ignore);
    
    char names [a][255];
    
    int j;
    for(j = 0; j < a; j  ){
        scanf("%[^\n]", names[j]);
        lowercase_words(names[j]);
        scanf("%c", &ignore);
    }

    // dump the list of names
    for(j = 0; j < a; j  ){
      printf("names[%d] = %s\n",j, names[j]);
    }
    
    printf("Input the name you searching: ");
    scanf("%[^\n]", b);
    // dump the name you are searching for
    lowercase_words(b);
    printf("\nYou wish to search for %s\n ", b);
    
    
    int i;
    for(i = 0; i < a; i  )
    {
        if(strcmp(b, names[i]) == 0)
        {
            c = 1;
            printf("Found at index:%d, the name %s\n", i, b);
            break;
        }
    }
    
    if (c == 0)
    {
        printf("Cant find %s", b);
    }
    
    return 0;
}
  •  Tags:  
  • c
  • Related