我正在使用do-while循环通过istringstream从用户获取输入行。根据它获得的输入,程序应该调用共同响应函数。输入X退出循环。
do
{
getline(std::cin, input);
std::istringstream oss(input);
if (oss >> comm >> m >> n && comm=='I') /* if the input is I followed by
two integers M & N, create and MxN table using a two dimensional char vector */
{
table = tableCreate(comm, m, n, table);
tableDisplay(m, n, table);
}
std::istringstream oss2(input); /*if the input is F followed by two ints X,Y and char c,
change the table[y][x] element to c*/
if (oss2>>comm>>x>>y>>c && comm=='F')
{
table = changeElement(x, y, c, table);
tableDisplay(m, n, table);
}
} while (input != "X");
我不会通过发布函数定义来发布这篇文章太久,因为它们没有任何问题。我看到它的方式,问题在于istringstream无法区分M / N和X / Y用于tabledisplay(m,n,table);功能
输入的第一种情况是我按预期工作,但在第二种情况下,它看起来像tableDisplay函数接受输入流中的两个整数作为其参数,并显示一个X by Y维的表,同时完全忽略changeElement函数的结果。我尝试删除changeElement函数,它是一样的。我也试过保留changeElement但把tableDisplay放在if循环之外 - 什么都没有改变。
如何清楚我在第二种情况下输入的整数不被视为tableDisplay函数的参数?