1

I have some college work and as i noticed that the gets() is not working but i can't figure out why.

I tried putting getch() and getchar() before gets() but there is something else wrong.

When i write a code implementing gets() before do-while (labeled -----> 3) it works!!!

Can somebody help me?

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

class student
{
    int rollNo;
    char department[20];
    int year;
    int semester;

public:
    student()
    {
        rollNo=0;
        year=0;
        semester=0;
    }

    void getData();
    void promote();
    void changeDepartment();
    void display();
};

void student::changeDepartment()
{
    if(rollNo!=0)
    {
        cout<<"\nEnter the new Department\n";
        gets(department);                                 -------------->1
    }

    else
    {
        cout<<"\nStudent not confirmed\n";
    }
}

void student::getData()
{
    cout<<"\nEnter the roll no\n";
    cin>>rollNo;
    cout<<"\nEnter the year\n";
    cin>>year;
    cout<<"\nEnter the semester\n";
    cin>>semester;
    cout<<"\nEnter the department\n";
    gets(department);                                  ----------------> 2
}

void student::promote()
{
    if(rollNo!=0)
    {
        semester+=1;
        if(semester%2==1)
        {
            year+=1;
        }
    }

    else
    {
        cout<<"\nStudent not confirmed\n";
    }
}

void student::display()
{
    if(rollNo!=0)
    {
        cout<<"\nRoll No : "<<rollNo;
        cout<<"\nYear : "<<year;
        cout<<"\nSemester : "<<semester;
        cout<<"\nDepartment : "<<department;
    }

    else
    {
        cout<<"\nStudent not confirmed";
    }
}

int main()
{
    student s;
    int ch;
    char choice;
                                                      ----------------> 3
    do
    {
        cout<<"\nMain Menu";
        cout<<"\n1. Enter student details";
        cout<<"\n2. Change department of student ";
        cout<<"\n3. Promote student ";
        cout<<"\n4. Display student details ";
        cout<<"\nEnter your choice ";
        cin>>ch;

        switch(ch)
        {
            case 1 : s.getData();
                     s.display();
                        break;

            case 2 : s.changeDepartment();
                     s.display();
                        break;

            case 3 : s.promote();
                     s.display();
                        break;

            case 4 : s.display();
                        break;
        }

        cout<<"\nDo you want to continue? (Y/n)\n";
        cin>>choice;
    }while((choice=='y')||(choice=='Y'));

    return(0);
}
Deanie
  • 2,316
  • 2
  • 19
  • 35
Lakshay Chhikara
  • 186
  • 2
  • 12

2 Answers2

2

Do not use gets

Use cin.getline() instead of gets, wherever you're using gets.

cin.getline(department, sizeof department);

gets is outdated, because of the danger of buffer overflow since the input size cannot be specified.

Getting rid of unwanted newlines

In your case, gets was using the (undiscarded) newline from the previous input and hence storing an empty char *. Use cin.ignore() to get rid of the unwanted spaces - you'll need this when using getline() too.

Alternatively, you may always want to use cin.getline() to read user input in a consistent way, and then parse the input depending on the type of data you're expecting. This will also allow you to perform much better error checking.

Community
  • 1
  • 1
AbdullahC
  • 6,649
  • 3
  • 27
  • 43
  • But i also used getchar() and getch() before it to test if that was the case but still there was no change? – Lakshay Chhikara Sep 30 '13 at 03:23
  • To discard the space, you can call `cin.ignore()` immediately after you read input using `cin`. (I'm not sure why `getchar()` isn't working for you though - it works for me in gcc). – AbdullahC Sep 30 '13 at 03:36
  • [cin.getline() is skipping an input in C++](http://stackoverflow.com/a/10200009/183120) – legends2k Sep 30 '13 at 03:39
  • @legends2k: Thanks for the comment - I've added an edit into my answer. – AbdullahC Sep 30 '13 at 03:45
1

You're mixing C and C++. Of course, it is allowed, but there's something called an idiomatic way of using a language; meaning the language users have a natural way of expressing constructs in an elegant way. Two places where I'd suggest change:

  1. Use std::strings instead of char arrays; std::string department;
  2. Use std::getline(std::cin, department);

Arrays are notorious for being a rich source of bugs. Leave such low-level memory management to the standard library facilities available.

legends2k
  • 31,634
  • 25
  • 118
  • 222
  • Agreed, I thought it's beating a dead horse more and more when it's already been done (comments and Hippo's answer). True, I could've added it. But I thought recommending the OP about string would completely veer him away from old habits. – legends2k Sep 30 '13 at 03:49
  • This explanation was particularly intuitive to me. Thanks! – Apekshik Panigrahi Jan 21 '20 at 13:19