编写 cin.get() 以接收字符数组时出现问题



我正在尝试用C++编写一个半 exel 控制台应用程序。 我写了一个while循环,我试图用cin.get()接收来自用户的命令,然后将其放入字符数组中。 但是当我写cin.get(str, 10, 'n');时,它进入了一个无限循环。

我需要做什么来解决这个问题?

while (true) {
cout << "select your choice : n" << "1-number  2-commandn";
cin >> n;
switch (n) {
case 1:
cout << "enter row " << x + 1 << " calum : " << y + 1 << ": ";
cin >> arr[x][y];
system("cls");
print(arr);
continue;
case 2:
cout << "enter your command : ";
cin.get(str, 10, 'n');
if (str[0] == 'g') {
for (char i = 'a'; i <= 't'; i++) {
if (str[6] == i) {
switch (i) {
case 'a': y = 0; break;
case 'b': y = 1; break;
case 'c': y = 2; break;
case 'd': y = 3; break;
case 'e': y = 4; break;
case 'f': y = 5; break;
case 'g': y = 6; break;
case 'h': y = 7; break;
case 'i': y = 8; break;
case 'j': y = 9; break;
case 'k': y = 10; break;
case 'l': y = 11; break;
case 'm': y = 12; break;
case 'n': y = 13; break;
case '0': y = 14; break;
case 'p': y = 15; break;
case 'q': y = 16; break;
case 'r': y = 17; break;
case 's': y = 18; break;
case 't': y = 19; break;
}
}
else
continue;
}
for (char i = '1'; i <= '20'; i++) {
if (str[7] == i)
x = int(i) - 49;
}
system("cls");
print(arr);
continue;
}

}

while 循环将始终为真,因为这就是您设置条件的原因。您可能希望创建一个可以为 false 的布尔语句,以便您可以退出循环,或者可能想要创建一个 break 语句。

最新更新