-2

I just made code that calculates the multiplication tables, based on user input. The problem is that after I print the results, the program closes and I need to relaunch it to get different input and results.

I want to get the results and press a key to get a new value from the user: I don't want to be constantly closing and opening the the program. How can I do this?
This is my code:

#include <iostream>

using namespace std;

int main() 
{
    int a, range;

    cout << "Introduce value: ";
    cin >> a;

    printf("\n");

    cout << "Introduce range: ";
    cin >> range;

    printf("\n");

    for (int i = 1; i <= range; ++i) 
    {
        cout << a << " * " << i << " = " << a*i << endl;
    }

    printf("\n");

    system("pause");
}
Tas
  • 7,023
  • 3
  • 36
  • 51
Victor
  • 1
  • 3
  • 5
    MS-DOS, eh? I don't want to be the one to break the news, but you are some 30 years late. – IInspectable Oct 20 '16 at 22:47
  • 1
    With MS-DOS, you mean the console window, right? – Angew is no longer proud of SO Oct 20 '16 at 22:54
  • I have [edited your question](http://stackoverflow.com/revisions/) to be hopefully more clear on what you are asking. If I have done so incorrectly, and you feel the question is different from what you are asking, you can [edit] the question to either rollback what I did or clarify further. I believe you are asking how to have a loop in your application so you don't need to open and close it. – Tas Oct 20 '16 at 22:57
  • If you want your program to query for user input multiple times, you should have code in there to do that. What do you think the problem is? – Red Alert Oct 20 '16 at 22:58
  • I'm just a beginner haha, okay now I know it's called "console window" thanks! – Victor Oct 21 '16 at 00:13

2 Answers2

1

Add something like while(1)

#include <iostream>

using namespace std;

int main() 
{

    while(1){
    int a, range;

    cout << "Introduce value: ";
    cin >> a;

    printf("\n");

    cout << "Introduce range: ";
    cin >> range;

    printf("\n");

    for (int i = 1; i <= range; ++i) 
    {
        cout << a << " * " << i << " = " << a*i << endl;
    }

    printf("\n");

    system("pause");
    }
}

Since the condition inside the while statement will always be true, your code here will loop forever!

Abozanona
  • 2,261
  • 1
  • 24
  • 60
1

If you want to press a key to determine if you want to continue with another value use the do while loop.

int main(void){
char c;
do{

   //......your code


   cout<<"Do you want to continue?"; // press 'y' if yes
   cin>>c;

}while(c=='y');

return 0;
}

Press 'y' to continue or anything else to stop. With this code you dont need system("Pause") at the end.

JPX
  • 93
  • 1
  • 4