新自学C++的小白,有一道题目不会,求解

在主函数,从屏幕获取用户输入的消息,并缓存到一个缓冲区,缓冲区用字符数组来实现。

获取用户输入之前,先提示用户输入缓冲区的长度iLength,根据用iLength大小,开辟相应大小的缓冲区。获取用户输入后,再将缓冲区的内容打印到屏幕。(C++)

代码如下,如有帮助,请采纳一下,谢谢。

#include <iostream>
using namespace std;
void main()
{
	int iLength;
	char* buf;
	cout <<"请输入字符串长度:";
	cin >> iLength;

	buf = new char[iLength+1];
	memset(buf,0,iLength+1);
	cout << "请输入字符串:" ;
	cin >> buf;
	buf[iLength] = '\0';
	cout << buf << endl;
	delete[] buf;
	buf = 0;
}

 

#include "stdio.h"
int main(){
	int n;
	printf("请输入缓冲区大小:");
	scanf("%d",&n);
	char str[n]={'\n'};
	printf("请输入字符串");
	scanf("%s",&str);
	puts(str); 
	return 0;
} 

代码如上,万望采纳。

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632