代码很简单。用户指针用于接收要在乘法表中显示的行数和列数的输入。我尝试使用Do-While循环验证系统,但它似乎不起作用。即使输入的值超出范围,它仍然接受它并继续前进,从而打印出不正确的表格。
我该如何解决这个问题?我不想改变太多使用的方法,因为它现在的方式是干净和简单的。是否有一个简单的解决方法,或者我应该完全重做逻辑?
#include <iostream>
#include <iomanip>
using namespace std;
int passByRow(int *x);
int passByColumn(int *x);
int main()
{
int row = 0, column = 0;
int dummy;
do // Input Row
{
passByRow(&row);
} while (row >! 0 && row <! 10);
//while ((ans != 'Y') && (ans != 'N') && (ans != 'y') && (ans != 'n'));
do // Input Column
{
passByColumn(&column);
} while (column >! 0 && column <! 10);
// Display the table
cout << "\nTHE MULTIPLICATION TABLE:\n" << endl << "" << endl;
for (int c = 1; c < row+1; c++)
{
for (int i = 1; i < column+1; i++)
{
cout << i * c << '\t';
}
cout << endl;
}
cin >> dummy;
return 0;
}
int passByRow(int *x)
{
cout << "Please enter a number of rows on the interval [1, 10]: ";
cin >> *x;
return *x;
}
int passByColumn(int *x)
{
cout << "Please enter a number of columns on the interval [1, 10]: ";
cin >> *x;
return *x;
}