I was going through Lesson 9 of the tutorial here when it wouldn't compile. I thought I may have typed it in wrong, so I cut and pasted it and tried again. No dice.
Here is the error I'm receiving:
[sugapablo@localhost c++]$ g++ -o string2copy string2copy.cc
string2copy.cc: In function `int main()':
string2copy.cc:19: `strcmpi' undeclared (first use this function)
string2copy.cc:19: (Each undeclared identifier is reported only once for each
function it appears in.)
string2copy.cc:37: `strupr' undeclared (first use this function)
string2copy.cc:43: `strlwr' undeclared (first use this function)
[sugapablo@localhost c++]$
I'm really new at C++, but it I would guess perhaps it isn't including string.h? (I'm using the g++ compiler on Mandrake 8.2)
Code:
#include <iostream.h> //For cout
#include <string.h> //For many of the string functions
int main()
{
char name[50]; //Declare variables
char lastname[50]; //This could have been declared on the last line...
cout<<"Please enter your name: "; //Tell the user what to do
cin.getline(name, 50, '\n'); //Use gets to input strings with spaces or
//just to get strings after the user presses enter
if(!strcmpi("Alexander", name)) //The ! means not, strcmpi returns 0 for
{ //equal strings
cout<<"That's my name too."<<endl; //Tell the user if its my name
}
else //else is used to keep it from always
{ //outputting this line
cout<<"That's not my name.";
}
cout<<"What is your name in uppercase..."<<endl;
strupr(name); //strupr converts the string to uppercase
cout<<name<<endl;
cout<<"And, your name in lowercase..."<<endl;
strlwr(name); //strlwr converts the string to lowercase
cout<<name<<endl;
cout<<"Your name is "<<strlen(name)<<" letters long"<<endl; //strlen returns
//the length of the string
cout<<"Enter your last name:";
cin.getline(lastname, 50, '\n'); //lastname is also a string
strcat(name, " "); //We want to space the two names apart
strcat(name, lastname); //Now we put them together, we a space in
//the middle
cout<<"Your full name is "<<name; //Outputting it all...
return 0;
}