Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifier not found error on function call

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.

like image 686
KRB Avatar asked Nov 30 '11 16:11

KRB


People also ask

What is identifier not found?

Then functionOne calls functionTwo but the name functionTwo was not yet declared. You shall place declarations of functionTwo and functionThree before using these names. You need to declare function prototypes.

What does undeclared identifier mean in C++?

The identifier is undeclared: In any programming language, all variables have to be declared before they are used. If you try to use the name of a such that hasn't been declared yet, an “undeclared identifier” compile-error will occur. Example: #include <stdio.h> int main()


2 Answers

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.

like image 192
Alex F Avatar answered Sep 20 '22 00:09

Alex F


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 doesn't 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. Don't 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.

like image 40
Michael Kristofik Avatar answered Sep 19 '22 00:09

Michael Kristofik