标签:std 数组 i++ stdin 申请 getc space for etc
法一:
#include <iostream>
#include <string>
using namespace std;
void main()
{
    char test[100]; // 定义够长的数组空间
    for(int i=0;i<100;i++) test[i]=‘\0‘;
    cin.getline(test,100); // 整行读取(包括空格)
    cout<<test<<endl;
    
}
法二:
#include <iostream>
#include <string>
using namespace std;
int main()
{
  cout << "输入字符串的长度:" << endl;
  int num;  //你要输入字符串的长度
  cin >> num;
  getc(stdin);  //去掉输入num的换行符
  char * p = new char [num + 1];  //动态申请你要输入字符串的长度
  memset(p, 0, num+1);   //申请的空间初始化为0
  gets(p);  
  *(p+num) = ‘\0‘;  //以‘\0‘结尾
  cout << p << endl;
  delete[] p;  //释放内存
  system("pause");
  return 0;
}
标签:std 数组 i++ stdin 申请 getc space for etc
原文地址:http://www.cnblogs.com/L-N-C-D/p/6009046.html