如何从程序开始

How to start from beginning of the program

本文关键字:开始 程序      更新时间:2023-10-16

我是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();
}