Victor Victor - 1 month ago 8
C++ Question

How can I create a program that doesn't need to rerun to get new results?

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

Answer

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!

Comments