gets(name) does not Invoke second time.

Dear Sir,

I always get problem in getting strings by gets(name). It does not invoke second time. can you please check below code.


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
 //Static Data Member.
#include <iostream>
#include <string>
using namespace std;

class Student{
      private:
              static int r;
              int marks, rno;
              char name[50];
      public:
             Student(){
                       r++;
                       rno = r;
                       }
             void get(){
                       cout<<"Enter your Name: ";
                       gets(name);
                       cout <<"Enter your Marks: ";
                       cin>>marks;
                  }
             void show(){
                         cout <<"Your roll no:" << rno<<endl;
                         cout <<"Your Name: "<< name<< endl;
                         cout<<"Your Marks" << marks<<endl;
                  }
};
int Student::r=0;
int main(){
    Student a,b,c;
    a.get();
    b.get();
    c.get();
    a.show();
    b.show();
    c.show();
    system("pause");
}
Gets leaves the newline character in the input buffer. Flushing the input buffer will solve it.

1
2
3
4
5
6
7
8
 void get()
  {
    fflush(stdin);
    cout<<"Enter your Name: ";
    gets(name);
    cout <<"Enter your Marks: ";
    cin>>marks;
  }


The most recent revision of the C standard (2011) has definitively removed this function from its specification.
The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3).

http://www.cplusplus.com/reference/cstdio/gets/

Better to use fgets - like fgets(name, sizeof(name), stdin);
http://www.cplusplus.com/reference/cstdio/fgets/

Thomas1965 wrote:
Flushing the input buffer will solve it.

No it will not. The behaviour is undefined if you use fflush on an input stream.
@Peter87

It worked on my PC.

The reference doesn't say anything about undefined behaviour.
http://www.cplusplus.com/reference/cstdio/fflush/

How would you do it then ?
Thomas1965 wrote:
It worked on my PC.

http://www.cplusplus.com/reference/cstdio/fflush/
In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).


Thomas1965 wrote:
The reference doesn't say anything about undefined behaviour.

http://en.cppreference.com/w/cpp/io/c/fflush
For input streams (and for update streams on which the last operation was input), the behavior is undefined.


Thomas1965 wrote:
How would you do it then ?

One common method to remove all input up to and including the next newline character is:
 
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
You can put this after you have used the >> operator on cin.


Thomas1965 wrote:
Better to use fgets

Another possibility is to use the C++ function getline.
 
std::cin.getline(name, sizeof(name));

You should also consider making name a std::string instead of a char array. In that case you use the non-member version of getline:
 
std::getline(std::cin, name);
Last edited on
Topic archived. No new replies allowed.