Hi, I've recently started learning C++ using 'C++ Demystified' and I'm trying to make this function work. I've done the pointers chapter and I saw a little function that uses <cctype>, and I'm trying to get it to work. Heres the code:

#include <iostream>
#include <cctype>
using namespace std;
bool isValidPassWord(char*);

int main(void)
{
	char* name;
	bool test;

	cout << "Enter name: ";
	cin.getline(name, 3);
	test = isValidPassWord(&name);
	if (test != true)
		cout << "Invalid password entered" << endl;
	else
		cout << "Your password is " << *name << endl;
return 0;
}

bool isValidPassWord(char* pw)
{
   if (!isupper(pw[0]))
      return false;
   if (!isdigit(pw[1]))
      return false;
   if (!islower(pw[2]))
      return false; 
   return true;
}

The error I get is error C2664: 'isValidPassWord' : cannot convert parameter 1 from 'char ** ' to 'char *' Types pointed to are unrelated

I have a feeling I'm missing something obvious, but really can't figure it out. Any help is appreciated.

Thanks.

try

test = isValidPassWord(name);

Wait a minute to check this in my compiler!!!

#include <iostream>
#include <cctype>
using namespace std;
bool isValidPassWord(char*);
 
int main(void)
{
	char name[4];
	bool test;
 
	cout << "Enter name: ";
	cin.getline(name, 4);
	test = isValidPassWord(name);
	if (test != true)
		cout << "Invalid password entered" << endl;
	else
		cout << "Your password is " << name << endl;
return 0;
}
 
bool isValidPassWord(char* pw)
{
   if (!isupper(pw[0]))
      return false;
   if (!isdigit(pw[1]))
      return false;
   if (!islower(pw[2]))
      return false; 
   return true;
}

This is working. Send PM for more help.

Review this code. I've changed my post not far from now!

One more. Look through th definition of

cin.getline(char*, int n);

if you typed n e.g. 5, 4 (n-1) symbols will be read!!!!

Thanks zhelih, the code is working now!

And I'm not sure what you mean with the latest post I'm afraid. :S

Thanks zhelih, the code is working now!

And I'm not sure what you mean with the latest post I'm afraid. :S

Let me explain this.

// read characters from stdin until a newline
// or 49 characters have been read
cin.getline(p,50);

You see that you gave the function argument 50, but only 49 sybmols are read.

In your program you entered:

getline(name, 3);

This is reading 2 symbols, but you need 3!
So you MUST type:

getline(name, 4);

.

Oh, I see now. I thought that when you typed 'int n' you literally meant for that to be typed as the code. I thought that by typing 3, it included 0 as one of the numbers so 3 was enough. Thanks for clearing this up.

I'm off to work now, so the next time I'll be checking this thread is in a few hours. Thanks again zhelih. :)

cin.getline(p,50);

The reason why it does read only 49 characters because the computer need the last character to be a NULL character.

cin.getline(p,50);

The reason why it does read only 49 characters because the computer need the last character to be a NULL character.

I was counting 1 to 49, as well as 0 as an actual usable element, therefore making 50. Thanks for clearing this up guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.