【问题标题】:Syntax error in a turbo c++ programturbo c++ 程序中的语法错误
【发布时间】:2016-04-24 07:30:54
【问题描述】:

我的代码中出现声明语法错误,但我没有发现任何错误输入或任何内容。请帮助。我正在使用 turbo c++ 进行学校级别的编程。 我在我输入 -void issue() 的地方得到它(在主函数内)

#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() {
        cout<<"Name of the book is :- ";puts(bookname);cout<<endl;
        cout<<"ISBN :- ";puts(isbn);cout<<endl;
        cout<<"Author :- ";puts(author);cout<<endl;
        cout<<"Category :- ";puts(category);cout<<endl;
        cout<<"Price :- ";cout<<price<<endl;
        cout<<"No of copies available :- ";cout<<noc<<endl;
    }

    void issue();

}b[5];


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

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

        if(flag==0) {
            cout<<"Book found \n";
            b[i].display();
            b[i].issue();
        } else {
            cout<<"Book not available:- \n";
        }

    }

    getch();
    return 0;
}

【问题讨论】:

  • 你为什么在main里面定义issue

标签: c++ function syntax compiler-errors turbo-c++


【解决方案1】:
  1. main 没有右大括号 在 void issue() 之前插入一个
  2. 现在将void issue() 更改为void book::issue()

解决这个问题,然后告诉我们任何进一步的错误以及它发生的行

编辑

而不是 1) 移动

  getch();
  return 0;
}

到那个位置

【讨论】:

  • b[5] 是类书的对象数组,无论如何我尝试按照通常的方式进行操作,但结果相同。 2)我在main函数里面创建了issue函数,是不是有效?
  • 我确实注意到了这一点并更新了我的答案。如果您整理缩进会有所帮助
  • @JonnyHenly (1) 通过为main 放入右大括号来解决此问题
  • @JonnyHenly 如果可以帮助我将问题移出主要问题 :)
  • @HarshAdwani 按照这个答案所说的去做 - 剪切所有 void issue() { ... } 并将其粘贴到 int main() { ... } 之外,然后在 issue() 前面添加 book:: 以形成 void book::issue() { ... }。然后编译并报告是否出现任何其他错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 1970-01-01
  • 1970-01-01
  • 2011-01-22
  • 2015-10-26
相关资源
最近更新 更多