0

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

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.

please tell me

thanks.

anni
  • 290
  • 1
  • 3
  • 15
  • 3
    Think about the code you want to repeat, then wrap only that code in `while(true) { }`. – Dai Apr 27 '14 at 04:21
  • `what conditions i should use` Whatever you want. @Dai already suggests looping forever, which is the simplest. You might want to loop a certain number of times. That number can be fixed at compile time, or maybe input by the user before he starts inputting numbers. Or you could loop until the user enters a non-numeric value. Or you could loop until the cows home or the stars align. It's entirely up to you. – Nicu Stiurca Apr 27 '14 at 04:25
  • This is not a tutorial website ! Why not look at [these](http://stackoverflow.com/q/388242/1870232) ? – P0W Apr 27 '14 at 04:32

6 Answers6

3

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;
}
amarVashishth
  • 847
  • 3
  • 12
  • 26
2

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.)

Nicu Stiurca
  • 8,747
  • 8
  • 40
  • 48
  • can i write `return main();` on the top of `system("pause");`? – anni Apr 29 '14 at 13:54
  • 1
    @atti Sure! But then you might as well delete `system("pause");` because you won't ever get to a line that occurs after a `return` statement. – Nicu Stiurca Apr 30 '14 at 01:07
  • Calling `main()` in C++ is not allowed. This is also terrible advice as it adds to the limited stack. A `while` loop would be the correct way. – Jeffrey Aug 11 '22 at 16:34
  • TIL that you cannot call `main()`. Practically though, it does seem like compilers do allow it by default; as for the stack, this is a trivial case of tail recursion which the compiler can optimize away and not actually create a new stack frame with every iteration. – Nicu Stiurca Jan 19 '23 at 15:59
2

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'.

2

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");    
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • you know every one is writing `while( true )` but can you tell me what is `true` here? or what is `true` is being used for? – anni Apr 29 '14 at 14:00
  • 1
    The block of code in the `while` loop is executed repeatedly as long as the expression in its `()` is true. When you use `while(true)`, you run the `while` loop forever. The only way to get out of such a loop is by using a `break;` statement. – R Sahu Apr 29 '14 at 15:18
1

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");    
}
Rakib
  • 7,435
  • 7
  • 29
  • 45
0

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