获取用户输入并将其存储到指针数组中

Getting User Input and storing it to a pointer array

本文关键字:指针 数组 存储 用户 输入 获取      更新时间:2023-10-16

我正在为我的编程项目创建一个零用金系统程序,它涉及我使用指针数组来模拟表。

我已经创建了一个部分,要求用户输入

其他数据以附加到我的当前表中,但是当它的可执行文件到达要求用户输入的部分时,它的可执行文件崩溃(在我的情况下,它将是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"的数组索引。