简体   繁体   中英

Program exits at the end

I am learning C++ now, very new. I am trying to make a very simple program which show a multiplication tables, when program runs user enter a first number and then a 2nd one and program shows the table. But the problem is that when I press any key on keyboard the program exits. And I want that at this point program repeats itself and asks the user for the first number. My code is here:

#include <iostream>
#include <conio.h>
using namespace std;
int main(int argc,char**argv){
    int value1;
    int limit;
    int result1=1;
    int result2;
    bool runing=false;
    printf("Welcome \n");
    cout << "Please enter 1st value: " << flush;
    cin >> value1;
    cout << "Please enter a limit value: "<< flush;
    cin >> limit;
    cout<< "Result is: \n";
    while(result1<=limit){
        result2=result1*value1;
        printf("%d x %d = %d\n",value1,result1,result2);
        result1++;
    }
    return 0;
}

To do what you want, you just need another while loop that wraps everything after printing welcome. Like so:

#include <iostream>
#include <conio.h>
using namespace std;
int main(int argc,char**argv){
    int value1;
    int limit;
    int result2;
    bool runing=false;
    printf("Welcome \n");
    //I don't know in which conditions you want to quit the program.
    //You could also use for() instead, to run this piece of code a certain number of times.

    while(true){
        int result1=1;
        cout << "Please enter 1st value: " << flush;
        cin >> value1;
        cout << "Please enter a limit value: "<< flush;
        cin >> limit;
        cout<< "Result is: \n";
        while(result1<=limit){
            result2=result1*value1;
            printf("%d x %d = %d\n",value1,result1,result2);
            result1++;
        }
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM