我认为分割错误与不正确使用内存有关。我检查了变量引用,但似乎无法发现给我分割错误的问题。我认为在函数2中添加其他字符时,可能与for循环中字符/字符串的无效使用有关。
//ISBN.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#include "ISBN.h"
//validates the isbn string
int isValid(const char str[]){
bool valid = false;
int sum = 0;
int i, j = 10;
if(str != NULL){
for(i=0; i<=9; i++){
sum += (str[i]-'0')*j;
//cout << "str[i]-'0' : " << (str[i]-'0')*j << endl;
//cout << "sum : " <<sum << endl;
j--;
}
//cout << sum << endl;
if(sum % 11 == 0){
valid = true;
}
}
return valid;
}
//checks if isbn is a registered number
int isRegistered(FILE* fp, const char str[], char area[], char publisher[], char title[]){
bool isregister = false;
bool farea = false;
bool fpub = false;
char areatmp[5] = {0};
char pubtmp[8] = {0};
char titletmp[7] = {0};
int len;
int lenpub;
int lentitle;
int areano;
int pospub, postitle;
for(int i=0; i < 5 && farea == 0; i++){
len = strlen(areatmp);
areatmp[len] = str[i];
areatmp[len+1] = '\0';
areano = atoi(areatmp);
farea = isRegistered(fp,areano);
pospub = i + 1;
}
strcpy(area,areatmp);
if(farea != false){
for(int i = pospub; i <= 8 && fpub == false; i++){
lenpub = strlen(pubtmp);
pubtmp[lenpub] = str[i];
pubtmp[lenpub+1] = '\0';
//cout << "---------------------------------------" << endl;
//cout << "pubtmpdigit : " << pubtmp << endl;
fpub = isRegistered(fp,atoi(area),pubtmp);
postitle = i + 1;
}
isregister = fpub;
for(int i = postitle; i <= 8; i++){
lentitle = strlen(titletmp);
titletmp[lentitle] = str[i];
titletmp[lentitle+1] = '\0';
}
}
strcpy(publisher, pubtmp);
strcpy(title, titletmp);
//cout << "title ---->" << titletmp << endl;
return isregister;
}