0

i m getting a runtime error and the program skips the first attribut i.e name,and gives a runtime error at the second one.compiler gives some warning about gets".\Assignment 3_Question 4.cpp(118) : warning C4996: 'gets': This function or variable may be unsafe. Consider using gets_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
"

my code is :

char *addcustomer(char **p,int n)
{
	

	cout << " ------- Add Customer Record --------" << endl << endl;

	

	
	
 for(int i=0;i<n;i++)
 {
	cout << "Please Enter Customer Name: " ;
	
	cin.getline(p[i],20,'\n');
	
	cout << endl;
	
	cout << "Please Enter Customer NIC Number: " << endl;

	gets(p[i+1]);
	
	//cout << p[i+1]<< endl;

	cout << "Please Enter Customer Telephone: Number " << endl;
	
	gets(p[i+2]);
	
	//cout << p[i+2]<< endl;

	cout << "Please Enter Customer Room Number: " << endl;
	
	gets(p[i+3]);
	
	//cout << p[i+3]<< endl;

	cout << "Please Enter Customer Entry Date and Time: " << endl;

	gets(p[i+4]);
	
	//cout << p[i+4]<< endl;

	cout << "Please Enter Customer Exit Date and Time: " << endl;
	
	gets(p[i+5]);
	
	//cout << p[i+5]<< endl;

	cout << "Please Enter Customer Payments: " << endl;

	gets(p[i+6]);
	
	//cout << p[i+6]<< endl;

	cout << "Please Enter Customer Status: " << endl;

	gets(p[i+7]);
	
	//cout << p[i+7]<< endl;
	
 }



return 0;

}

int _tmain()

{
	int choice,n;

	cout << "Please Enter the number of records you want to Enter: " << endl;

	cin>>n;

	char **p=new char*[n];
	cin.ignore();
	
	for(int i=0;i<n;i++)
	{
		p[i]=new char[20];
	}

	 cin.ignore();
.
.
.
.
addcustomer(p,n);

.
.
.
return 0;
}

thanks in advance!

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by vmanes
0

The warning about gets( ) is just a warning that it's going to be deleted in future verisons of the compiler because it is an unsafe function - it doesn't guarantee not to overflow the destination array.

Why are you using it? Why not continue using getline( ) ?

To get rid of the warning, put the following before your #include statements.
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE

As to your actual code, you've got a big logic flaw. You ask the user how many records, and you allocate that many strings. But a record consists of 8 strings. Your input function starts at string 0 and fills up to string 7 for customer #1. When you start on the next customer, you store his name in string 1, overwriting the NIC number of customer #1, and then overwriting all the rest of customer #1 with the succeeding data.

If you intend to store your data as a sequence of 8 strings per customer, then you need to allocate 8*n arrays in main( ). In any function that accesses a customer, you need to offset by 8* the customer number. That is, customer #3 (who you would normally think starts at index [2]) would start at index 2*8, or 16. Rows 0-7 are customer 1, rows 8-15 are customer #2, rows 16-23 are customer #3, etc.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.