gpt4 book ai didi

c++ - 程序最后退出

转载 作者:行者123 更新时间:2023-11-28 00:23:16 25 4
gpt4 key购买 nike

我现在正在学习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 = %d\n",value1,result1,result2);
result1++;
}
return 0;
}

最佳答案

要执行您想要的操作,您只需要另一个 while 循环,它在打印 welcome 之后包装所有内容。像这样:

#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;
}

关于c++ - 程序最后退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com