谁看下我错哪了,一开始输入0后就出错!!peach5460在吗,给分的机会到了
#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]<'0'||str[i]>'9' )&& (str[i]!='.'))
{
return false;
}
}
return true;
}
book * creat()
{
book * pone;
book * ptwo;
pone=new book;
ptwo=pone;
head=pone;
cout<<"输入图书编号以0结束"<<endl;
string str;
cin>>str;
while (!check(str))
{
cout<<"你输入的不是数字,请输入数字"<<endl;
cin>>str;
}
pone->num=atoi(str.c_str());
if(pone->num!=0)
{
cout<<"请输入图书的价格:"<<endl;
cin>>str;
while (!check(str))
{
cout<<"你输入的不是数字,请输入数字"<<endl;
cin>>str;
}
pone->price=atof(str.c_str());
}
else
{
delete pone;ptwo=NULL;ptwo->next=NULL;head=NULL;return head;
}
while (pone->num!=0)
{
ptwo=pone;
pone=new book;
cout<<"输入图书编号以0结束"<<endl;
cin>>str;
while (false==check(str))
{
cout<<"你输入的不是数字,请输入数字"<<endl;
cin>>str;
}
pone->num=atoi(str.c_str());
if(pone->num!=0)
{
cout<<"请输入图书的价格:"<<endl;
cin>>str;
while (false==check(str))
{
cout<<"你输入的不是数字,请输入数字"<<endl;
cin>>str;
}
pone->price=atof(str.c_str());
}
}
delete pone;
pone->next=NULL;
return head;
}
int main()
{
creat();
return 0;
}
要讲清楚呦,本人比较笨的