Identifier not found error on function call

KRB picture KRB · Nov 30, 2011 · Viewed 197.1k times · Source

I have a program here where I invert the case of an entered string. This is the code in my .cpp file and I am using Visual Studio C++ IDE. I am not sure what I need in a header file or if I need one to make this work.

Error with my function call swapCase. Main does not see swapCase for some reason that I'm not sure of.

#include <cctype>
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    char name[30];
    cout<<"Enter a name: ";
    cin.getline(name, 30);
    swapCase(name);
    cout<<"Changed case is: "<< name <<endl;
    _getch();
    return 0;
}

void swapCase (char* name)
{
    for(int i=0;name[i];i++)
    {
        if ( name[i] >= 'A' && name[i] <= 'Z' )
            name[i] += 32; //changing upper to lower
        else if( name[i] >= 'a' && name[i] <= 'z')
            name[i] -= 32; //changing lower to upper
    }
}

Any other tips for syntax or semantics is appreciated.

Answer

Alex F picture Alex F · Nov 30, 2011

Add this line before main function:

void swapCase (char* name);

int main()
{
   ...
   swapCase(name);    // swapCase prototype should be known at this point
   ...
}

This is called forward declaration: compiler needs to know function prototype when function call is compiled.