简体   繁体   English

程序最后退出

[英]Program exits at the end

I am learning C++ now, very new. 我现在正在学习C ++,非常新。 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. 要执行您想要的操作,您只需要另一个while循环,该循环将在欢迎打印后包装所有内容。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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