2016-04-23 89 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>