【问题标题】:gets() & puts() not declared in scope in dev c++get() & puts() 未在 dev c++ 的范围内声明
【发布时间】:2014-06-08 06:26:43
【问题描述】:

这是我的书店简单代码 代码没有问题。我正在使用 DevC++ 运行代码,编译后它给出了一个错误,指出在这个范围内没有声明 'gets' 并且对于 puts 也有同样的错误。请帮我。

#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<iomanip>
#include<cstring>

using namespace std;

class Book
{
    char *title,*author,*publisher,ans;
    int price,quant,quant_ent;

    public:
            Book()
            {
        title = new char[50];
        author = new char[50];
        publisher = new char[50];
        price = quant = quant_ent = 0;
            }

        void getdata()
        {
            cout<<"\nEnter The Title";
            gets(title);
            cout<<"\nEnter The Author";
            gets(author);
            cout<<"\nEnter The Publisher";
            gets(publisher);
            cout<<"\nEnter The Price";
            cin>>price;
            cout<<"\nEnter The Quantity";
            cin>>quant;
        }

    void display()
    {
        cout<<setw(15)<<title<<setw(15)<<author<<setw(15)<<publisher<<setw(10)<<quant
            <<setw(10)<<price;
    }

    void search(char search_title[],char search_author[])
    {
        if(strcmpi(author,search_author)==0)
        {
            if(strcmpi(title,search_title)==0)
            {
                cout<<"\nBook Found!";
                cout<<"\nEnter The Quantity: ";
                cin>>quant_ent;
                if(quant_ent <= quant)
                {
                    cout<<"\nThe Title is: ";
                    puts(title);
                    cout<<"\nThe Author is: ";
                    puts(author);
                    cout<<"\nThe Publisher is: ";
                    puts(publisher);
                    cout<<"\nPrice Of Single Copy: "<<price;
                    cout<<"\nTotal Price = "<<price*quant_ent;
                    quant = quant - quant_ent;
                }

                else
                {
                    cout<<"\nSufficient Quantity Not Available!";
                }
            }
        }
    }
};


int main()
{
Book obj[10];
int i=0,ch;

char author[50],title[50];

for(;;)
{
    cout<<"\n*******MENU********\n1)Enter Details\n2)Buy Book\n3)Display All Books\n4)Exit";
    cin>>ch;

    switch(ch)
    {
        case 1:
            obj[i].getdata();
            i++;
            break;

        case 2:
            cout<<"\nEnter The Authors Name: ";
            gets(author);
            cout<<"\nEnter The Title: ";
            gets(title);
            for(int j=0;j<i;j++)
            {
                obj[j].search(title,author);
            }

            break;

        case 3:
            cout<<setw(15)<<"TITLE"<<setw(15)<<"AUTHOR"<<setw(15)<<"PUBLISHER"<<setw(15)<<"QUANTITY"<<setw(15)<<"PRICE";
            cout<<"\n"<<setw(75)<<"-----------------------------------------------------------------------------------------------------";
            for(int j=0;j<i;j++)
            {
                cout<<"\n";
                obj[j].display();
            }

        case 4:
            exit(1);
    };
}
}

【问题讨论】:

  • 您缺少包含:#include &lt;stdio.h&gt;。也就是说...为什么在 C++ 中你使用 gets()、puts() 和 char[] 而不是 getline()、cout 和 string?
  • 您的问题是您必须使用它们,因为您从 C 风格的字符串开始。移至std::string,您将不需要C 函数,因为cin 和cout 与string 配合得很好。
  • "代码没有问题。" -- 你的说法有什么依据?
  • @Adriano 非常感谢。我不太了解 std::string 类。我一定会在网上读到它。但是我的大学电脑有 Turbo C++,而且这个命名空间的东西似乎不起作用。那么这个 std::string 会起作用吗?提前感谢
  • @user237566 Turbo C++ 来自远古时代,但是是的,如果它不是太旧,那么你就有 std::string 类:不再有 puts()/gets()/char[] (嗯,或多或少)。

标签: c++ dev-c++


【解决方案1】:

因为它是在stdio.h(C++ 中的cstdio)标头中声明的,而您没有包含它。

但是you shall not use gets. 这是一个无可救药的坏功能。请改用fgets。更好的是,放弃指向 char 数组的裸指针并改用 std::string 类。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-12-17
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 2012-06-28
  • 2015-08-21
  • 2015-08-20
  • 1970-01-01
相关资源
最近更新 更多