首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

输入出现两次或以下的“ ”(空格键),调试就停止了,为什么呢

2012-09-29 
输入出现两次或以上的“ ”(空格键),调试就停止了,为什么呢?各路高手,我是个C++新手,就是我写了一个简单的链

输入出现两次或以上的“ ”(空格键),调试就停止了,为什么呢?
各路高手,我是个C++新手,就是我写了一个简单的链表的代码
这个代码作用是:输入一串字符,然后只输出其中的阿拉伯数字
但是,遇到了2个问题
--------------------------
问题1、比如输入zxw123r
  结果输出时,尾插法输出会出现一个“?”,可是我已经做了处理,为什么处理不管用?
问题2:同时输入两个以上的空白字符,exe调试就会自动停止,这是为什么呢?
  比如我输入:zxw aishangle zd
  由于有了两个" ",exe就停止了。
-------------------------
代码如下:
#include<iostream>
using namespace std;
struct link
{
char data;
link *plink;
};
int main()
{
char str[100];
cout<<"请输入一串数据:";
cin>>str;
int num=strlen(str);
cout<<"这一串数据的情况为:"<<endl;
for(int i=0;i<num;i++)
{
cout<<str[i];
if(i%5==0 && i!=0)
cout<<endl;
}
cout<<endl;
link* headput(char [],int n);//不带头结点的头插法
link* tailinto_link(char [],int n);//带头结点的尾插法
void scan(link*tempch);
cout<<endl<<"不带头结点的头插法输出数字"<<endl;
scan(headput(str,num));
cout<<endl<<"带头结点的尾插法输出数字"<<endl;
scan(tailinto_link(str,num));
system("pause");
return 0;
}
link* headput(char str[],int n)
{
link *pch,*tempch;
tempch=NULL;
for(int i=0;i<n;i++)
if(str[i]>='0' && str[i]<='9')
{
pch=new link;
pch->data=str[i];
pch->plink=tempch;
tempch=pch;
}
return tempch;
}
link* tailinto_link(char str[],int n)
{
link *pch,*tempch;
link *pnew;
pch=pnew=new link;
pch->plink=NULL;
for(int i=0;i<n;i++)
if(str[i]>='0' && str[i]<='9')
{
tempch=new link;
tempch->data=str[i];
pnew->plink=tempch;
pnew=tempch;
}
pnew->plink=NULL;
return pch;
}
void scan(link*tempch)
{
link*temp;
temp=tempch;
while(temp->plink!=NULL)
{
if(temp->data==NULL) //这一步好像没有用,解决不了输出“?”的问题
temp=temp->plink;
else
{
cout<<temp->data<<" ";
temp=temp->plink;
}
}
cout<<temp->data<<endl;
}
//请各路高手相助,解答我的问题!

[解决办法]
getline(cin, str);
没记错的话。

热点排行