工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1490|回复: 7

C++ source file 编译后运行的程序实现不了功能

[复制链接]
发表于 2007-4-27 23:25 | 显示全部楼层 |阅读模式
#include <iostream.h>
void main()
{
    char s[50];
    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[j]!=' '&&s[j]=='\0')
            cout<<s[j];
        else
            break;
    }
}



这个程序是要求  输入一段字符串,然后把其中的空格删除后输出其他的字符,例如:输入"what are you doing?"
输出"whatareyoudoing?"
大家帮我看看可以怎么改写

友情提示,数组下标不要用i,否则字体倾斜,很难看

[ 本帖最后由 powerwind 于 2007-4-28 00:10 编辑 ]
发表于 2007-4-27 23:50 | 显示全部楼层
貌似后面的循环是有问题的...

for(i = 0; i<50; i++)
{
    if(' ' != s )
    {
        cout<<s;
    }
    else if( '\0' != s )
    {
        break;
    }
    else
    {
        continue;
    }
}
这样应该可以用吧...
回复

使用道具 举报

发表于 2007-4-28 00:12 | 显示全部楼层
#include <iostream.h>
void main()
{
    /*char s[50];*/
    char s[51];
    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[j]=='\0')break;
        if(s[j]!=' ')
            cout<<s[j];
       /* else
            break;*/
    }
}

[ 本帖最后由 powerwind 于 2007-4-28 00:14 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2007-4-28 00:53 | 显示全部楼层
#include <iostream.h>
void main()
{
    /*char s[50];*/
    char s[51];
    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[j]=='\0')break;
        if(s[j]!=' ')
            cout<<s[j];
       /* else
            break;*/
    }
}
  

这个运行时,输入"what are you doing?"结果是"whatareyoudoing?"

[ 本帖最后由 luo123 于 2007-4-28 00:58 编辑 ]
回复

使用道具 举报

发表于 2007-4-28 09:40 | 显示全部楼层
原帖由 luo123 于 2007-4-28 00:53 发表
这个运行时,输入"what are you doing?"结果是"whatareyoudoing?"


这个结果不正确?

你的意思应该就是要这样的效果吧?
回复

使用道具 举报

发表于 2007-4-28 09:47 | 显示全部楼层
晕死,才发现数组下标怎么没了的样子...
回复

使用道具 举报

发表于 2007-4-28 13:04 | 显示全部楼层
LZ的代码:(红色部分)
#include <iostream.h>
void main()
{
    char s[50];
    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[j]!=' '&&s[j]=='\0')
            cout<<s[j];
        else
            break;
    }
}
回复

使用道具 举报

发表于 2007-4-28 13:07 | 显示全部楼层
[quote]原帖由 [i]jinry[/i] 于 2007-4-28 09:47 发表
晕死,才发现数组下标怎么没了的样子... [/quote]

有这个选项:(顺便TEST)
禁用 [url=https://www.gdutbbs.com/faq.php?page=misc#1]Discuz! 代码[/url]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 10:04

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表