[英]Data isn't stored properly from tempArray[] to realArray[][]
我有一个文件,每行一个记录(名称,名称,ID#,等级,等级,等级,等级,等级,等级),我必须根据64位或更少的位数进行验证:名称,ID:9位且0 <等级< 100.我想存储在tempArray [9]中,先验证然后存储在realArray [9] [200]中。 我认为我的主要问题是当我尝试存储时。
tempArray在所有地方都使用std::cerr << tempArray[i] <<std::endl;
进行了几乎所有测试std::cerr << tempArray[i] <<std::endl;
它包含适当的数据。
但是,realArray也经过了测试,仅包含第一条记录。 我将realArray传递给以下函数,以便当我进入storeData时,可以根据行号使用列标记将tempArray传输到realArray。
我知道错误和编程“不要”的可能性很大,但我需要知道
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
预期出现在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 Debuggung并以聪明的方式提问 。
但我对您有一个总体提示:
这是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 ++水。 尝试将您的示例简化为没有警告的汇编内容,并清楚地显示出单个问题,我们将为您提供简洁的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.