如何从程序开始 [英] How to start from beginning of the program
本文介绍了如何从程序开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C ++的初学者.我只是在学习这种语言的abc.我创建了一个小程序,该程序将添加:
I am a very beginner in c++. i am just learning abc of this language.. i created this small program that would add:
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
system("pause");
}
以上代码是我对c ++的第一个可用的应用程序
this above code is my very first usable application of c++
现在我希望当某个人想要再次添加时,该程序再次启动...我想到了使用循环,但我却不知道如何使用这种方式.我的意思是我应该使用什么条件.
now i wanted that when some one wants to again add then this program starts again... i thoughts of using loops, i but cannot think how to use in such a way. i mean what conditions i should use.
请告诉我
谢谢.
推荐答案
以下代码将做到这一点:
The following code will do that:
#include <iostream>
using namespace std;
float add(float a, float b){
return a+b;
}
int main(){
float num1;
float num2;
while( true ){
cout<<"add...enter digits \n";
cout<<"first digit: ";
cin>>num1;
cout<<"\n Second number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
}
system("pause");
}
以上代码将永远运行.如果要给用户一个选择,请应用循环中断条件.
above code will run forever. If you want to give user a choice, then apply a loop break condition.
char repeat = 'y';
while( repeat == 'y'){
// do as previous
//.....
//finally give user a choice
cout<< "Do you want to repeat?(y/n):";
cin>> repeat;
}
system("pause");
}
这篇关于如何从程序开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文