私は以前にデータで満たされたファイルを持っています。ファイルは構造体の配列で構成されています。各構造体はラウンドを表し、各配列の位置は個々のラウンドを20回まで表します。私の.hファイル:データファイルで変数howManyと同じ値を返し続けます
define READTWENTY_H
class readTwenty {
public:
readTwenty();
void nonZeroes(int, int &);
struct a_round {
int score;
double course_rating;
int slope;
char date[15];
char place[40];
char mark[1];
}; //end structure definition
struct a_round all_info[20];
FILE *fptr;
}; //end class
#endif
いくつかの「ラウンド」は彼らの実際のデータを持っているし、いくつかは、以前にゼロで満たされています。私はゼロラウンドを数えたいと思う。私は見るために別の "人"の価値を求めることができるループを持っています。この値は、ゼロラウンドの数が決定され、 "howMany"という名前の変数への参照によって返される関数に送られます。
// readMember.cpp : Defines the entry point for the console application.
//
かつて私が作成した「キー」としてnonzeroes機能に送ら#include "stdafx.h"
#include <iostream>
#include "readTwenty.h"
using namespace std;
int main()
{
int person = 0;
readTwenty personData;
int howMany = 0;
while (person != -999) {
cout << "Which member (keyfield) would you like to see? -999 to stop ";
cin >> person;
if (person == -999)
exit(0);
personData.nonZeroes(person-1, howMany);
cout << "The number of non-zero values for this member is " << howMany << endl;
}//end while
return 0;
}
ファイル内のオフセットおよびその個体のために20ラウンドを読み、参照することにより、バックカウントの値を返します呼び出しルーチンに変数howManyに渡します。
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "readTwenty.h"
#include <errno.h>
#include <cstdio>
readTwenty::readTwenty() {
const char *configfile;
configfile = "scores.dat";
#ifdef WIN32
errno_t err;
if((err = fopen_s(&fptr,configfile, "rb")) != 0)
#else
if ((fp_config = fopen(configfile, "rb")) == NULL)
#endif
fprintf(stderr, "Cannot open cinfig file %s!\n", configfile);
}//end constructor
void readTwenty::nonZeroes(int key, int &count) {
int zeroes = 0;
int offset = key * ((sizeof(all_info[0]) * 20));
fseek(fptr, offset, SEEK_SET);
for (int i = 0; i < 20; i++){
fread(&all_info[i], sizeof(all_info[0]), 1, fptr);
if (all_info[i].score == 0)
zeroes++;
all_info[i].mark[0] = ' ';
}//end for loop
count = 20 - zeroes;
fclose(fptr);
}//end of function nonZeroes
問題は、私が人に与える最初の価値が正しい数の非ゼロラウンドに戻ってしまうことです。しかし、私が人に与える第二の値にかかわらず、whileループの各反復は、最初の人と同じ結果を返します。あなたが持っている可能性のあるアイデアを大いに感謝します。
あなたは運が良かったです。お使いのコンピュータには、一度に1行ずつコードをステップ実行させ、すべての変数の値を調べることができる「デバッガ」というツールがあります。デバッガの助けを借りて、ループを踏んでいくうちに、すぐに問題を突き止めることができます。 –