c++ - 数据未从 tempArray[] 正确存储到 realArray[][]

标签 c++ loops argument-passing

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

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

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

我知道可能有成吨的错误和“不要做”的编程,但我需要知道

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

最佳答案

您的代码“示例”包含大量不会直接导致问题的代码(为什么数据没有按预期在 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", 420012345, 64, 71, 89, 91, 88, 75 );
class.push_back( input );    

最重要的是,您的问题与 realArray 无关不包含您想要的数据,您的问题是您仍然穿着 (C) 游泳衣跳入 C++ 深水。尝试将您的示例简化为在没有警告的情况下进行编译并清楚地显示单个问题的内容,我们将能够为您提供一个简洁的答案。

关于c++ - 数据未从 tempArray[] 正确存储到 realArray[][],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5533832/

相关文章:

C++ - 将项目添加到排序数组的最快方法

c++ - 检查值是否存在于数组的所有索引中

php - 准备好的语句迭代记录集

c++ - 为 STL 容器使用没有默认构造函数的仿函数

javascript - 扩展 jQuery 方法时如何传递参数?

以不显眼的方式为 fprintf 创建代理函数?

android - 将 tess-two(Tesseract Tools for Android)库集成到 Android 工作室并构建 ndk

c++ - 从多项式函数字符串动态分配数组

PHP循环遍历JSON多维数组并在单击按钮时加载数据

c++ - 用户空间(Linux)中是否有任何高分辨率时钟(us)?