my program doesnot execute gets(temp->name);line

#include <iostream.h>
#include <stdio.h>
using namespace std;
struct st{
int a;
char name[50];
};
void add(void){
st *temp=new st;
cout<<"Enter Your Name:";
gets(temp->name); //skips this line
}
int main(){
int n=1;
do{
cout<<"Enter 1 for name\n";
cout<<"Enter 2 for display\n";
cout<<"enter 3 to exit";
cin>>n;
if(n==1) add();
} while(n!=3);

return 0;
}
you need a cin.ignore() after cin>>n to consume the endline character.

Also, you need to forget gets() exists: use fgets() when doing C I/O and getline() when doing C++ I/O.
Or you can also use scanf
Topic archived. No new replies allowed.