C++中循环使用程序

来源:百度知道 编辑:UC知道 时间:2024/05/25 18:50:10
怎么能让一个程序反复用啊
#include<iostream>
using namespace std;
int she(int n)
{
int s=0;
if(n>0){if(n==1) s=1;
else s+=n*n+she(n-1);}
else cout<<"error"<<endl;
return s;
}
int main()
{
int n;
cout<<"请输入n的值:";
cin>>n;
cout<<"n的平方的累加值为:"<<she(n)<<endl;
return 0;
}
我输入一次值了然后得到一个值,我不想退出想继续输入,那代码应该加入什么啊,怎么改啊?知道的高手谢谢了,告诉一下!!

#include<iostream>
using namespace std;
int she(int n)
{
int s=0;
if(n>0){if(n==1) s=1;
else s+=n*n+she(n-1);}
else cout<<"error"<<endl;
return s;
}
int main()
{
int n;
while(n!=-1)//当输入-1时退出
{
cout<<"请输入n的值:";
cin>>n;
cout<<"n的平方的累加值为:"<<she(n)<<endl;
}
return 0;
}

goto