2016-02-20 72 views
0

我正在为编程中的项目创建一个小额现金基金系统程序,它涉及到使用指针数组来模拟表。获取用户输入并将其存储到指针数组

我创建了一个部分,用户被要求输入额外的数据来追加到我当前的表中,但是当我的可执行文件到达需要输入用户的部分时崩溃了(在我的情况下,它会是getline(cin,my2dArrayPointerHere [] []))

任何人都可以指向我做错了什么吗?我也尝试使用常规的非指针字符串数组,但我仍然遇到相同的崩溃。

我使用std命名空间btw。也请忽略一些评论。这些适用于我的部分代码可能有问题的我的同事。

   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; 
+0

不知道如果老师真的在教C++,而不是C,可以节省多少时间和头痛! – iksemyonov

+0

在第一维中,您在范围'[0,numTrans]'中进行迭代,这与分配的大小一致。在第二维中,你只在'[col,col + 9]'范围内迭代,而你只分配了'[0,col]'。这是为什么?可能是坠机的原因。 – iksemyonov

+0

Omg。我明白了为什么......呃。在这里,我想也许我做了错误的语法,但它是合乎逻辑的!我被教导总是在索引中使用变量,我忘记了我不需要列索引。多谢你们! – Kobowo

回答

0

你分配在 'COL' 字符串元素 'tempDataHolder [I] =新的字符串[COL]',但是当你读用户输入,可以使用 'COL', 'COL + 1' 等这意味着您尝试写入大于“col”的数组索引。

相关问题