1

Here is a code snippet I have written using c++ in turbo C++ IDE. The problem I am facing is after using gets(), cin is not working as it is skipping the inputs.Can someone possibly provide a solution to this issue. Here is the code snippet :-

        #include<iostream.h>
        #include<conio.h>
        #include<stdio.h>   
        int Resc()
           {
              char fName[10],lName[10],addr[100],usr[70],pass[20];
              int   d,y,m;
              unsigned int phNo;
              char *Gend; 
             clrscr();
             cout<<"Enter First Name :"<<endl;
             gets(fName);
             cout<<"Enter Last Name :"<<endl;
             gets(lName);
             cout<<"Enter Gender :"<<endl;
             gets(Gend);
             cout<<"Enter Address:"<<endl;
             gets(addr);
             cout<<"Enter Date Of Birth (d/m/y):"<<endl;
             cin>>d>>m>>y;
             cout<<"Enter Phone Number :"<<endl;
             cin>>phNo;
             cout<<"Enter Username:"<<endl;
             gets(usr);
             cout<<"Enter Password:"<<endl;
             gets(pass);
             getch();
             return 0;
           }

It would be a great help. Thanks.

2 Answers2

1

Turbo-C++ is ancient. There are free compilers available that are much better. Though I realize that in some countries, unfortunately it is still required by educators. However, if there is any way you can use an alternative, you should. The code you are learning to write right now won't compile on compilers actually used in the industry. This will cause problems for you in future jobs.

gets is the worst function ever to make it into a language standard library. It is impossible to use correctly. Don't ever use it. Excise its existence from your mind.

Don't mix C and C++ I/O. It leads to tricky issues where they get out of sync. Use one or the other, exclusively. If you use C I/O, use scanf and fgets with stdin, not gets. If you use C++ I/O, use cin exclusively. Note that parsing a "d/m/y" date with cin is a bit tricky. On the other hand, using cin would allow you to use string instead of character arrays, which would be infinitely superior. (For example, it would mean you can enter names longer than 9 characters without making your program do weird things.)

Gend should probably be a single char instead of a pointer pointing at nothing. This part of your program is just wrong and extremely likely to misbehave or crash.

Phone numbers are not integers. They often start with zeros, in typical usage contain punctuation and whitespace, and are long enough to overflow an unsigned int. (The moment you use an area or carrier prefix, the integer interpretation is likely more than 4000000000.) Use strings to store phone numbers, always.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
0

Basically, gets() is not a C++ function (it is there because the whole C library is available to C++)

The thing is that you are messing usage of buffered input (using stdio package) with buffered C++ input system. And buffers hit to each other.

Don't mix stdio and c++ buffered I/O systems, as you'll get this kind of problems. What's happening inside of Turbo C++'s implementation is far from being able to check, as 1) you haven't disclosed which TC++ version you are using and 2) I have no such compiler at hand to make tests.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31