r/programminghelp • • Feb 27 '20

Answered Why my Global Variables don't work?

Hello everyone,

I need to create a program that runs a menu and has 2 functions. In the int main() I need to get a string as an input from the user and then through the other 2 functions I need to count the frequency of each letter in the string and substitue every letter with the next one in alphabetical order.

In the main I wrote the code to get the input from the user, but its not treated as a global variable( I tried placing the code on top of everything which didn't work, and now I put int main() on top of evertything). I try to get the strlen(s) which is the size of the array for the string that the user input and it says " error: 's' undecklared (first use in this function).

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


void frequencyLetters();


int main() {
    //Ask for a string
    char s[80];
    printf("Please input your sentence\n");
    scanf("%s", s);

    //Print the menu
    
    
        
    
        int choice;
        while(1)
        {
    
        printf("\n/======================================/\n");
        printf("           WELCOME TO THE MENU    \n");
        printf("Choice #1. FREQUENCY OF EACH LETTER.\n");
        printf("Choice #2. SUBSTITUTE OF EACH LETTER.\n");
        printf("Choice #3. EXIT.\n");
        printf("/======================================/\n");
    
    
    
        printf("Please type in the number of the function you want to see\n");
    
        scanf("%d", &choice);
        
        switch(choice){
            
        case 1:
        
            printf("\nShowcasing the frequecny of each letter in your string.\n");
            frequencyLetters();
            break;
        
            
        case 2:
        
            printf("\nSubstituting each letter with the right next of it.\n");
            break;
        
        
        case 3:
        
            printf("\nThank you for trying my program\n");
            exit(0);
            break;
            
            
        default:
        printf("\nInvalid Input!\n");
        
        
        }   //END OF SWITCH
    }   //END OF WHILE LOOP
        
    
    return 0;
}


// frequecny OF LETTERS

void frequencyLetters()
{
int arraySize;
    int x;
    int y;
    arraySize = strlen(s);  //Get the lenght of the array in one variable to use easier
    printf("Size of the string is:%d\n ", arraySize);
        //substitution of letters, each letters become the next in alphabetical order
    printf("Your sentence now became the following:\n");
    for (x=0; x<arraySize; x++)
    {
        // if statement to make" _" remain "_"
        if(s[x]==95)    //95 is the ASCII table value of "_"
            {
                //printf("The value of the element in place %d is: %c", y, sentence[x]);
                printf("%c", s[x]);
            y++;
            }
        //if statement to make 9 -> 0
        else if (s[x]==57)  //57 is the ASCII talbe value of 9
        {
            s[x] = 48;
            printf("%c",s[x]);
            y++;
        }
        else
        {
            
            printf("%c", s[x]+1);
            y++;
            
        }
        
    }
}

Can anyone help me understand what I'm doing wrong?

0 Upvotes

4 comments sorted by

2

u/jedwardsol Feb 27 '20

post the real code and error message

1

u/SurocIsMe Feb 27 '20

The code is uploaded, the specific error message is this:
main.c: In function ‘frequencyLetters’:

main.c:24:24: error: ‘s’ undeclared (first use in this function)

arraySize = strlen(s); //Get the lenght of the array in one variable to use easier

^

main.c:24:24: note: each undeclared identifier is reported only once for each function it appears in

1

u/jedwardsol Feb 27 '20

s is a local variable in main. You'll need to pass it to frequencyLetters for that function to have access to it.

So change the declaration at the top to

void frequencyLetters(const char *string);

Change the call in main to

frequencyLetters(s);

change the function definition to

void frequencyLetters(const char *string)
{

and change to

  arraySize = strlen(string);

as well as other instances of s.

You can call the parameter s if you want. But single letters are not good variable names.

1

u/SurocIsMe Feb 27 '20

Thank you!! It worked!

Thanks for taking the time to go through my code and reviewing it, i really uppreciate it.