| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 543 人关注过本帖
标题:好头痛啊,俺是新手,求朋友们帮帮忙
只看楼主 加入收藏
chuigeng
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2011-3-4
结帖率:71.43%
收藏
已结贴  问题点数:20 回复次数:4 
好头痛啊,俺是新手,求朋友们帮帮忙
下面是我写的程序,定义一个book类存储书籍信息,编译成功,但是在运行时却好像发生了错误,运行的最后没有像以前那样显示:“请按任意键继续”,而且发出‘嘭’的一声提示音,好像是发生了什么错误,然后关不掉窗口,只能是点窗口右上角的叉强行关掉它,不明白为什么啊,希望有朋友能帮帮忙
注:应该是switch那个地方错了,但我怎么改也是那样没变化


//book.h

#include<iostream>
using namespace std;

//定义book类

class book
{
    char*bookname;
    double price;
    int number;
public:
    book(char *a="nobook", double b=0, int c=0);
    ~book();
    void display();
    void borrow();
    void restore();
    void set(char *e, double f, int g);
};


//book.cpp,定义函数

#include<iostream>
#include"aa.h"
using namespace std;

book::book(char *a, double b, int c)
{bookname=new char[40];bookname=a;price=b;number=c;}

book::~book()
{delete []bookname;bookname=NULL;}

void book::display()
{
    cout<<"bookname:"<<bookname<<endl
        <<"price:"<<price<<endl
        <<"number"<<number<<endl;
}

void book::borrow()
{
    number--;
    cout<<"number:"<<number<<endl;
}

void book::restore()
{
    number++;
    cout<<"number:"<<number<<endl;
}

void book::set(char *e, double f, int g)
{bookname=e;price=f;number=g;}

//main.cpp

#include<iostream>
#include"aa.h"
using namespace std;
int main()
{
    int h;
    book A;
    A.display();
    A.borrow();
    cout<<"What are going ti do?"<<endl;
    cin>>h;
    switch(h)
    {
    case 0:
        {
            char* e=new char[50];double f;int g;
            cout<<"name:"<<endl;
            cin>>e;
            cout<<"price"<<endl;
            cin>>f;
            cout<<"number:"<<endl;
            cin>>g;
            A.set(e,f,g);
            
        }
    case 1:
        {break;}
    case 2:
        {break;}
    default:
        {cout<<"error"<<endl;return -1;}
    }
}
搜索更多相关主题的帖子: book 朋友 
2011-03-04 23:06
xishui777
Rank: 2
等 级:论坛游民
帖 子:53
专家分:94
注 册:2010-8-17
收藏
得分:10 
#include<iostream>
#include"aa.h"
using namespace std;
int main()
{
    int h;
    book A;
    A.display();
    A.borrow();
    cout<<"What are going ti do?"<<endl;
    cin>>h;
    switch(h)
    {
    case 0:
        {
            char* e=new char[50];double f;int g;
            cout<<"name:"<<endl;
            cin>>e;
            cout<<"price"<<endl;
            cin>>f;
            cout<<"number:"<<endl;
            cin>>g;
            A.set(e,f,g);
            
        }break;
    case 1:
        break;
    case 2:
        break;
    default:
        {cout<<"error"<<endl;return -1;}break;
    }
}
不知道改的对吗?
2011-03-05 08:42
chuigeng
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2011-3-4
收藏
得分:0 
不对啊
2011-03-05 08:47
chuigeng
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2011-3-4
收藏
得分:0 
求各位帮忙啊
2011-03-05 09:01
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:10 
//book.h

#include<iostream>
using namespace std;

//定义book类

class book
{
    char *bookname;
    double price;
    int number;
    public:
     book(char *a, double b, int c);  //你的构造函数的参数怎么可以赋值?
    ~book();
    void display();
    void borrow();
    void restore();
    void set(char *e, double f, int g);
};


//book.cpp,定义函数

#include<iostream>
#include"book.h"
using namespace std;

book::book(char *a, double b, int c)
{  bookname=new char[40];  bookname=a;  price=b; number=c; }

book::~book()
{ bookname=""; } //delete只能删除用new申请的内存,你确定所有的bookname都是new申请的?

void book::display()
{
    cout<<"bookname:"<<bookname<<endl
        <<"price:"<<price<<endl
        <<"number"<<number<<endl;
}

void book::borrow()
{
    number--;
    cout<<"number:"<<number<<endl;
}

void book::restore()
{
    number++;
    cout<<"number:"<<number<<endl;
}

void book::set(char *e, double f, int g)
{ bookname=e; price=f; number=g; }

//main.cpp

#include<iostream>
#include"book.cpp"
using namespace std;
int main()
{
    int h;
    book A;
    A.display();
    A.borrow();
    cout<<"What are going to do?"<<endl;
    cin>>h;
    switch(h)
    {
    case 0:
        {
            char *e=new char[50]; double f; int g;
            cout<<"name:";
            cin>>e;
            cout<<"price:";
            cin>>f;
            cout<<"number:";
            cin>>g;
            A.set(e,f,g);
          }  break;
    case 1:
           break;
    case 2:
           break;
    default:
        {  cout<<"error"<<endl;  return -1;  break;  }
    }
    return 0;
}

   唯实惟新 至诚致志
2011-03-05 09:44
快速回复:好头痛啊,俺是新手,求朋友们帮帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021133 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved