i have to creat a program that verifes a valid user id before printing out the new email address. here are my errors:

c:\users\rena0514\documents\visual studio 2005\projects\user_id\user_id\verification.cpp(50) : error C2664: 'strncpy' : cannot convert parameter 2 from 'std::string' to 'const char *'
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\rena0514\documents\visual studio 2005\projects\user_id\user_id\verification.cpp(66) : error C2664: 'isalpha' : cannot convert parameter 1 from 'char *' to 'int'
1>        There is no context in which this conversion is possible

here is my code:

#include <iostream>
#include <string>

using namespace std;

bool user_ID_length(char*, int);
bool user_ID_alpha(char*);
bool user_ID_num(char*, int);
bool user_ID_special(char*, int);

int main()
{
	char userId[35]; 
	string email_address="@mvsu.edu";
	int length;

	cout<<"Create a user ID: ";
	cin.getline(userId,20);

	length = (long)strlen(userId);

	do
	{
		cout<<endl<<"Invalid. User Id must be at least 6 digits. Retry:";
		cin.getline(userId, 20);

		length = (long)strlen(userId);
	}while(user_ID_length==false);

	do
	{
		cout<<endl<<"Invalid. User Id must begin with a letter. Retry: ";
		cin.getline(userId, 20);
	}while(user_ID_alpha==false);


	do
	{
		cout<<endl<<"Invalid. User Id must contain 1 number. Retry: ";
		cin.getline(userId, 20);
	}while(user_ID_num==false);


	do
	{
		cout<<endl<<"Invalid. User Id cannot contain a special character. Retry: ";
		cin.getline(userId, 20);
	}while(user_ID_special==false);

	strncpy(userId, email_address, 9);

	cout<<"Your new email address is"<<userId;

	return 0;
}
bool user_ID_length(char* userId, int length)
{
	if(length<=5)
		return false;
	else 
		return true;
}

bool user_ID_alpha(char* userId)
{	
	if(!isalpha(userId))
		return false;
	else 
		return true;
}

bool user_ID_num(char* userId, int length)
{
	bool results = false;

	for(int i=0; i<length; i++)
	{
		if(isdigit(userId[i]))
			results = true;
		else 
			return false;
	}
	
	return results;

}

bool user_ID_special(char* userId, int length)
{
	bool results = false;

	for(int i=0; i<length; i++)
	{
		if(!ispunct(userId[i]))
			results = true;
		else
			return false;
	}

	return results;

}

Recommended Answers

All 8 Replies

first for your strncmp you need to make pass the string email_address as a char*. to do this you need to use the c_str() method

strncpy(userId, email_address.c_str(), 9);

second isalpha actually takes an int so you can only use a single character not a whole string so you would need to write a for loop to go through each element of the array and call isalpha on that.

third you are using do..while loops. i would use while loops instead because as it stands you will run the loop at lease once even if your input is correct. also i believe you are calling functions in you while part of the loop but there is no (). if

}while(user_ID_length==false);

// did you mean?

}while(user_ID_length(userId, length) == false);

if you have any more problems please post them and i would be happy to help

commented: Good Points, well done :) +0
commented: Agreed +4

thanks....i jave corrected that error and the program runs and i run into another error......
here's the problem in the run of the program and last line. why is it putting 1 as a special character:

Create a user ID: crand

Invalid. User Id must be at least 6 digits. Retry:1crand

Invalid. User Id must begin with a letter. Retry: crande

Invalid. User Id must contain 1 number. Retry: crand1

Invalid. User Id cannot contain a special character. Retry:

here's the corrected code:

#include <iostream>
#include <string>

using namespace std;

bool user_ID_length(char*, int);
bool user_ID_alpha(char*);
bool user_ID_num(char*, int);
bool user_ID_special(char*, int);

int main()
{
	char userId[35]; 
	string email_address="@mvsu.edu";
	int length;

	cout<<"Create a user ID: ";
	cin.getline(userId,20);

	length = (long)strlen(userId);

	do
	{
		cout<<endl<<"Invalid. User Id must be at least 6 digits. Retry:";
		cin.getline(userId, 20);

		length = (long)strlen(userId);
	}while(user_ID_length(userId, length)==false);

	do
	{
		cout<<endl<<"Invalid. User Id must begin with a letter. Retry: ";
		cin.getline(userId, 20);
	}while(user_ID_alpha(userId)==false);


	do
	{
		cout<<endl<<"Invalid. User Id must contain 1 number. Retry: ";
		cin.getline(userId, 20);
	}while(user_ID_num(userId, length)==false);


	do
	{
		cout<<endl<<"Invalid. User Id cannot contain a special character. Retry: ";
		cin.getline(userId, 20);
	}while(user_ID_special(userId, length)==false);

	strncpy_s(userId, email_address.c_str(), 9);

	cout<<"Your new email address is"<<userId;

	return 0;
}
bool user_ID_length(char* userId, int length)
{
	if(length<=5)
		return false;
	else 
		return true;
}

bool user_ID_alpha(char* userId)
{	
	if (isalpha(userId[1])) 
		return true;
	else
		return false;
}

bool user_ID_num(char* userId, int length)
{
	for(int i=0; i<length; i++)
	{
		if(isdigit(userId[i]))
			return true;
	}

	return false;
}

bool user_ID_special(char* userId, int length)
{
	bool results = false;

	for(int i=0; i<length; i++)
	{
		if(!ispunct(userId[i]))
			results = true;
		else
			return false;
	}

	return results;

}

change your do...while loops to just a while loop.

do
	{
		cout<<endl<<"Invalid. User Id must be at least 6 digits. Retry:";
		cin.getline(userId, 20);

		length = (long)strlen(userId);
	}while(user_ID_length(userId, length)==false);

// to

while(user_ID_length(userId, length)==false)
{
        cout<<endl<<"Invalid. User Id must be at least 6 digits. Retry:";
	cin.getline(userId, 20);

	length = (long)strlen(userId);
}

Program change:

#include <iostream>
#include <string>

using namespace std;

void user_ID_length(char*, int);
void user_ID_alpha(char*);
void user_ID_num(char*, int);
void user_ID_special(char*, int);

int main()
{
	char userId[35]; 
	string email_address="@mvsu.edu";
	int length;

	cout<<"Create a user ID: ";
	cin.getline(userId,20);

	length = (long)strlen(userId);

	user_ID_length(userId, length);

	user_ID_alpha(userId);

	user_ID_num(userId, length);

	user_ID_special(userId, length);

	strncpy_s(userId, email_address.c_str(), 9);

	cout<<"Your new email address is "<<userId<<endl;

	return 0;
}
void user_ID_length(char* userId, int length)
{
	if(length<=5)
	{
			do
		{
			cout<<endl<<"Invalid. User Id must be at least 6 digits. Retry: ";
			cin.getline(userId, 20);

			length = (long)strlen(userId);
		}while(length<=5);
	}
}

void user_ID_alpha(char* userId)
{	
	if (!isalpha(userId[1])) 
	{   do
		{
			cout<<endl<<"Invalid. User Id must begin with a letter. Retry: ";
			cin.getline(userId, 20);
		}while(!isalpha(userId[1]));
	}
}

void user_ID_num(char* userId, int length)
{
	bool results = false;

	for(int i=0; i<length; i++)
	{
		if(isdigit(userId[i]))
			results = true;
	}
	
	if(results=false)
	{
		do
		{
			cout<<endl<<"Invalid. User Id must contain 1 number. Retry: ";
			cin.getline(userId, 20);

			for(int i=0; i<length; i++)
			{
				if(isdigit(userId[i]))
					results = true;
			}

		}while(results==false);
	}
}

void user_ID_special(char* userId, int length)
{
	int count=0;

	for(int i=0; i<length; i++)
	{
		if(isalnum(userId[i]))
			count++;
	}

	if(count<length)
	{
		do
		{
			cout<<endl<<"Invalid. User Id cannot contain a special character. Retry: ";
			cin.getline(userId, 20);
			
			for(int i=0; i<length; i++)
			{
				if(isalnum(userId[i]))
					count++;
			}

		}while(count<length);
	}
}

The only problem i have now is that i cant seem to append email_address to userId.


this is printout from program when run:

Create a user ID: crande
Your new email address is @mvsu.edu
Press any key to continue . . .

strncpy_s(userId, email_address.c_str(), 9); copies your email domain into user ID. Whatever was there is overwritten.

By the way, just use the strncpy() function. strncpy_s() is non-standard, non-portable, not a good choice to use.

strncpy_s(userId, email_address.c_str(), 9); copies your email domain into user ID. Whatever was there is overwritten.

By the way, just use the strncpy() function. strncpy_s() is non-standard, non-portable, not a good choice to use.

how can i append the email domain to the user id? thats what my teacher asked us to do

strcat()

> By the way, just use the strncpy() function. strncpy_s() is non-standard, non-portable, not a good choice to use.
And the use of either in a C++ program (when the OP has already used std::string for something) is just bone-headed :)

EVERY char array and char* needs to be turned into a std::string (or a reference to one).

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.