I have written a simple program to save details about a maximum of 20 people using structures.

But when i run it, gets gives me error.......

Here is the code...

// Hihi.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	return 0;
}

#include<iostream>
#include<conio.h>

using namespace std;

struct address
{
	char name[20];
	int homeno;
	char city[20];
	char district[20];
	char state[20];
	long int pincode;
};
address addr[20];

int main()
{
	
	cout<<"Enter the number of people"<<endl;
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cout<<"Enter the name"<<endl;
		gets(addr[i].name);
		cout<<"Enter the house number"<<endl;
		cin>>addr[i].homeno;
		cout<<"Enter the city"<<endl;
		gets(addr[i].city);
		cout<<"Enter the district"<<endl;
		gets(addr[i].district);
		cout<<"Enter the state"<<endl;
		gets(addr[i].state);
		cout<<"Enter the pincode"<<endl;
		cin>>addr[i].pincode;
		cout<<endl<<endl;
	}

	cout<<endl;
	cout<<"Name\tHome No.\tCity\tDistrict\tState\tPincode"<<endl;
	for(int i=0;i<n;i++)
	{
		cout<<addr[i].name<<"\t"<<addr[i].homeno<<"\t"<<addr[i].city<<"\t"<<addr[i].district<<"\t"<<addr[i].state<<"\t"<<addr[i].pincode<<endl;
	}

	getch();
	return 0;
}

Now output:

Enter the number of people
1
Enter the name
Enter the house number
512
Enter the city
Enter the district
Blah Blah
Enter the state
Blah
Enter the pincode
69584



Name    Home No.        City    District        State   Pincode
        512             Blah Blah       Blah    69584

See i didn't get the chance to type, the name & city, Even though all others are OK. What could be causing this.......

#1) What zeroliken said
#2) It's C++. Why are you using the terrible and dangerous C function gets() ? Use getline() .
#3) cin is your problem. Reading an int leaves the NEWLINE in the input buffer. The solution has been explained many many times on this site -- do a search.

#1) What zeroliken said
#2) It's C++. Why are you using the terrible and dangerous C function gets() ? Use getline() .
#3) cin is your problem. Reading an int leaves the NEWLINE in the input buffer. The solution has been explained many many times on this site -- do a search.

Wow! I never knew it was a C function. Thanks for sharing that, now onward i will not use that.

Well by cin , you mean cin>>n ?

Thanks in advance

Wow! I never knew it was a C function. Thanks for sharing that, now onward i will not use that.

The fact that it's a function inherited from C is largely irrelevant. The important piece of information is that gets() is quite literally impossible to use safely. There's always a chance of buffer overflow, and you cannot avoid it. You can create the same problem using cin and C-style strings:

char buf[N];

cin >> buf; // No better than gets()

But at least here there's a fix for it:

char buf[N];

cin >> setw(N) >> buf; // All better

Of course, in C++ you should prefer to use the string class over C-style strings because they're easier to get right.

The fact that it's a function inherited from C is largely irrelevant. The important piece of information is that gets() is quite literally impossible to use safely. There's always a chance of buffer overflow, and you cannot avoid it. You can create the same problem using cin and C-style strings:

char buf[N];

cin >> buf; // No better than gets()

But at least here there's a fix for it:

char buf[N];

cin >> setw(N) >> buf; // All better

Of course, in C++ you should prefer to use the string class over C-style strings because they're easier to get right.

Ok! So that's how it is. Will keep it in mind next time. Thank U

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.