无法cout char

时间:2016-08-16 21:27:54

标签: c++ arrays char cout cin

我无法理解这里的问题,所以我希望有些人可以提供帮助 我的程序开始就像这个int main():

    Board br;
    GetNames();
    br.PresentPlayer();

    system("PAUSE");
    return 0;

GetNames()看起来像这样:

char p[100];
char p2[100];
Board br;
cout << "Please Enter Player 1's Name:";
cin >> _player_name_1;
cout << "Please Enter " << _player_name_1 << "'s Symbol(Make sure its 1 characters long):";
cin >> p;
cout << "Please Enter Player 2's Name:";
cin >> _player_name_2;
cout << "Please Enter " << _player_name_2 << "'s Symbol(Make sure its 1 characters long):";
cin >> p2;
br.SetSymbols(p[0], p2[0]);

Board :: SetSymbols(char p,char p2)看起来像这样:

void Board::SetSymbols(char p1,char p2)
{
_player_char_1 = p1;
_player_char_2 = p2;
_current_player = _player_char_1;
cout << _current_player << endl;
}

变量在Board.h中:

char _player_char_1;
char _player_char_2;
char _current_player;

最后是PresentPlayer():

void Board::PresentPlayer()
{
cout << _current_player << endl;

}

因此,当执行SetSymbols时,它会正确打印_player_char_1 但是,当执行PresentPlayer时,它会打印出你要执行此操作的符号alt + numlock numbers

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:-5)

<body>

  <h1>Homework and Quiz average calculator</h1>

  <p>Please Enter the required information to calcuate</p>
  <div class="container" id="formDiv">
    <form id="formID">
      <p>
        <strong>Calculate the average of homework grades</strong>
      </p>
      How many grades?
      <input type="number" id="quantity" name="quantity" min="1" max="10" value="1">
      <!--<input onclick="setQuantity()" type="submit" value="Apply">-->
    </form>
    <button onclick="setQuantity()">APPLY</button>
    <br>
  </div>

  <script src="script.js"></script>

</body>

应该是

char _player_char_1;
char _player_char_2;
char _current_player;

或者,既然您正在使用C ++,使用char *_player_char_1; char *_player_char_2; char *_current_player; 停止(您没有为它们分配内存或检查边界),请改用char*。< / p>

std::string

需要

void Board::SetSymbols(char p1,char p2)

甚至更好

void Board::SetSymbols(char *p1,char *p2)