-
Notifications
You must be signed in to change notification settings - Fork 0
/
16_1_geline.cpp
37 lines (33 loc) · 975 Bytes
/
16_1_geline.cpp
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
// if we use getline after >> operators then it will not take string and
// our program terminates , as it reads \n character .
/*
#include <iostream>
using namespace std;
int main() {
int x , y;
cout<<"enter 2 numbers \n";
cin>>x>>y;
string str;
cout<<"enter string \n";
getline(cin,str); // will not take string
cout <<x<<" "<<y<<" "<<str;
return 0;
}
*/
// therefore to solve above problem use cin.ignore()
// cin leaves the newline character in the stream. Adding cin.ignore() to the next line clears/ignores the newline from the stream.
//This is used mainly with combinations of cin and getline.
#include <iostream>
using namespace std;
int main()
{
int x, y;
cout << "enter 2 numbers \n";
cin >> x >> y;
string str;
cout << "enter string \n";
cin.ignore(); //
getline(cin, str); // it will take string now
cout << x << " " << y << " " << str;
return 0;
}