2016-04-23 164 views
1

請幫我解決這個問題我在學校也輸入了代碼,即使在那裏它也顯示了聲明語法錯誤。 - 無法弄清楚這一點! 。當你剛開始學習編碼時,它非常令人沮喪。函數聲明語法錯誤

無效問題的錯誤:聲明語法錯誤 無效顯示錯誤:非法使用指針,如果

道歉在我任何愚蠢發現。

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


class book 
{ 
char bookname[20]; 
char isbn[20]; 
char author[20]; 
char category[20]; 
float price; 
int noc; 

public: 

void accept() 
{ 

cout<<"Enter book name :- \n"; 
gets(bookname); 
cout<<"Enter isbn no of the book:- \n"; 
gets(isbn); 
cout<<"Enter authour name:- \n"; 
gets(author); 
cout<<"Enter category of book:- \n"; 
gets(category); 
cout<<"Enter price of the book :- \n"; 
cin>>price; 
cout<<"Enter no of copies of book available in the library :- \n"; 
cin>>noc; 
} 

void display() 
{ 
puts(bookname)<<endl; 
puts(isbn)<<endl; 
puts(author)<<endl; 
puts(category)<<endl; 
cout<<price<<endl; 
cout<<noc<<endl; 
} 

}b[5]; 

int main() 
{ 
for(int i=0;i<5;++i) 
{ 
b[i].accept(); 
} 

void issue() 
{ 
int flag=0; 
char booksearch[20]; 
cout<<"Enter name of book member wants to issue :- \n" 
gets(booksearch); 
    for(i=0;i<5;++i) 
    { 
     flag=strcmp(booksearch,b[i].bookname) 
    } 

} 

if(flag==1) 
{ 
    b[i].display(); 
    b[i].issue(); 
} 
getch(); 
return 0; 
} 
+0

你可以更具體..並張貼更多的代碼 – Pavan

+0

歡迎來到StackOverflow!請花點時間瀏覽http://stackoverflow.com/help/mcve。特別是你應該在你的問題中包含完整的複製粘貼錯誤。 –

+0

您使用的是純文本編輯器還是文字處理器? –

回答

1

有許多錯誤,你的代碼:

  1. strcmp通話後失蹤分號:
  2. strcmp返回0時,有一個匹配,而不是1,並且您可能會在循環的下一次迭代中覆蓋標誌,
  3. 您對issue的定義位於的中間,
  4. 你混合C風格變得和C++ - 風格的操作符>>,
  5. 你混合 - 嚴重 - C風格的看跌期權和運營商< <

這裏是不工作的版本你的代碼: http://ideone.com/sGdXcm

這裏是一個固定的工作版本:

#include <iostream> 
#include <string> 
#include <array> 
#include <limits> 

using namespace std; 

class book 
{ 
    std::string bookname; 
    std::string isbn; 
    std::string author; 
    std::string category; 
    float price; 
    int noc; 

public: 
    const std::string& getBookname() const { return bookname; } 
    const std::string& getISBN() const { return isbn; } 
    const std::string& getAuthor() const { return author; } 
    const std::string& getCategory() const { return category; } 
    float getPrice() const { return price; } 
    float getNoC() const { return noc; } 

    void accept() 
    { 
     cout<<"Enter book name :- \n"; 
     std::getline(std::cin, bookname); 
     cout<<"Enter isbn no of the book:- \n"; 
     std::getline(std::cin, isbn); 
     cout<<"Enter authour name:- \n"; 
     std::getline(std::cin, author); 
     cout<<"Enter category of book:- \n"; 
     std::getline(std::cin, category); 
     cout<<"Enter price of the book :- \n"; 
     std::cin>>price; 
     cout<<"Enter no of copies of book available in the library :- \n"; 
     std::cin>>noc; 
     std::cin.ignore(std::numeric_limits<streamsize>::max(), '\n'); 
    } 

    void display() 
    { 
     std::cout<<bookname<<std::endl; 
     std::cout<<isbn<<std::endl; 
     std::cout<<author<<std::endl; 
     std::cout<<category<<std::endl; 
     std::cout<<price<<std::endl; 
     std::cout<<noc<<std::endl; 
    } 

    void issue() 
    { 
    } 
}; 

int main() 
{ 
    std::array<book, 5> b; 
    for(int i=0;i<b.size();++i) 
    { 
     b[i].accept(); 
    } 

    std::string booksearch; 
    std::cout<<"Enter name of book member wants to issue :- \n"; 
    std::getline(cin, booksearch); 
    std::cout<<"Searching for: " << booksearch << "\n"; 
    for(int i=0;i<b.size();++i) 
    { 
     if (b[i].getBookname() == booksearch) 
     { 
      b[i].display(); 
      b[i].issue(); 
      break; 
     } 
    } 

    std::string dummy; 
    std::cout << "Hit return:"; 
    std::getline(std::cin, dummy); 

    return 0; 
} 

李ve demo:http://ideone.com/p3Ygw3

注意:如果用戶在輸入書籍時輸入錯字,那麼錯誤將會出錯,我沒有在此代碼中添加任何錯誤檢查。

1
  1. flag=strcmp (searchbook, b [I]. bookname)行後添加一個分號。
  2. 聲明flag,searchbook,b如果尚未聲明。
  3. 你的函數之前執行#include <string.h>