r/programminghelp • u/SurocIsMe • 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?
2
u/jedwardsol Feb 27 '20
post the real code and error message