0

I encountered a problem where gets() is not working sometimes without any error compiling. In other words, gets() will not return any value but no warning or error explanation. Here is the code where it's not returning value

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;

class student
{
private:
    int admno;
    char sname[20];


public:
    void Takedata()
    {
        cout<<"Enter admission number ";
        cin>> admno;

        cout<<"Enter student name " ;
        gets(sname);
    }

    void Showdata()
    {
        cout<<"Admission number "<<admno<<"\nStudent name "<<sname;
    }
};

int main ()
{
    student obj ;
    obj.Takedata();
    obj.Showdata();
    getch();
    return 0;
}

And in contrast here is the code where it's returning value to "sname"

#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;

class student
{
private:
    int admno;
    char sname[20];


public:
    void Takedata()
    {

        cout<<"Enter student name " ;
        gets(sname);
    }

    void Showdata()
    {
        cout<<"\nStudent name "<<sname;
    }
};

int main ()
{
    student obj ;
    obj.Takedata();
    obj.Showdata();
    getch();
    return 0;
}

If anything is unclear don't hesitate to ask me! I'm glad to accept ant answer/solution/advise!

roalz
  • 2,699
  • 3
  • 25
  • 42
K.Wang
  • 1
  • 1
  • 2
    Do note that in C11 and C++14 `gets` has been removed. In C++ you really should be using `std::string` and `getline` to deal with strings. – NathanOliver Oct 27 '16 at 21:15
  • [Never use `gets`](http://stackoverflow.com/q/1694036/10077). – Fred Larson Oct 27 '16 at 21:18
  • Thank you for your reply, I changed gets(sname) to getline(cin,sname), char sname[20] to string sname, same problem still occur. – K.Wang Oct 27 '16 at 22:09
  • 1
    you can follow [this](http://stackoverflow.com/questions/5739937/using-getlinecin-s-after-cin).I think it will help you. – Real73 Oct 27 '16 at 22:11
  • The wierdest thing is that if i initialize any variable in my private class the value for string will not be returned, otherwise it will:( – K.Wang Oct 27 '16 at 22:12

1 Answers1

0

Use cin.ignore() before taking string input.I try this and it works fine.

#include<iostream>
#include<conio.h>
#include<stdio.h>

using namespace std;

class student
{
private:
    int admno;
    char sname[20];



public:
    void Takedata()
    {
        cout<<"Enter admission number ";
        cin>> admno;
        cin.ignore();
        cout<<"Enter student name " ;
        gets(sname);

    }

    void Showdata()
    {
        cout<<"\nAdmission number "<<admno<<"\nStudent name "<<sname;
    }
};

int main ()
{
    student obj ;
    obj.Takedata();
    obj.Showdata();
    getch();
    return 0;
}
Real73
  • 490
  • 4
  • 13
  • Thank you for ur reply, it works on mine too. Just one additional question does cin.ignore(); cin.ignore ( std::numeric_limits::max(), '\n' ); and cin.ignore(256,"\n") have the same meaning? – K.Wang Oct 27 '16 at 22:32