c++ – Identifier not found error on function call

c++ – Identifier not found error on function call

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.

Unlike other languages you may be used to, everything in C++ has to be declared before it can be used. The compiler will read your source file from top to bottom, so when it gets to the call to swapCase, it doesnt know what it is so you get an error. You can declare your function ahead of main with a line like this:

void swapCase(char *name);

or you can simply move the entirety of that function ahead of main in the file. Dont worry about having the seemingly most important function (main) at the bottom of the file. It is very common in C or C++ to do that.

c++ – Identifier not found error on function call

At the time the compiler encounters the call to swapCase in main(), it does not know about the function swapCase, so it reports an error. You can either move the definition of swapCase above main, or declare swap case above main:

void swapCase(char* name);

Also, the 32 in swapCase causes the reader to pause and wonder. The comment helps! In this context, it would add clarity to write

if (A <= name[i] && name[i] <= Z)
    name[i] += a - A;
else if (a <= name[i] && name[i] <= z)
    name[i] += A - a;

The construction in my if-tests is a matter of personal style. Yours were just fine. The main thing is the way to modify name[i] — using the difference in a vs. A makes it more obvious what is going on, and nobody has to wonder if the 32 is actually correct.

Good luck learning!

Leave a Reply

Your email address will not be published.