| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付赛孚耐:软件保护加密专家
身份认证令牌USB KEY   
共有 188 人关注过本帖
标题:[求助]关于动态内存+cin.get的问题
收藏  订阅  推荐  打印 
volvox
Rank: 1
等级:新手上路
帖子:2
积分:120
注册:2007-11-18
[求助]关于动态内存+cin.get的问题

本人是个C++新手,在国外打工,被可恶的BOSS赶鸭子上架编了一个小程序,可是好象运行不正常,请高手帮忙指点一下.

程序要求:
Ask the user how long their name is (问用户的名字是几个字母)
Create an array of chars in dynamic memory large enough to store the name
Ask the user to enter their name
Read the name into the array (assume it may include spaces)
Output the name
Delete the array from dynamic memory

程序如下:

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
int n;
char *a;
cout<<"How many character of your name ";
cin>>n;

int m=n+5;
a=new char[m];

cout<<"Pleas enter your name: ";
cin.get(a,m);//此处能编译但无法运行,程序运行到此便结束
cout << a;

delete[] a;
system("PAUSE");
return 0;
}



故障

见倒数第七行中的"//此处能编译但无法运行,程序运行到此便结束,有个朋友说是内存问题,但不知如何解决."

请高手帮忙看看是什么问题!!!不胜感激.

搜索更多相关主题的帖子: cin  内存  get  动态  
2007-11-18 13:29
光的赞歌
Rank: 1
等级:新手上路
帖子:17
积分:270
注册:2007-11-16

我没学好,不太懂,呵呵………………………………………………6
2007-11-18 13:41
xiaot1729
Rank: 1
等级:新手上路
帖子:3
积分:130
注册:2007-11-18
回复:(volvox)[求助]关于动态内存+cin.get的问题

你在 cin.get(a,m);//此处能编译但无法运行,程序运行到此便结束
之前加一句cin.getline(a,2);就可以了。要不程序会把"回车键"赋给a。
#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
int n;
char *a;
cout<<"How many character of your name ";
cin>>n;

int m=n+5;
a=new char[m];

cout<<"Pleas enter your name: ";
cin.getline(a,2);
cin.get(a,m);//此处能编译但无法运行,程序运行到此便结束

cout << a;

delete[] a;
system("PAUSE");
return 0;
}

2007-11-18 13:58
volvox
Rank: 1
等级:新手上路
帖子:2
积分:120
注册:2007-11-18

谢谢楼上的热心朋友,不过,我按以上所说试了一下,好象还是不行啊.直接就退出了.

:-(
2007-11-18 14:18
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.065654 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved