Problems using cin.getline() after using cin>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<conio.h>
#include<iostream.h>

class customer
{
	char city[21];
	char state[21];
	char name[51];
	char email[81];
	char address[201];

	public :
	
	void getdetails();
	void showdetails();
	
};

void customer::getdetails()
{
	cout<<" Enter Your Full Name (Max 50 chars): ";
	cin.getline(name,51);
	cout<<"\n\n Enter e-mail (Max 80 chars): ";
	cin.getline(email,81);
	cout<<"\n\n Enter You Address (Max 200 chars): ";
	cin.getline(address,201);
	cout<<"\n\n Enter City (Max 20 chars ): ";
	cin.getline(city,21);
	cout<<"\n\n Enter State (Max 20 Chars): ";
	cin.getline(state,21);

	
	cout<<"\n\n\t\t\t\t Thank You ";
	cout<<"\n\n\t\t\t PRESS ANY KEY TO CONTINUE ";
	_getch();
}

void customer::showdetails()
{
	cout<<"\nName  : "; puts(name);
	cout<<"E-mail  : "; puts(email);
	cout<<"Address : "; puts(address);
	cout<<"City    : "; puts(city);
	cout<<"State   : "; puts(state);
}


void main()
{
	void accept();
	void display();
	clrscr();
	
	int ch;
	customer obj;
	
	cout<<"1.Enter details "<<endl;
	cout<<"2.Exit "<<endl;
	cin>>ch;
	
	while(ch==1)
	{
		if(ch==1)
		{
			clrscr();		
			obj.getdetails();
			clrscr();
			obj.showdetails();
		}
		
		cout<<"1.Enter details "<<endl;
		cout<<"2.Exit "<<endl;
		cin>>ch;		
	}
	
	getch();

	
}	

void accept()
	{
		clrscr();
		customer obj;
		obj.getdetails();
		clrscr();
		obj.showdetails();
	}
	



After i compile and run this program and enter choice as 1 (to enter details) -
the program skips my name and directly goes to accepting my e-mail ! Its like i pressed enter when it asked me to enter my name even though i didn't.

The output is like :

Enter Your Full Name (Max 50 chars):

Enter e-mail (Max 80 chars): (cursor is here)

What's the problem ?
Last edited on
After the cin statement, use:
cin.ignore(std::numeric_limits<std::streamsize>::max, '\n');
to clear the stream of the carriage return, so the getline statement doesn't end right away.

cin.ignore(std::numeric_limits<std::streamsize>::max, '\n');

With that statement i am receiving two errors:

1. Cannot convert 'long(*)()' to 'long' in function main()
2. Type mismatch in parameter 'n' (wanted 'long', got 'long(*)() ) in function main


Do i need to include any header file for that statement ?
Sorry, yes.
#include <limits>.
Umm, It still doesn't work. Are you sure the statement can be compiled on borland compilers (version 5.5) ? I included <limits.h>
Last edited on
You're using an old compiler which doesnt follow the ISO standard.
I'm not sure about the ignore thing but cin.sync() *should* do the trick.
Also, numeric_limits <> ::max() is a functor (an object), so you have to have those parentheses at the end.

Borland 5.5 is pre-ISO standard, and does not have the proper limits file.
Thanks a lot duos. It worked. All i needed to do was to add the paranthesis after 'max' . Thx to QWERTYman too.
Topic archived. No new replies allowed.