C++ source file 编译后运行的程序实现不了功能
#include <iostream.h>void main()
{
char s;
int j;
cout<<"Input a string:";
cin.get(s,50);
cout<<"The string you put in:"<<s<<endl;
cout<<"Output string:";
for(j=0;j<50;j++)
{
if(s!=' '&&s=='\0')
cout<<s;
else
break;
}
}
这个程序是要求输入一段字符串,然后把其中的空格删除后输出其他的字符,例如:输入"what are you doing?"
输出"whatareyoudoing?"
大家帮我看看可以怎么改写
友情提示,数组下标不要用i,否则字体倾斜,很难看
[ 本帖最后由 powerwind 于 2007-4-28 00:10 编辑 ] 貌似后面的循环是有问题的...
for(i = 0; i<50; i++)
{
if(' ' != s )
{
cout<<s;
}
else if( '\0' != s )
{
break;
}
else
{
continue;
}
}
这样应该可以用吧... #include <iostream.h>
void main()
{
/*char s;*/
char s;
int j;
cout<<"Input a string:";
cin.get(s,50);
cout<<"The string you put in:"<<s<<endl;
cout<<"Output string:";
for(j=0;j<50;j++)
{
if(s=='\0')break;
if(s!=' ')
cout<<s;
/* else
break;*/
}
}
[ 本帖最后由 powerwind 于 2007-4-28 00:14 编辑 ] #include <iostream.h>
void main()
{
/*char s;*/
char s;
int j;
cout<<"Input a string:";
cin.get(s,50);
cout<<"The string you put in:"<<s<<endl;
cout<<"Output string:";
for(j=0;j<50;j++)
{
if(s=='\0')break;
if(s!=' ')
cout<<s;
/* else
break;*/
}
}
这个运行时,输入"what are you doing?"结果是"whatareyoudoing?"
[ 本帖最后由 luo123 于 2007-4-28 00:58 编辑 ] 原帖由 luo123 于 2007-4-28 00:53 发表
这个运行时,输入"what are you doing?"结果是"whatareyoudoing?"
这个结果不正确?
你的意思应该就是要这样的效果吧? 晕死,才发现数组下标怎么没了的样子... LZ的代码:(红色部分)
#include <iostream.h>
void main()
{
char s;
int j;
cout<<"Input a string:";
cin.get(s,50);
cout<<"The string you put in:"<<s<<endl;
cout<<"Output string:";
for(j=0;j<50;j++)
{
if(s!=' '&&s=='\0')
cout<<s;
else
break;
}
}
原帖由 jinry 于 2007-4-28 09:47 发表
晕死,才发现数组下标怎么没了的样子...
有这个选项:(顺便TEST)
禁用 Discuz! 代码
页:
[1]