从tempArray []到realArray [] []的数据存储不正确

时间:2011-04-04 01:47:21

标签: c++ loops argument-passing

我有一个文件,每行一条记录(姓名,姓名,身份,年级,年级,年级,年级,年级,等级) 我必须根据64位或更少的数字进行验证:名称,id:9位数和0<等级< 100。 我想存储在tempArray [9]中,然后验证然后存储在realArray [9] [200]中。我认为我的主要问题是当我试图存储时。

tempArray几乎在所有地方都使用std::cerr << tempArray[i] <<std::endl;进行了测试,它包含了适当的数据。

但是realArray也经过测试,只包含第一条记录。我将realArray传递给以下函数,这样当我到达storeData时,我可以根据lineNumber将tempArray传递给realArray,并使用列标记。

我知道有可能出现错误并编程“不要做”,但我需要知道

1)如果我想做的事情可以做到 2)为什么我的realArray只获得第一张唱片。

/ 添加After:我知道它没有存储在realArray中,因为我的增量变量是const int。但是对我来说,为什么它接受存储第一条记录是没有意义的。是因为行号初始化为零? 如果storeData第一次接受lineeNumber,为什么第二次不接受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 个答案:

答案 0 :(得分: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,等级),在构造函数中验证数据,还包含对该数据进行操作的函数({{ 1}}等。)。

当您需要一组学生时,请使用sumOfAssignments()而不是数组:

<vector>

最重要的是,你的问题不在于#include <vector> // ... std::vector<Student> class; Student input( "John", "Doe", 420012345, 64, 71, 89, 91, 88, 75 ); class.push_back( input ); 不包含你想要的数据,你的问题是你在穿着(C)游泳时跳入了深层C ++水中。尝试将您的示例简化为无需警告的编译,并清楚地展示单个问题,我们将能够为您提供简明的答案。