1,105,546 Community Members

gets() working erratically

Member Avatar
PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
0
 

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.......

Member Avatar
zeroliken
Nearly a Posting Virtuoso
1,222 posts since Nov 2011
Reputation Points: 79 [?]
Q&As Helped to Solve: 216 [?]
Skill Endorsements: 15 [?]
 
1
 

Don't use gets()
-->Article explaining why not
Try to use fgets() instead

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
1
 

#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.

Member Avatar
PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
0
 

Don't use gets()
-->Article explaining why not
Try to use fgets() instead

Thank you very much! That explained a-lot.

Member Avatar
PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
0
 

#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

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
2
 

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.

Member Avatar
PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 10 [?]
Q&As Helped to Solve: 17 [?]
Skill Endorsements: 3 [?]
 
0
 

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

Question Answered as of 2 Years Ago by Narue, WaltP and zeroliken
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: