帮我看看这个C++程序怎么就运行崩溃了

来源:百度知道 编辑:UC知道 时间:2024/06/06 06:13:47
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class book
{
public:
int num;
float price;
book *next;
};
book *head =NULL;
bool check(string str)
{
for(int i=0;i<str.length();i++)
{
if((str[i]>'9'||str[i]<'0')&&(str[i]!='.'))
return false;
}
return true;
}
book *creat()
{
book *p1,*p2;
p1 = new book ;
head =p1;
p2=p1;
cout <<"请输入书籍的编号(0返回):";
string str;
cin >> str;
while(!check(str))
{
cout <<"输入不是数字,请重新输入,按0返回"<<endl;
cin >>str;
}
p1->num = atoi(str.c_str());
if (p1->num!=0)
{
cout <<"请输入书籍的价格:";
cin >> str;
while(!check(str))
{
cout <<"输入不是数字,请重新输入,按

main函数这样改就好了

int main(int argc, char* argv[])
{ /*book *head=NULL;---->这句话删除就好了,head要用全局的,不要临时的。*/
head=creat();
showbook(head);
cout <<"请输入要删除书本的编码:"<<endl;
int booknum;
cin >>booknum;
Delete(head,booknum);
showbook(head);
return 0;
}

把 main() 函数中的第一句注释掉
其它话我不说

把#include "stdafx.h" 删掉就行了

首先建议你学习如何调试程序。在VC里面调试可以一句一句跟踪执行,查看你的变量的值。
我怀疑你的creat的最后的一句:delete p1.
你已经释放了p1的空间,然后又返回了head. 但是head仍然指向p1所new出来的内存空间,这个head已经是坏指针了。你应该p1 = NULL,或者不管它也可以。p1是函数的局部变量会自动释放。

你的C语言需要好好学学。看看《C Programming Language》吧,有中文版<C程序设计语言>。C语言的发明者写的。之后再学C++会很容易的。
直接学C++也可以的,不过内容比较多。
good luck!