How to start from beginning of the program

前端 未结 6 1728
灰色年华
灰色年华 2021-01-17 02:00

I am a very beginner in c++. i am just learning abc of this language.. i created this small program that would add:

#include 
   using namesp         


        
相关标签:
6条回答
  • 2021-01-17 02:06

    You can try putting the main part of your 'adding' in an endless loop. I suggest use a post condition loop, meaning one that will execute it's body at least once (then it will check the condition and so on), because you'll be wanting to add some numbers at least once.

    Example:

    do {
      // do stuff here
    } while (true) // always true condition -> makes the loop infinite
    

    So I guess you'll ask how do you stop this. You can ask the user if he wants to continue. Add this to the loop's body:

     int lock = 0;
     cout << "Do you want to continue? (0 = no, 1 = yes)" << endl;
     cin << lock;
     if (lock == 0) break; // stops the loop immeadiately
    

    You can do the same with lock being char with values 'y' or 'n'.

    0 讨论(0)
  • 2021-01-17 02:10

    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");    
    }
    
    0 讨论(0)
  • 2021-01-17 02:11

    Since you are starting, I am going to suggest changing your code a little bit:

    #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");    
    }
    

    Now, you can add various methods to call the core function repeatedly. One has been suggested in the answer by Rakibul Hassan.

    That can be implemented with:

    int main()
    {
       while (true)
       {
         read_input_print_sum();
       }
       system("pause");    
    }
    

    Another way: Ask the use whether they want do the work again.

    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");    
    }
    

    Another way: Ask the number of times they wish to repeat the computation before you start.

    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");    
    }
    
    0 讨论(0)
  • 2021-01-17 02:11

    Just call main(); again.. in your code will be like this:

    #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();
    }
    
    0 讨论(0)
  • 2021-01-17 02:18

    If we were to take "start from the beginning" literally, we can call main() again when we get to the end!

    #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()!!
    }
    

    (This will eventually crash when the program runs out of stack space, but the user will almost certainly get tired of adding numbers long before then. Of course, a clever compiler would realize this is tail recursion, and use a goto instead of a function call.)

    0 讨论(0)
  • 2021-01-17 02:20

    Here is how we do :

    #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;
    }
    
    0 讨论(0)
提交回复
热议问题