Problem with gets() input statement

I have tried this code (in multiple programs) using dev c++ and code::Blocks but the first gets() command will never get executed when followed by a cin>>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int i;
    char c[10], d[10];
    cout<<"Enter int ";
    cin>>i;
    cout<<"Enter string ";
    gets(c); // this is where things act all possessed
    cout<<"Enter 2nd string ";
    gets(d);
    cout<<i<<endl<<c<<endl<<d;
    return 0;
}


Please help me out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    int i;
    char c[10], d[10];
    cout<<"Enter int ";
    cin >> i; cin.ignore();
    cout<<"Enter string ";
    gets(c); // this is where things act all possessed
    cout<<"Enter 2nd string ";
    gets(d);
    cout<<i<<endl<<c<<endl<<d;
    return 0;
}


Enter int 100
Enter string My
Enter 2nd string cat
100
My
cat
worked like a charm
thank you so much
but can you please explain what was wrong
and what exactly does the ignore() statement do?
After you use cin >> something;, you must use cin.ignore(); before you use gets();. Because when you press Enter, the "Enter" character still remains in the cin stream, and when the program calls gets() it will effectively catch the "Enter" character and skip the input. We should ensure there is no "Enter" character left in the cin stream before calling gets().
Topic archived. No new replies allowed.