0
#include<iostream>
using namespace std;
struct teacher 
{
    char name[20];
    int id;
    char grade[20];
    char qual[20];
    char heighest[20];  
};


int main()
{
    teacher t1,*ptr_t1;
    ptr_t1=&t1;
    cout<<"please Enter your Name \n";
    gets(ptr_t1->name);
    cout<<"please Enter your grade\n";
    gets(ptr_t1->grade);
    cout<<"please Enter your ID \n";
    cin>>ptr_t1->id;
    cout<<"please Enter your Qualification \n";
    gets(ptr_t1->qual);
    cout<<"please Enter your heighest \n";
    gets(ptr_t1->heighest); 
    system("pause");
    return 0;
}

this is my code gets(ptr_t1->qual) is not accepting any value and my program jumped to gets(ptr_t1->heighest) i am facing problem in it

  • 1
    better use `string`, what happens if the person has a name that's more than 19 characters long ? – user May 02 '20 at 21:35
  • 1
    This is basically [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction), only with `geline` replaced by `gets`. The root cause is the same. – Igor Tandetnik May 02 '20 at 21:35
  • 1
    `gets` is evil as it causes buffer overflow vulnerabilities. It's so evil that it has been removed from C++14, so you shouldn't be using it. – BessieTheCookie May 02 '20 at 21:42

1 Answers1

0

You shouldn't be mixing cin with gets. Instead, you can always use cin:

#include<iostream>
using namespace std;

struct teacher 
{
    char name[20];
    int id;
    char grade[20];
    char qual[20];
    char heighest[20];  
};


int main()
{
    teacher t1,*ptr_t1;
    ptr_t1=&t1;

    cout << "please Enter your Name \n";
    cin >> ptr_t1->name;
    cout << "please Enter your grade\n";
    cin >> ptr_t1->grade;
    cout << "please Enter your ID \n";
    cin >> ptr_t1->id;
    cout << "please Enter your Qualification \n";
    cin >> ptr_t1->qual;
    cout << "please Enter your heighest \n";
    cin >> ptr_t1->heighest; 

    system("pause");
    return 0;
}
Alejandro De Cicco
  • 1,128
  • 1
  • 15
  • 1
    It's ok to mix formatted and unformatted input if you are careful and using formatted input for things like names that may contain spaces isn't always a good idea. – Ted Lyngmo May 02 '20 at 21:55