5 Replies - 1147 Views - Last Post: 10 March 2009 - 08:00 AM Rate Topic: -----

#1 arshad115  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-March 09

Runtime Error - Dynamic Arrays

Post icon  Posted 08 March 2009 - 04:16 PM

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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Runtime Error - Dynamic Arrays

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Runtime Error - Dynamic Arrays

Posted 08 March 2009 - 10:23 PM

The compiler warning is that gets is dangerous because it can allow a buffer overrun, meaning that it can read in more characters than the amount of space you provided in your input buffer. If that happens, the input can overwrite other parts of memory and cause a crash or worse. So for standard C input the fgets function is safer. But why are you mixing C and C++ functions. Why don't you just stick with getline?

As for the errors -- what do you mean "skips the first attribute.."? Does it print "Please Enter Customer Name" and then immediately print "Please Enter Customer NIC Number:" ? If so, you probably had some other keyboard input request preceding that which left a newline in the stream. (Can't tell for sure without seeing more code.)

And then what -- it crashes? How is the p array defined? Did you allocate enough memory to accomodate the input?
Was This Post Helpful? 0
  • +
  • -

#3 arshad115  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 08-March 09

Re: Runtime Error - Dynamic Arrays

Posted 09 March 2009 - 11:38 PM

View Postr.stiltskin, on 8 Mar, 2009 - 09:23 PM, said:

The compiler warning is that gets is dangerous because it can allow a buffer overrun, meaning that it can read in more characters than the amount of space you provided in your input buffer. If that happens, the input can overwrite other parts of memory and cause a crash or worse. So for standard C input the fgets function is safer. But why are you mixing C and C++ functions. Why don't you just stick with getline?

As for the errors -- what do you mean "skips the first attribute.."? Does it print "Please Enter Customer Name" and then immediately print "Please Enter Customer NIC Number:" ? If so, you probably had some other keyboard input request preceding that which left a newline in the stream. (Can't tell for sure without seeing more code.)

And then what -- it crashes? How is the p array defined? Did you allocate enough memory to accomodate the input?


yes it does that exactly,prints "Please Enter Customer name" nd imediately after that prints "Please Enter NIC" and after that the program crashes.i have allocated enough memory.here is the code through which i initialize the array
 
char **p=new char*[n];
	cin.ignore();
	
	for(int i=0;i<n*8;i++)
	{

		p[i]=new char[20];
	}



and after that pass the p array to addcustomer() function.thanks
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Runtime Error - Dynamic Arrays

Posted 10 March 2009 - 07:27 AM

Not waiting for user input for Customer Name: There is probably an unused newline character in the input stream, left over from before. Most likely, just before your addcustomer function is called (in code you haven't posted here), you have code that inputs an int or char using cin; something like
cin >> x;
where "x" is an int or char. When you enter, say, "1" as the value for x on the keyboard, you press enter. That puts '1''\n' in the input stream, '1' is used for x, and the '\n' is left in the stream. Then the cin.getline() call in getcustomer simply takes the '\n' as its input, since getline() stops at the first newline it finds.

You can fix this by adding
cin.ignore()
after the previous "cin>>x" call that left the newline in the stream.

The crash is happening because you have allocated memory for an array n pointers, but then you are trying to load that array with pointers to (n*8) char arrays, i.e. if n =5 you have an array that can hold 5 pointers and you are using 40 pointers worth of memory.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5874
  • View blog
  • Posts: 12,754
  • Joined: 16-October 07

Re: Runtime Error - Dynamic Arrays

Posted 10 March 2009 - 07:50 AM

This could be a silly question, but why aren't you using string? You're already using C++ and STL; take the plunge.

Also, why are you using an array for your record, wouldn't a struct make more sense? e.g.
struct CustomerType {
	string name;
	string nic;
	string phone;
	string roomNumber;
	string dtEntry;
	string dtExit;
	string payments;
	string status;
};



You could have a function to ask for a string:
void load(string &value, const string& prompt) {
	cout << "Please Enter Customer "<< prompt << ": ";
	cin >> value;
	cout << endl;
	cin.ignore();
}



And use that function for each of your fields.

void load(CustomerType &rec) {
	load(rec.name, "Name");
	load(rec.nic, "NIC Number");
	load(rec.phone, "Telephone Number");
}

void print(const CustomerType &rec) {
	cout << "Customer" << endl;
	cout << "Name: " << rec.name << endl;
	cout << "NIC Number: " << rec.nic << endl;
	cout << "Telephone Number: " << rec.phone << endl;
}

//...

CustomerType cust;
load(cust);
print(cust);



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Runtime Error - Dynamic Arrays

Posted 10 March 2009 - 08:00 AM

Lamen terms of: baavgai
Technical definition of: baavgai

Quote

verb (used with object)
(lowercase) to regard or treat as a god; deify; idolize.


EDIT: If you're going to use C++ (as baavgai pointed out), take advantage of the STL (Standard Template Library)!

This post has been edited by Hyper: 10 March 2009 - 08:01 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1