C/C++如何整行读入字符串

如题所述

运行结果:

在 C 中可以使用 gets() , 在C++ 中一般使用 cin.getline(), getline(),代码如下:

#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <string>

using namespace std;

int main()
{
char str[100];

// C 中使用
gets(str);
printf("%s\n", str);

// C++ 使用
cin.getline(str, 100);
cout << str << endl;

string line;
getline(cin, line);
cout << line << endl;

system("pause");
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-05-30
int a[100];
c ① scanf("%s",&a); getchar()//吸收回车键\n
②gets(a);
c++ ①cin>>a;
第2个回答  2018-05-30
用gets函数即可
相似回答