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

如何让程序终止

最近不在 发布于 2010-03-11 18:48, 1040 次点击
程序代码:
#include<iostream>
using namespace std;
int asd(int n);
void out();
int main()
{
    out();
    return 0;
}

int asd(int n)
{
    int m;
    if(n==0||n==1)
    m=1;
    else
    m=asd(n-1)*n;
    return (m);
}

void out()
{
    int n,m;
    cin>>n;
    m=asd(n);
    cout<<m;
    cout<<"\n";
    while(n>=0)
    out();
}
   

   
   
   
   
我想让程序在输入负数时,终止,该怎么改
9 回复
#2
最近不在2010-03-11 18:53
程序代码:
#include<iostream>
using namespace std;
int asd(int n);
void out();
int main()
{
    out();
    return 0;
}

int asd(int n)
{
    int m;
    if(n==0||n==1)
    m=1;
    else
    m=asd(n-1)*n;
    return (m);
}

void out()
{
    int n,m;
    cin>>n;
    while(n>=0)
    {
    m=asd(n);
    cout<<m;
    cout<<"\n";
    out();
    }   
}

我这样试了,也不对,到底该怎么改,求帮助!
#3
卸载重装2010-03-11 19:06
我也算新手,改下看合适不
void out()
{
    int n,m;
    cin>>n;
    if(n<0)
    {
        cout<<"Error!again:"<<endl;
        out();
    }
    m=asd(n);
    cout<<m<<endl;
    out();
}
#4
卸载重装2010-03-11 19:10
先没明白你的意思...要终止的话这样改看行不
void out()
{
    int n,m;
    cin>>n;
    if(n<0)
    {
        cout<<"Error!"<<endl;
        return;
    }
    m=asd(n);
    cout<<m<<endl;
    out();
}
#5
最近不在2010-03-11 19:38
程序代码:
#include<iostream>
using namespace std;
fill_arry(double arry[],int len);
show_arry(double arry[],int len);
reverse_arry(double arry,int len);
const int len=5;
int main()
{
    int i=0;
    double arry[len];
    cout<<"please enter a number: ";
    while(i<len&&cin>>arry[i])
     {
        i++;
        
        cout<<"please enter a number: ";
     }
    return 0;
}


谢谢了,可行,老想着while,忘记了最简单的if
那这个该怎么改了(程序没写完,可运行,就是数组输入满了,最后还会多输出行please enter)?
就是让数组满了或者输入了非数字,输入终止.
#6
最近不在2010-03-11 19:42
帮忙运行下,到底该怎么解决了?
我看了一个星期的c++,书上的例子也照着打了,可当自己做书后编程题,总会出问题,书上没答案,囧,只好在这里问了!
#7
卸载重装2010-03-11 21:16
这要用到函数了.. 我刚找到的方法
#include<iostream>
using namespace std;
int main()
{
    int i=0,m;
    int arry[5];
    for(i=0;i<5;i++)
     {
        cout<<"please enter a number: ";
        cin>>arry[i];
        if(cin.fail())
        {
            cout<<"Error!"<<endl;
            i--;
            cin.clear();
            cin.ignore();
        }
    }
    return 0;
}
#8
qq1135514702010-03-11 22:52
if(cin.fail())
        {
            cout<<"Error!"<<endl;
            i--;
            cin.clear();
            cin.ignore();
        }
    }
请问楼主这段代码是什么意思  实现什么功能,还有你的整段代码是为了输出   
只有本站会员才能查看附件,请 登录

因为本人也是新手  所以请你多指教
#9
shiyuehai2010-03-12 09:01
加个if语句不就可以了么???
#10
卸载重装2010-03-12 09:13
LZ要实现判断输入的是不是数字.没想到其他方法。 在网上找的的用cin.fail()函数判断。我也是新手啦
1