我在C++上写了一些代码,我说我是这门语言的新手。实际上,在if中给出的指令之后,我编写了一个字符串,指示用户编写一个新的输入。但是当它第二次插入变量时,程序就停止了。你知道我能做什么吗?啊,你也知道怎么在不需要按回车的情况下给用户添加字符吗?
#include <iostream>
using namespace std;
int main() {
string line1 = "OOOOOO";
string line2 = "OOIOOO";
string line3 = "OOOOOO";
string line4 = "OOOOOO";
char I = 'I';
char W = 'W';
char A = 'A';
char S = 'S';
char D = 'D';
char Input;
cout << line1 << endl;
cout << line2 << endl;
cout << line3 << endl;
cout << line4 << endl;
cin >> Input;
if (Input == D) {
size_t found = line2.find(I);
if (found != string::npos)
line2[found] = 'O';
line2[found + 1] = 'I';
system("cls");
cout << line1 << endl;
cout << line2 << endl;
cout << line3 << endl;
cout << line4 << endl;
cout << endl;
cout << "x: " << found << endl;
cin >> Input;
}
if (Input == A) {
size_t found = line2.find(I);
if (found != string::npos)
line2[found] = 'O';
line2[found - 1] = 'I';
system("cls");
cout << line1 << endl;
cout << line2 << endl;
cout << line3 << endl;
cout << line4 << endl;
cout << endl;
cout << "x: " << found << endl;
cin >> Input;
}
return 0;
}发布于 2019-09-27 10:39:07
试试看,我已经把你的如果改到了。
如果你在'D‘之后输入'A’,它会执行这两个循环。
#include <iostream>
using namespace std;
int main() {
string line1 = "OOOOOO";
string line2 = "OOIOOO";
string line3 = "OOOOOO";
string line4 = "OOOOOO";
char I = 'I';
char W = 'W';
char A = 'A';
char S = 'S';
char D = 'D';
char Input;
cout << line1 << endl;
cout << line2 << endl;
cout << line3 << endl;
cout << line4 << endl;
cin >> Input;
while (Input == D) {
size_t found = line2.find(I);
if (found != string::npos)
line2[found] = 'O';
line2[found + 1] = 'I';
system("cls");
cout << line1 << endl;
cout << line2 << endl;
cout << line3 << endl;
cout << line4 << endl;
cout << endl;
cout << "x: " << found << endl;
cin >> Input;
}
while (Input == A) {
size_t found = line2.find(I);
if (found != string::npos)
line2[found] = 'O';
line2[found - 1] = 'I';
system("cls");
cout << line1 << endl;
cout << line2 << endl;
cout << line3 << endl;
cout << line4 << endl;
cout << endl;
cout << "x: " << found << endl;
cin >> Input;
}
return 0;
}https://stackoverflow.com/questions/58132266
复制相似问题