Runtime Error - Dynamic Arrays
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 :
Code:
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!
Re: Runtime Error - Dynamic Arrays
I'm not sure exactly what you are trying to do ... but in addcustomer,
look at the for loop, when "i = n-1"
1) you do a getline() using p[i] ... ok
2) you do a gets() in p[i+1] , p[i+2] , ... , p[i+7] ... these are
past the bounds of the array.
3) Why are you mixing getline() and gets() ?
Re: Runtime Error - Dynamic Arrays
i just used both of them to run my code,but none of them works,both give error,i just wanted to show that i have tried both of them!
Re: Runtime Error - Dynamic Arrays
Let's say that you're on the iteration of the loop when i == n-1. At that point, where are you trying to store the telephone number?
Re: Runtime Error - Dynamic Arrays
i am a bit confused in your for loop..... what will happen when the loops runs after entering 1st record.... i think it will overlap your previous data(record).....
though m not sure whether i am correct or not....