【问题标题】:gets() not taking input获取()不接受输入
【发布时间】:2013-09-30 03:15:54
【问题描述】:

我有一些大学工作,我注意到 get() 不起作用,但我不知道为什么。

我尝试将 getch() 和 getchar() 放在 get() 之前,但还有其他问题。

当我在 do-while(标记为 -----> 3)之前编写实现 gets() 的代码时,它可以工作!!!

有人可以帮帮我吗?

#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);
}

【问题讨论】:

  • 你为什么混合cingetsgets 是一个过时的 C 函数。
  • 你为什么混合cingets
  • 为了你自己,永远不要使用getsstackoverflow.com/questions/1694036/…
  • 我应该用什么来获取可以有空格的字符串?
  • 你可以使用cin.getline()

标签: c++ gets


【解决方案1】:

不要使用gets

使用cin.getline() 而不是gets,无论您使用的是gets

cin.getline(department, sizeof department);

gets is outdated,因为无法指定输入大小,存在缓冲区溢出的危险。

摆脱不需要的换行符

在您的情况下,gets 使用的是前一个输入中的(未丢弃的)换行符,因此存储了一个空字符 *。使用 cin.ignore() 去除不需要的空格 - 在使用 getline() 时也需要它。

或者,您可能总是希望使用cin.getline() 以一致的方式读取用户输入,然后使用parse the input depending on the type of data you're expecting。这也将允许您执行更好的错误检查。

【讨论】:

  • 但我之前也使用了 getchar() 和 getch() 来测试是否是这种情况,但仍然没有变化?
  • 要丢弃空间,您可以在使用cin 读取输入后立即调用cin.ignore()。 (我不确定为什么 getchar() 不适合你——它在 gcc 中适合我)。
  • @legends2k:感谢您的评论 - 我在答案中添加了一个编辑。
【解决方案2】:

您正在混合使用 C 和 C++。当然,这是允许的,但有一种叫做惯用语言的方式。这意味着语言用户有一种以优雅的方式表达结构的自然方式。我建议更改的两个地方:

  1. 使用std::strings 代替char 数组; std::string department;
  2. 使用std::getline(std::cin, department);

数组是臭名昭著的错误来源。将此类低级内存管理留给可用的标准库设施。

【讨论】:

  • 同意,当它已经完成时,我认为它正在越来越多地击败一匹死马(cmets 和 Hippo 的回答)。没错,我可以添加它。但我认为推荐关于字符串的 OP 会让他完全摆脱旧习惯。
  • 这个解释对我来说特别直观。谢谢!
猜你喜欢
  • 2014-09-08
  • 1970-01-01
  • 2013-08-27
  • 2014-01-24
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多