注册 登录
编程论坛 C++教室

一个小小的程序,为什么运行失败呢??who can help me????

lijunbo 发布于 2011-10-06 12:02, 608 次点击
题目是读入两个string对象,测试它们是否相等。若不相等,指出两个中哪个较大。
以下是我编写的程序,编译没有问题,但是运行后输入两个字符串后就自动退出了。why???
who can tell me??

#include <iostream>
#include<string>
using namespace std;

int main()
{
    string s1,s2;
    cin>>s1>>s2;
    if(s1==s2)
        cout<<s1<<"="<<s2<<endl;
    else if(s1<s2)
        s1=s2;
    else
        cout<<"the big of them is "<<s1;
    return 0;
}

[ 本帖最后由 lijunbo 于 2011-10-6 12:06 编辑 ]
2 回复
#2
黄昏的王座2011-10-06 12:31
当s1<s2时你要有个输出语句吧,不然在此条件下就直接return 0了!
#3
Toomj2011-10-06 13:56
#include <iostream>
#include<string>
using namespace std;

int main()
{
    string s1,s2;
    cin>>s1>>s2;
    if(s1==s2)
        cout<<s1<<"="<<s2<<endl;
    else if(s1<s2)
        s1=s2;
    //else
        cout<<"the big of them is "<<s1;
        system("pause");//VC中要加这句才不会立即退出
    return 0;
}
1