why is a function-definition isn’t allowed before “{”?

We we’re tasked to write a program that

  • Display the inputted strings,
  • Count the number of characters in the string(s),
  • Count the number of vowels in the string(s),
  • Count the number of consonants in the string(s),
  • Convert the string(s) to uppercase,
  • Convert the string(s) to lowercase,
  • Compare the two strings and Combine the two strings

Now, I’m facing a problem where everytime I try to run a program it gets an error that says function-definition is not allowed here before { token and error expected } at end of input.

Code below:

#include <cstring>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;

void Display_Inputted_Strings()
{
    string str1,str2;
    
    cin >> str1;
    cin >> str2; 
    cout << str1 << endl;
    cout << str2 << endl;
    
    return;
}

void The_Number_Of_Character()
{
    char str1[10];
    cout << "Input you're First String: ";
    cin >> str1;
    char str2[10];
    cout << "Input you're Second String: ";
    cin >> str2;
    cout << "Lenght of string 1: " << strlen(str1) << endl; 
    cout << "Lenght of string 2: " << strlen(str2) << endl; 
}

void The_Number_Of_Vowels()
{       
    bool isVowel(char ch)
    {
        {   
            ch = toupper(ch);
            return (ch=='A' || ch=='E' || ch=='I' ||
                            ch=='O' || ch=='U');
        }
    }
}

int countVowels(int str)
{
    int isVowel;
        int count = 0;
        for (int i=0; i<str.length(); i++)
            if (isVowel(str[i]))
                ++count;
        return count;
    
    int str;
    cout << "Enter a string: ";
    cin >> str;
    
    cout << "There are " << countVowels(str) << " Vowels in this string" << endl;
    return 0;
}

void The_Number_Of_Consonants()
{
        bool isConsonant(char ch)
    {
        ch = toupper(ch);
 
        return !(ch == 'A' || ch == 'E' ||
                ch == 'I' || ch == 'O' ||
                ch == 'U') && ch >= 65 && ch <= 90;
    }
 
    int totalConsonants(string str)
    {
        int count = 0;
        for (int i = 0; i < str.length(); i++)
 
       
            if (isConsonant(str[i]))
                ++count;
        return count;
     string str;
    cout << "Enter a string: ";
    cin >> str;
    
    cout << "There are a total of " << totalConsonants(str) << " Consonant";
    
    }
}

void Convert_Strings_To_Uppercase()
{
    void convert(string & s1)
{
        for(int i = 0; i<s1.length(); i++)
        {
            s1[i] = toupper(s1[i]);
        }
    }
        {
        string s1;
        cout << "Enter a string: " << endl;
        getline(cin, s1);
        convert(s1);
        cout << s1 << endl;
        return 0;
        }
    }

void Convert_Strings_To_Lowercase()
{
    void convert(string & s1)
{
        for(int i = 0; i<s1.length(); i++)
        {
            s1[i] = tolower(s1[i]);
        }
    }

    {
        string s1;
        cout << "Enter a string: " << endl;
        getline(cin, s1);
        convert(s1);
        cout << s1 << endl;
        return 0;
    }
}

void Compare_Two_Strings()
{
    {
    char str1[10] = "String";
    char str2[10] = "Strings";
    
    int result;
    result=strcmp(str1,str2);
    if(result==0)
    cout << "String are equal" << endl;
    else
    cout << "String are not equal" << endl;
    
    return 0;
}

}

void Combine_Two_Strings()
{
    {
    string s1, s2, result;
    
    cout << "Enter string s1: ";
    getline(cin,s1);
    cout << "Enter string s2: ";
    getline(cin,s2);
    
    result = s1+s2;
    
    cout << result << endl;
    
    return 0;
}
}

void Input_String()
{
    string str1;
        cout << "Input a String: ";
        cin >> str1; 
        cout << "The String You Inputted: " << str1;
}

void Exit_Program()
{
    cout << "Thank You.n";
}

int main()
{
    int str1,str2;
      {
      cout << "Input Two Strings: ";
      getline(cin, str1);
      getline(cin, str2)
      }
      
      
      int selected
  do
    {
     std::cout << "A.(0) Display Inputted Stringsn";
     std::cout << "B.(1) Count the number of characters in the string(s)n";
     std::cout << "C.(2) Count the number of vowels in the string(s)n";
     std::cout << "D.(3) Count the number of consonants in the string(s)n";
     std::cout << "E.(4) Convert the string(s) to uppercasen";
     std::cout << "F.(5) Convert the string(s) to lowercasen";
     std::cout << "G.(6) Compare the two stringsn";
     std::cout << "H.(7) Combine the two stringsn";
     std::cout << "I.(8) Input anothern";
     std::cout << "J.(9) Exit Programn";
     
     std::cin >> selected;
     
       switch (selected)
       {
        case 0:
           Display_Inputted_Strings();
             break;
        case 1:
            The_Number_Of_Character();
             break;
        case 2:
            The_Number_Of_Vowels();
             break;
        case 3:
            The_Number_Of_Consonants();
            break;
        case 4:
            Convert_Strings_To_Uppercase();
            break;
        case 5:
            Convert_Strings_To_Lowercase();
            break;
        case 6:
            Compare_Two_Strings();
            break;
        case 7:
            Combine_Two_Strings();
            break;
        case 8:
            Input_String();
            break;
        case 9:
            Exit_Program();
            break;
         default:
            std::cout << "You have entered an invalid optionn";
            }
        }  while (selected != 9);
    }
}

Answer

You probably come to the world of C++ from the world of another language like Python. It is not allowed to define a nested function within another function in C++. However you may define a lambda that may have the same semantics. For example:

void Convert_Strings_To_Lowercase()
{
    auto convert = [](string & s1)
    {
        for(int i = 0; i<s1.length(); i++)
        {
            s1[i] = tolower(s1[i]);
        }
    };

    {
        string s1;
        cout << "Enter a string: " << endl;
        getline(cin, s1);
        convert(s1);
        cout << s1 << endl;
    }
}

P.S. I’ve just converted your function into something compilable, without checking whether the code has any meaning. For example, I removed the return 0; statement as your function has the void return type.