开发者

Data isn't stored properly from tempArray[] to realArray[][]

I have a file with one record per line (name, name, id#, grade, grade, grade, grade, grade, grade) I have to validate according 64 digits or less : names, id:9 digits and 0 < grade < 100. I want to store in tempArray[9], validate then store in realArray[9][200]. I think my MAIN problem is when I try to store.

tempArray was tested pretty much everywhere with std::cerr << tempArray[i] <<std::endl; and it contains proper data.

BUT realArray also was tested and contains only the first record. I am passing realArray to the following functions so that when I get to storeData, I can transfer tempArray to realArray, with column marker according to lineNumber.

I KNOW THERE ARE PROBABLY TONNS of errors and programming "DON'T Do's", but I need to know

1) if what I'm trying to do can be done 2)Why my realArray is only getting the first record.

/Added After: I know that it's not being stored in realArray, because my increment variable is a const int. but then it doesn't make sense to me why it accepts to store the first record. is it because line Number is initialised to zero? if storeData accepts lineeNumber the first time, why doesnt it do so for the second?/

code:

#include <iostream>
#inclu开发者_如何转开发de <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;
}

And here's the header file

#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


Your code "example" contains tons of code not directly contributing to the problem (why data doesn't end up in realArray as expected). Reducing the amount of code before posting would have given you valuable insight into which parts of code are not faulty, and possibly uncovered the bug before you even had to ask.

Even worse, you dumped 500 lines of code on us and it does not even compile (FThis.h is missing, copying the required function declarations into the example would have been trivial.)

And once I fixed the declarations and added #include <stdlib.h> so I don't get diagnostics about atoi() not being declared, I still get several warnings regarding stuff like this...

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

...or this...

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++)

Hint:

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

...and at that point, I'm somewhat reluctant to spend more time debugging your code. Try Machete Debuggung and asking questions the smart way.

But I have a general hint for you:

This is C++, not C. In C++, you don't use arrays of arrays, because you organize your code and data object-oriented.

Define a class (say, class Student), which contains the data (name, ID, grades), does the validation of data in the constructor, and also contains the functions to operate on that data (sumOfAssignments() etc.).

And when you need a collection of students, use <vector> instead of arrays:

#include <vector>

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

Bottom line, your problem is not about realArray not containing the data you want, your problem is that you jumped into deep C++ water while still wearing (C) swimmies. Try reducing your example into something compiling without warnings and clearly exhibiting a single problem, and we will be able to give you a concise answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜