我现在正在学习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;
}
答案 0 :(得分:3)
要做你想做的事,你只需要另一个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 = %d\n",value1,result1,result2);
result1++;
}
}
return 0;
}