We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,916 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

gets() working erratically

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

4
Contributors
6
Replies
11 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Solved Threads: 16
Skill Endorsements: 3

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

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 14

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

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

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

Thank you very much! That explained a-lot.

PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Solved Threads: 16
Skill Endorsements: 3

#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

PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Solved Threads: 16
Skill Endorsements: 3

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.

Narue
Bad Cop
Team Colleague
15,460 posts since Sep 2004
Reputation Points: 6,483
Solved Threads: 1,407
Skill Endorsements: 54

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

PrimePackster
Posting Whiz in Training
252 posts since Nov 2011
Reputation Points: 21
Solved Threads: 16
Skill Endorsements: 3
Question Answered as of 1 Year Ago by Narue, WaltP and zeroliken

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1281 seconds using 2.97MB