2016-07-14 155 views
-3

我在C++中使用char命令缓冲区溢出时遇到问题,因为我是C++编码的新手。这是我的代码。我的问题在第七行。C++溢出缓冲区

#include "stdafx.h" 
#include <iostream> 
#include <cstdlib> 
int main() 
{ 
    char word[90]; 
    std::cout << "Type in your name to find out your gangster name!" << std::endl; 
    std::cin >> word; 
    std::cout << "Your gangster name is..." << std::endl; 
    std::cout << "Da" << word << std::endl; 
    system("pause"); 
} 

如何让变量由无限量的字母组成?

+2

为什么不改变'字符字[90]''到的std :: string word' –

+0

'char'是不是“命令”。 –

回答

3

您应该使用std::stringstd::getline在这种情况下

std::string word; 
std::getline(std::cin, word); 

http://www.cplusplus.com/reference/string/string/getline/

+0

啊,这个解决方案为我工作。 –

+0

为什么人们甚至使用字符?这看起来不安全。 –

+1

@AlexSummers:他们没有。它是。 –