2011-04-04 58 views
0

我有一個文件,每行一個記錄(名稱,名稱,ID號,等級,等級,等級,等級,等級,等級) I必須根據64位或更少的數字進行驗證:名稱,ID:9位和0 <等級< 100. 我想存儲在tempArray [9]中,然後存儲在realArray [9] [200]中。我認爲我的主要問題是我嘗試存儲時。數據沒有從tempArray []存儲到realArray [] []

tempArray在std::cerr << tempArray[i] <<std::endl;幾乎無處不在測試,它包含正確的數據。

但是realArray也進行了測試,只包含第一條記錄。我將realArray傳遞給以下函數,以便當我到達storeData時,可以將tempArray傳遞給realArray,並根據lineNumber使用列標記。

我知道有錯誤和編程「不要做的」大概TONNS,但我需要知道

1)如果我想要做的可以做 2)爲什麼我的realArray是隻獲得第一個記錄。

/添加後:我知道它沒有被存儲在realArray中,因爲我的增量變量是const int。但是爲什麼它接受存儲第一條記錄對我來說沒有意義。是否因爲行號被初始化爲零? 如果storeData第一次接受lineeNumber,爲什麼第二次不接受呢?/

代碼:

#include <iostream> 
#include <string> 
#include <sstream> 
#include <fstream> 
#include <iomanip> 
#include <stdlib.h> 
#include "A4prototypes.h" 
#include "search.h" 
#include "sort.h" 

using namespace std; 


void getFile(std::string realArray[][200], const int ROWS) 
{ 
std::string filename, line, token; 

int row(0);       
int lineNumber(0); 
const int MAX_RECORDS (200); 
const int TEMP_ROWS(9); 
const int ZERO(0); 
std::string tempArray[TEMP_ROWS]; 

std::cout << "Please enter the desired filename with it's extension:\t "; 
std::cin >> filename; 

std::ifstream input(filename.c_str(), std::ios::in); 

if (input.is_open()) 
{ 
    getline (input,line);      

    while (input.good() && lineNumber < MAX_RECORDS) 
    { 
    std::istringstream inputss (line);  

     while (getline(inputss, token, ',') && row < ROWS)    
     { 
      tempArray[row] = token;          

      row++; 
     } 

     row = ZERO; 

     /*I know I don't need to send the rows size of both of these arrays, but ... */ 

     validateData (lineNumber, tempArray, TEMP_ROWS, realArray, ROWS); 
     lineNumber++; 

     getline (input,line); 

    } 
} 
else 
{ 
    std::cout << "The file did not open correctly. \n\nPlease enter a valid filename.\n"; 
    } 

    if (lineNumber == MAX_RECORDS) 
    { 
    std::cout << "The maximum number of records to be read (" << MAX_RECORDS << ") has been reached.\n"; 
} 
} 

void validateData (int lineNumber, std::string tempArray[], const int ROW, std::string realArray[][200], const int ROWS) 
{  
    int j(0); 

//Validate Data functions... 


// Pass tempArray and realArray along with lineNumber to update realArray. 

storeData(lineNumber, tempArray, ROW, realArray ,ROWS); 

} 

int storeData(int record, std::string tempArray[], const int ROWS, std::string realArray[][200], const int ROW_SIZE) 
{ 
int k(0); 

std::string tempstr; 

record-=1; 


for (k; k < ROWS; k++) 
{ 
    tempstr = tempArray[k].data(); 

    realArray[k][record]=tempstr; 
} 

return 0; 
} 

int main() 
{ 
/* There should be a pointer here that gets sent to getFile and incriminates with the record line, gets sent to store data and the 
rest instead of just lineNumber,????...*/ 

int i(0), j(0);   
const int ROWS(9); 
const int COLUMNS(200); 

/* int * const rows = &ROWS; => It says in the book you can do this and pointer isn't const, but you could do *rows =10, which is what I want to 
do with the column, but it wont work... */ 

std::string realArray[ROWS][COLUMNS]={}; // Declare array for storing the data once it's been validated so I don't keep unecessary data. 


// Pass realArray to getFile so I can have access to it from main but it can be changed by getFile was the plan so fn's dont have to all be related to main. 

getFile(realArray,ROWS); 



return 0; 
} 

而這裏的頭文件

#ifndef _h 
#define _h 


void getFile(std::string [][200], const int); 

void validateData (int,std::string [], const int, std::string [][200], const int); 

int storeData(int, std::string [], const int, std::string [][200], const int); 

#endif 

回答

1

您的代碼「例如」包含大量代碼不直接帶動了問題(爲什麼數據不結束如預期的那樣在realArray)。在發佈之前減少代碼量可以讓您瞭解代碼的哪些部分是而非有錯誤,並且可能在您甚至不得不詢問之前發現了該錯誤。

更糟的是,你甩了500行代碼對我們,它甚至不編譯FThis.h丟失,複製所需的函數聲明爲例子本來微不足道的。)

一旦我固定聲明並添加#include <stdlib.h>所以我不明白沒有宣佈有關atoi()診斷,我仍然得到關於這樣的東西幾個警告...

char letterGrade('NR'); // character constants may only have *one* character 

...或...這

int i(0); 
int numOfArrayBox(5); 

// This is broken on several levels; chiefly, "i < NUM_OF_ASSIGNMENTS" will 
// never terminate the loop as it is followed by a comma, the effect of which 
// is apparently lost to you as you make this mistake in several places. 
for (i, numOfArrayBox; i < NUM_OF_ASSIGNMENTS, numOfArrayBox < MAX_NUM_OF_ARRAY_BOX; i++, numOfArrayBox++) 

提示:

for (int i = 0, int numOfArrayBox = 5; (i < NUM_OF_ASSIGNMENTS) && (numOfArrayBox < MAX_NUM_OF_ARRAY_BOX); ++i, ++numOfArrayBox) 

...在這一點,我不太願意花更多的時間調試代碼。嘗試Machete Debuggungasking questions the smart way

但我有一個一般提示給你:

這是C++,而不是C.在C++中,您不使用數組的數組,因爲您組織代碼和數據面向對象的

定義一個類(例如,class Student),其中包含數據(名稱,ID,等級),在構造函數中驗證數據,還包含對數據進行操作的函數(sumOfAssignments()等)。

而當你需要學生的集合,使用<vector>代替陣列:

#include <vector> 

// ... 
std::vector<Student> class; 
Student input("John", "Doe", 420, 64, 71, 89, 91, 88, 75); 
class.push_back(input);  

底線,你的問題是不是realArray不包含你想要的數據,你的問題是,你跳進深C++水仍然穿着(C)游泳。嘗試將你的例子縮減爲沒有警告的編譯,並清楚地顯示出單個問題,我們將能夠給你一個簡潔的答案。

+0

@Natlie:Yikes ...你不知道爲什麼頭文件是必要的,但你試圖讓一個不平凡的文件數據存儲程序運行?嗯...也許你應該重新考慮你選擇的學習材料。 (頭文件提供了函數和變量聲明,所以編譯器知道,例如'validateNames'是一個需要三個'int'作爲參數並返回一個'int'本身的函數。* *該函數在第一次調用之前。) – DevSolar 2011-04-04 07:08:46

+0

否, 我知道我知道 。 。 。太久了!那對不起,看起來很遲鈍。我實際上比我想象的要多得多。 。 。隨你。 PS這是土木工程必修課......我是唯一一個真正想要做到這一點的罪人之一......我正在努力! @其餘的,我編輯了代碼,拿出了無用的fn'並放入我的修改過的東西。 PPS還有一個用while循環獲取有效文件名的概率。如果輸入無效,循環不會停止!就好像ifstream不會將文件名更改爲新的輸入...? – Natlie 2011-04-04 07:31:14

+0

嗨,嗨,我想我們的老師試圖讓我們爲他找到我們自己的成績...... – Natlie 2011-04-04 07:36:45