PE14-1编程练习.exe 中的 0x58742258 (msvcp100d.dll) 处有未经处理的异常: 0xC0000005: 写入位置 0x00000

guangdongmutou 2011-01-05 04:28:52
程序能编译,但是运行的时候写入数据提示
PE14-1编程练习.exe 中的 0x58742258 (msvcp100d.dll) 处有未经处理的异常: 0xC0000005: 写入位置 0x00000

#include "stdafx.h"
#include <iostream>
#include <valarray>
#include <string>
#include <cstring>
using namespace std;

template <typename T1, typename T2>
class Pair
{
private:
T1 a;
T2 b;
public:
T1 first() { return a; }
T2 second() { return b; }
Pair (const T1 & t1, const T2 & t2) : a (t1), b (t2) { }
Pair () { }
};

class Wine
{
private:
typedef valarray<int> ArrayInt;
typedef Pair<ArrayInt, ArrayInt> PairArray;
string label;
int year;
PairArray pa;
public:
Wine (const char * l, int y, const int yr[], const int bot[])
: label (l), year (y), pa(ArrayInt(yr, year), ArrayInt(bot, year)) { }
Wine (const char * l, int y) : label (l), year (y) { }
void GetBottles();
string & Label() { return label; }
int Sum();
void Show();
};

void Wine::GetBottles()
{
cout << "Enter " << label << " data for " << year << " year(s):\n";
for (int i = 0; i < year; i++)
{
cout << "Enter year: ";
cin >> pa.first()[i];
cout << "Enter bottles for that year: ";
cin >> pa.first()[i];
}
}
void Wine::Show()
{
cout << "Wine: " << label << endl;
cout.width(4);
cout << "Year";
cout << " ";
cout.width(7);
cout << "Bottles" << endl;
for (int i = 0; i < year; i++)
{
cout.width(4);
cout << pa.first()[i];
cout << " ";
cout.width(7);
cout << pa.second()[i];
}
}
int Wine::Sum()
{
int sum = 0;
for (int i = 0; i < year; i++)
{
sum += pa.first()[i];
}
return sum;
}


int main()
{
cout << "Enter name of wine: ";
char lab[50];
cin.getline(lab, 50);
cout << "Enter number of years: ";
int yrs;
cin >> yrs;

Wine holding(lab, yrs); //stors label, years, give arrays yrs elements
holding.GetBottles();
holding.Show();

const int YRS = 3;
int y[YRS] = {1993, 1995, 1998};
int b[YRS] = {48, 60, 72};
//create new object, initialize using data in array;s ;y and b
Wine more("Gushing Grzpe Red", YRS, y, b);
more.Show();
cout << "Total bottles for " << more.Label() //use Label() method
<< ": " << more.Sum() << endl; //use sum() method
cout << "Bye\n";
return 0;
}
...全文
581 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyp568 2012-01-30
  • 打赏
  • 举报
回复
3L正解。。。
BruceLaroche 2011-10-11
  • 打赏
  • 举报
回复
什么错误呢?
guangdongmutou 2011-01-05
  • 打赏
  • 举报
回复 1
谢谢你
我找到错误了[Quote=引用 4 楼 luciferisnotsatan 的回复:]

引用 3 楼 luciferisnotsatan 的回复:

C/C++ code
char lab[50] = {0}; // 初始化下
cin.getline(lab, 50-1); // 至少留一个要给字符串结束符 \0

getline会自己补\0的,问题不在这。。。
[/Quote]
guangdongmutou 2011-01-05
  • 打赏
  • 举报
回复
up
求帮助
luciferisnotsatan 2011-01-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 luciferisnotsatan 的回复:]

C/C++ code
char lab[50] = {0}; // 初始化下
cin.getline(lab, 50-1); // 至少留一个要给字符串结束符 \0
[/Quote]
getline会自己补\0的,问题不在这。。。
luciferisnotsatan 2011-01-05
  • 打赏
  • 举报
回复
   char lab[50] = {0};  // 初始化下
cin.getline(lab, 50-1); // 至少留一个要给字符串结束符 \0

luciferisnotsatan 2011-01-05
  • 打赏
  • 举报
回复
0xC0000005是内存访问违规
单步调试下吧。注意指针,越界等问题

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧