-8

I have made a "program" that just says welcome! Type two numbers you want to be added to each other: there you type the two numbers and then you get the answer out... When that is done it says: Press any key to continue . . . When you press a key the program shuts down, but I want it to restart when you pres any key... How do I do that? I use Microsoft visual studio express 2013 for windows desktop... langue is C++

This is my code:

 #include <iostream>
   #include <limits>
   #include <cstdio>
   using namespace std;

   int Add(int x, int y)
   {


        cout << "Calculating the sum of " << x << " + " << y << "\n";
        return (x + y);
    }

     int main()
     {
         cout << " Welcome!\n";
         int a, b, c;
         cout << "Type two numbers you want to be added to each other: ";
         cin >> a;
         cin >> b;
         c = Add(a, b);
         cout << "The answere is: " << c;
         cout << "\nShutting down....\n\n";
         system("pause");
         return 0;
    }
crashmstr
  • 28,043
  • 9
  • 61
  • 79

5 Answers5

3

To loop, you can use while.

For example:

while (false) {
    std::cout << "You will never see this output" << std::endl;
}

bool loop = true;
while (loop) {
    std::cout << "Type 'quit' to quit this loop." << std::endl;
    std::string input;

    // This will grab a *single word* from the input. If you want a line, look
    // at std::getline
    std::cin >> input;
    if (input == "quit") {
        loop = false;
    }
}

while (true) {
    std::cout << "This will be repeated forever" << std::endl;
}

There are also two other forms, do while:

std::string input;
do {
    std::cout << "Type 'quit' to quit." << std::endl;
    std::cin >> input;
} while (input != "quit");

... and for (which is generally used for loop over a defined list of things):

for (size_t i = 0; i < 10; ++i) {
    std::cout << i << " out of 10" << std::endl;
}

Technically you can use any of these loop types for any kind of looping, but I suspect the type you want is either one of the two standard infinite loops (whichever one you prefer):

while (true) {
    // stuff to repeat forever
}

for (;;) {
    // stuff to repeat forever
}

... or a do while loop similar to the do { ... } while (input != "quit"); loop above.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
0
     int main()
     {
         cout << " Welcome!\n";
         int a, b, c;
         while (true)
         {
             cout << "Type two numbers you want to be added to each other: ";
             cin >> a;
             cin >> b;
             c = Add(a, b);
             cout << "The answere is: " << c;
             cout << "\nPress a key to go again....\n\n";
             system("pause");
         };
         return 0;
    }
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

You can do something like this:

     int main()
     {
         cout << " Welcome!\n";
         int a, b, c;

         while(true) {
             cout << "Type two numbers you want to be added to each other: ";
             cin >> a;
             cin >> b;
             c = Add(a, b);
             cout << "The answere is: " << c;
             cout << "\nPress any key to continue\n";
             system("pause");
         }
         return 0;
    }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

Use a do while loop mentioning your condition before exiting the program so that you can determine when to continue and when to exit

user3256147
  • 378
  • 2
  • 9
0

I am not sure I understand your question, but I think this is what you are looking for. Have a boolean that determines if the program will loop or not.

int main() {
    // stillRun is true while we want to keep looping the program 
    boolean stillRun = true; 

    while(stillRun) { 
        runProgram() ; // this function has all the other code in your old main() function
        cin >> stillRun ; 
    }   
}   
gravitas
  • 703
  • 4
  • 16