程序在结束时退出

Program exits at the end

本文关键字:退出 结束 程序      更新时间:2023-10-16

我现在正在学习C++,非常新。我正在尝试制作一个非常简单的程序,它显示乘法表,当程序运行时,用户输入第一个数字,然后输入第二个数字,程序显示该表。但问题是,当我按下键盘上的任何键时,程序都会退出。我希望在这一点上,程序会重复它自己,并要求用户输入第一个数字。我的代码在这里:

#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 = %dn",value1,result1,result2);
        result1++;
    }
    return 0;
}

要想做什么,只需要另一个while循环,在打印欢迎之后包装所有内容。像这样:

#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 = %dn",value1,result1,result2);
            result1++;
        }
    }
    return 0;
}