2016-02-20 115 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」的數組索引。

相關問題