首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >回到指令上去

回到指令上去
EN

Stack Overflow用户
提问于 2019-09-27 09:58:52
回答 1查看 102关注 0票数 0

我在C++上写了一些代码,我说我是这门语言的新手。实际上,在if中给出的指令之后,我编写了一个字符串,指示用户编写一个新的输入。但是当它第二次插入变量时,程序就停止了。你知道我能做什么吗?啊,你也知道怎么在不需要按回车的情况下给用户添加字符吗?

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-27 10:39:07

试试看,我已经把你的如果改到了。

如果你在'D‘之后输入'A’,它会执行这两个循环。

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58132266

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档