获取用户输入并将其存储到指针数组中
Getting User Input and storing it to a pointer array
我正在为我的编程项目创建一个零用金系统程序,它涉及我使用指针数组来模拟表。
我已经创建了一个部分,要求用户输入其他数据以附加到我的当前表中,但是当它的可执行文件到达要求用户输入的部分时,它的可执行文件崩溃(在我的情况下,它将是getline(cin,my2dArrayPointerHere[][])
谁能指出我做错了什么?我也尝试使用常规的非指针字符串数组,但我仍然遇到同样的崩溃。
顺便说一句,我正在使用标准命名空间。也请忽略一些评论。这些是为我的小组成员准备的,他们可能对我的代码有疑问。
int numTrans;
char tempChar[1000];
double tempHolder;
string **tempDataHolder;
cout<<"Input number of transactions to add: ";
cin>>numTrans;
tempDataHolder = new string*[numTrans]; //pointer array
for(i=0;i<numTrans;i++)
tempDataHolder[i] = new string[col];
cout<<"nPlease input the following data as necessary: n";
for(ir=0; ir<numTrans;ir++)
{
cout<<"Date Requested: "; //This may seem unnecessary but some companies require paper
//documentation aside from a system to approve petty cash
//and sometimes the accounting staff has too much to do to
//perform data entry jobs in realtime such as this
getline(cin,tempDataHolder[ir][col]);
cout<<"Person Requesting Funds: ";
getline(cin,tempDataHolder[ir][col+1]);
cout<<"Person who approved request: ";
getline(cin,tempDataHolder[ir][col+2]);
cout<<"Amount Requested: Php. ";
cin>>tempHolder; //temp double number to properly format money
ostringstream convert;
convert<<tempHolder;
tempDataHolder[ir][col+3] = convert.str();
cout<<"Particulars: ";
getline(cin,tempDataHolder[ir][col+4]);
tempDataHolder[ir][col+5] = " "; //initialized to empty space because
tempDataHolder[ir][col+6] = " "; //data has not yet been retrieved
tempDataHolder[ir][col+7] = " "; //through liquidation
tempDataHolder[ir][col+8] = " ";
tempDataHolder[ir][col+9] = "false";
}
tableSize = deterSize(curFileName) + (numTrans*col); //this will ensure the next table will house all new elements as well
readCurFile(curFileName, tableSize);
displayCurTable(tableSize);
delete tempDataHolder;
你在 'tempDataHolder[i]=new string[col]' 中分配 'col' 字符串元素,但当你读取用户输入时,你会使用 'col'、'col+1' 等。这意味着您尝试写入大于"col"的数组索引。
相关文章:
- 指向指向字符数组的指针数组的指针
- 通过指向指针数组的指针访问子类的属性
- C++,指针数组,指向双链表中的条目
- 在C++中,如何初始化指向wchar_t*的指针数组(生成wchar_t**)
- C++从函数指针数组调用函数
- 关于指向指针数组的指针
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 将链表转换为指针数组时出错
- C++ 对象指针数组的复制构造函数
- C++ - 循环访问指针数组会导致错误
- 删除指针数组 (C++) 中的元素
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 将函数指针数组中的函数指针作为模板参数传递
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 具有推导参数的模板函数指针数组变量
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- C++指针数组到字符数组中的特定位置
- 如何在C++中复制指针数组的数据
- 初始化类中的指针数组,并在另一个类中检索它
- 尽管直接设置了指针数组,但仍为空