2014-04-23 57 views
1

這是我對書店的簡單代碼 代碼沒有任何問題。我正在使用DevC++來運行代碼,並在編譯後給出一個錯誤,說'獲取'沒有在此範圍內聲明&對於puts相同的錯誤。請幫幫我。dev(++)中沒有聲明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); 
    }; 
} 
} 
+0

你錯過了一個include:'#include '。這就是說......爲什麼在C++中使用gets(),puts()和char []而不是getline(),cout和string? –

+0

你的問題是你必須使用它們,因爲你開始使用C風格的字符串。移動到'std :: string',你不需要C函數,因爲'cin'和'cout'和'string'一起工作良好。 –

+0

「代碼沒有問題。」 - 你有什麼基礎? – hvd

回答

1

因爲它宣佈stdio.hcstdio在C++)報頭和您還沒有它。

但是you shall not use gets.這是一個絕望的破碎的功能。改爲使用fgets。更好的辦法是,將指針指向char數組,然後使用std::string類。

+1

但請注意,'fgets'並不是一個安全的'gets'。 [它完全有不同的行爲。](http://www.drdobbs.com/cpp/social-processes-and-heartbleed-part-1/240167053) – BoBTFish