LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem with test class pro, from C++ for dummies. (https://www.linuxquestions.org/questions/programming-9/problem-with-test-class-pro-from-c-for-dummies-384975/)

RHLinuxGUY 11-20-2005 05:37 PM

Problem with test class pro, from C++ for dummies.
 
I have copied the following piece of code from c++ for dummies...

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string.h>
using namespace std;

class NameDataSet
{
public:
char firstName[128];
char lastName [128];
int creditCard;
};

bool getData(NameDataSet& nds);
void displayData(NameDataSet& nds);

int main(int nNumberofArgs, char* pszArgs[])
{
const int MAX = 25;
NameDataSet nds[MAX];

std::cout << "Read name/credit card information\n"
<< "Enter 'exit' to quit"
<< endl;
int index = 0;
while (getData(nds[index]) && index < MAX)
{
index++;
}

std::cout << "\Entries:" << endl;
for (int i = 0; i < index; i++)
{
displayData(nds[i]);
}

system("PAUSE");
return 0;
}

bool getData(NameDataSet& nds)
{
std::cout << "\nEnter first name:";
cin >> nds.firstName;

if (stricmp(nds.firstName, "exit") == 0)
{
return false;
}

std::cout << "Enter last name:";
cin >> nds.lastName;

cout << "Enter credit card number:";
cin >> nds.creditCard;

return true;
}

void displayData(NameDataSet& nds)
{
std::cout << nds.firstName
<< " "
<< nds.lastName
<< "/ "
<< nds.creditCard
<< endl;
}

... now, when I compile it with "g++ class.cpp -o class", I get the following message.

george@linux:~> g++ class.cpp -o class
class.cpp: In function `bool getData(NameDataSet&)':
class.cpp:47: error: `stricmp' undeclared (first use this function)
class.cpp:47: error: (Each undeclared identifier is reported only once for each
function it appears in.)
george@linux:~>

... I've been looking at the code, and I don't see where "stricmp" is located at in the code. I don't know if it is a function, or something that should have been declared (or initialized?) earlier in the program. I'm pretty confident that this is not an error in the authors wrighting, but then I be ignorant to believe that "I know".

tuxdev 11-20-2005 05:49 PM

You need to include <cstring>, not <string.h>. Also, it is inconsistent in its usage of "using namespace std;". Either get rid of std:: everywhere, or put it in where nessecery and get rid of the using.

Mistro116@yahoo.com 11-20-2005 05:58 PM

Your gonna hit yourself when you see this:

It's a comparsion call...

It should probably be strgcmp(string 1, string 2);

Check line 47 :)

Mistro116

RHLinuxGUY 11-20-2005 06:25 PM

I'm not sure what to do. What I wrote down is exactly what is showed. Line 47 is what is says. And I changed string.h to cstring. Still can't seem to get where I need to. I'll probably go on some online resource to find out how classes can be used. I have an idea, but I need to see it in action to fully understand it.

FLLinux 11-20-2005 06:54 PM

It might be a typo. Try changing it from stricmp(string 1, string2) to strcmp(string1, string 2).

xhi 11-20-2005 08:03 PM

instead of
Code:

stricmp(nds.firstName, "exit")
use
Code:

strcasecmp(nds.firstName, "exit")
I know I have saw stricmp or strcmpi used, maybe its in a win32 lib?? not sure, but there is not a man page for it in my distro....

oh by the way this did compile when I changed that line.

RHLinuxGUY 11-21-2005 11:08 AM

Alright, ty, I will try that as soon as I get home. Thanx again for the posts guys.


All times are GMT -5. The time now is 08:20 AM.