如何从程序开始
How to start from beginning of the program
我是c++的初学者。我只是在学习这门语言的abc。。我创建了这个小程序,它将添加:
#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++的应用程序
现在我想,当有人想再次添加时,这个程序会再次启动。。。我想到了使用循环,但我无法思考如何以这种方式使用。我的意思是我应该使用什么条件。
请告诉我
谢谢。
以下是我们的做法:
#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<<"nSecond number: ";
cin>>num2;
cout<< "your sum is: "<<add(num1, num2)<<endl;
char ch = 'n';
cout << "Start Again, [y/n] ? ";
cin >> ch;
if (ch == 'Y' || ch == 'y')
continue;
else
break;
}
return 0;
}
如果我们按照字面意思"从头开始",我们可以在到达结尾时再次调用main()
!
#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");
return main(); // <---- starts again from beginning of main()!!
}
(当程序耗尽堆栈空间时,这最终会崩溃,但用户几乎肯定早在那之前就厌倦了添加数字。当然,聪明的编译器会意识到这是尾部递归,并使用goto
而不是函数调用。)
您可以尝试将"添加"的主要部分放入一个无休止的循环中。我建议使用后条件循环,这意味着它将至少执行一次它的主体(然后它将检查条件等等),因为你将希望至少添加一些数字一次。
示例:
do {
// do stuff here
} while (true) // always true condition -> makes the loop infinite
所以我想你会问如何阻止这种情况。您可以询问用户是否要继续。将此添加到循环的主体:
int lock = 0;
cout << "Do you want to continue? (0 = no, 1 = yes)" << endl;
cin << lock;
if (lock == 0) break; // stops the loop immeadiately
您也可以使用值为"y"或"n"的char来执行同样的操作。
既然您正在开始,我建议您稍微更改一下代码:
#include <iostream>
using namespace std;
float add(float a, float b)
{
return a+b;
}
// Function that does the core work.
void read_input_print_sum()
{
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;
}
int main()
{
read_input_print_sum();
system("pause");
}
现在,您可以添加各种方法来重复调用核心函数。拉基布尔·哈桑在答复中提出了一个建议。
这可以通过以下方式实现:
int main()
{
while (true)
{
read_input_print_sum();
}
system("pause");
}
另一种方法:询问使用者是否想再次做这项工作。
bool getRepeat()
{
cout << "Do you want to repeat? (Y/N): ";
int yesno = cin.getc();
return ( yesno == 'Y' || yesno == 'y' );
}
int main()
{
bool repeat = true;
while (repeat)
{
read_input_print_sum();
repeat = getRepeat();
}
system("pause");
}
另一种方法:在你开始之前,询问他们希望重复计算的次数。
int main()
{
int N = 0;
cout << "How may times do you want to add numbers: ";
cin >> N;
for ( int i = 0; i <= N; ++i )
{
read_input_print_sum();
}
system("pause");
}
以下代码将执行此操作:
#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");
}
以上代码将永远运行。如果您想给用户一个选择,那么应用循环中断条件。
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");
}
只需调用main();再一次在你的代码中会是这样的:
#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;
main();
}
相关文章:
- C++程序从主程序开始执行并在主程序结束?
- 从头开始为应用程序创建 docker 映像是否有意义?
- 正在尝试循环回到开始以再次运行我的程序
- 为什么当我输入大量数字时,我的计算器程序开始闪烁和滚动
- 从头开始为使用Qt的应用程序制作简约的构建文件
- 无论如何,是否可以确定MFC应用程序是通过PC重新启动或用户单击应用程序快捷方式开始的
- G :内部编译器错误:分割故障(程序CC1PLUS) - 我在哪里开始
- 循环,使程序从头开始
- C++开始程序
- 如何在应用程序开始时破坏堆栈
- 我的COCOS2D游戏开始,而不是其他音乐应用程序停止
- 在我关闭刚开始的孩子对话框并进入父母对话框之后,我的QT桌面应用程序不断崩溃
- 在程序开始时填充数组
- 从头开始创建C 背景Win32应用程序
- 在程序开始时设置的C 内存语义和全球
- 嗨,我需要帮助构建具有特定要求的程序.我已经开始,但只需要帮助完成即可
- 使用C++在Windows 8中不固定到"开始屏幕"的应用程序快捷方式
- 如何在c/c++中从不同的函数而不是main()开始执行程序
- 从java程序开始c++程序
- 需要帮助开始c++程序