C++ Do-While逻辑正确但无法正确运行
问题描述:
代码很简单。用户指针接收用于在乘法表中显示的行数和列数的输入。我尝试使用Do-While循环来验证系统,但似乎不起作用。即使输入的值超出范围,它仍会接受并继续,因此会打印不正确的表格。C++ 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;
}
答
而不是column >! 0 && column <! 10
,请尝试column < 1 || column > 10
。类似于row
。找到some manner of documentation on C++ operators而不是猜测....
+0
这工作!非常感谢! – user2115635
你有奇怪的间距。 'row>! 0 &&行!0 && row immibis
而不是'column>! 0 &&列 10'。 –
@TonyD将它作为答案 – Barmar