hi everyone. i have a problem with my c++ program that i am making for my assignment. This is an ISBN asignment and basically The ISBNPrefix module simply reads the ISBN prefix table. This module contains all of the functions that are needed to access the table. Store the function prototypes in a header file named ISBNPrefix.h and your function definitions in a separate implementation file named ISBNPrefix.cpp. Store the function prototypes in a header file named ISBN.h and your function definitions in a separate implementation file named ISBN.cpp.
a sample run should be something like this:
ISBN Processor
==============ISBN (0 to quit) : 9070002043
Area : 90
Publisher : 70002
Title : 04ISBN (0 to quit) : 0003194875
Invalid check digit. Try again.ISBN (0 to quit) : 000319487
Incorrect number of digits. Try again.ISBN (0 to quit) : 0003194876
Area : 0
Publisher : 00
Title : 319487ISBN (0 to quit) : 9972000001
Area : 9972
Publisher : 00
Title : 000ISBN (0 to quit) : 9995500000
This ISBN does not have a registered prefixISBN (0 to quit) : 0
Signing off ...
anyway i have written my program but im getting an debug assertion error in c++ express and i got a seg fault in VI when i run the program. i have not been able to fix this issue but im suspecting the problem is from int valid(const char str[]) in ISBN.cpp or at least that is responsible for the seg fault. also i tried adding printfs to debug and i noticed that the close function never runs :/
there are 2 header files and 3 source files. a1test.cpp is provided by the teacher and cannot be altered. here are my codes:
/* Assignment 1 Test Main
* Bookstore Order Processor
*
* a1test.cpp
*
* Version 1.00
*
* Set DEBUG to 1 for verbose debugging output
* Reset DEBUG to 0 for submission version
*/
#define DEBUG 1 // Output control
#define PREFIX "prefixRanges.txt" // file of ISBN Prefix Ranges
#include <cstdio>
#include <cstring>
using namespace std;
#include "ISBN.h"
#include "ISBNPrefix.h"
bool prefixTests(FILE* fp, int& testNumber, int* totalPassed,
int* totalTested);
bool isbnTests(int& testNumber, int* totalPassed,
int* totalTested);
bool isbnPrefixTests(FILE* fp, int& testNumber, int* totalPassed,
int* totalTested);
void passed(const char* message, int i, int* totalPassed, int* total);
void failed(const char* message, int i, bool* ok, int* total);
int main() {
bool ok = true;
FILE* fp = NULL;
int totalPassed = 0, totalTested = 0, testNumber = 0;
// open the prefix file
fp = open(NULL);
if(fp != NULL) {
failed("open(NULL) did not return false", ++testNumber, &ok,
&totalTested);
} else
passed("open(NULL)", ++testNumber, &totalPassed, &totalTested);
fp = open(PREFIX);
if(fp == NULL) {
failed("open(PREFIX) did not open the prefix file", ++testNumber,
&ok, &totalTested);
} else
passed("open(PREFIX)", ++testNumber, &totalPassed, &totalTested);
// ISBNPrefix Tests
if (DEBUG)
printf("Prefix Tests\n============\n");
if(prefixTests(fp, testNumber, &totalPassed, &totalTested) != 0)
if (DEBUG)
printf("\nPrefix Tests passed!\n\n");
else
printf("Prefix Tests passed!\n");
else {
printf("\n");
if (DEBUG)
printf("Prefix Tests had errors!\n\n");
else
printf("Prefix Tests had errors!\n");
}
// ISBN Tests
if (DEBUG)
printf("ISBN Tests\n==========\n");
if(isbnTests(testNumber, &totalPassed, &totalTested) != 0)
if (DEBUG)
printf("\nISBN Tests passed!\n\n");
else
printf("ISBN Tests passed!\n");
else {
printf("\n");
printf("\nISBN Tests had errors!\n");
}
// ISBN Prefix Tests
if (DEBUG)
printf("ISBN/Prefix Tests\n=================\n");
if(isbnPrefixTests(fp, testNumber, &totalPassed, &totalTested) != 0)
if (DEBUG)
printf("\nISBN/Prefix Tests passed!\n\n");
else
printf("ISBN/Prefix Tests passed!\n");
else {
printf("\n");
printf("\nISBN/Prefix Tests had errors!\n");
}
// close the prefix file
if(close(NULL) != 0) {
failed("close(NULL) did not return false", ++testNumber, &ok,
&totalTested);
} else
passed("close(NULL)", ++testNumber, &totalPassed, &totalTested);
if(close(fp) == 0) {
failed("close(fp) did not close the prefix file", ++testNumber,
&ok, &totalTested);
} else
passed("close(fp)", ++testNumber, &totalPassed, &totalTested);
// Conclusion
printf("\nYour Bookstore modules have passed %d of %d tests\n",
totalPassed, totalTested);
if (totalPassed == totalTested)
printf("\nCongratulations!!! "
"Your Bookstore modules are ready for submission\n\n");
else
printf("Keep working on your code - good luck!\n");
return 0;
}
/* prefixTests performs tests on the ISBNPrefix class and return false
* for invalid results
* *totalPassed - total number of tests passed
* *totalTested - total number of tests attempted
*/
bool prefixTests(FILE* fp, int& testNumber, int* totalPassed,
int* totalTested) {
bool ok = true;
// test registeredA
if(registeredA(fp, -1) != 0) {
failed("registeredA(fp, -1) did not return false",
++testNumber, &ok, totalTested);
} else
passed("registeredA(fp, -1)", ++testNumber, totalPassed,
totalTested);
if(registeredA(fp, 99955) != 0) {
failed("registeredA(fp, 99955) did not return false",
++testNumber, &ok, totalTested);
} else
passed("registeredA(fp, 99955)", ++testNumber, totalPassed,
totalTested);
if(registeredA(fp, 0) == 0) {
failed("registeredA(fp, 0) did not return true", ++testNumber, &ok,
totalTested);
} else
passed("registeredA(fp, 0)", ++testNumber, totalPassed,
totalTested);
// test registerAP
if(registeredAP(fp, -1, "0") != 0) {
failed("registeredAP(fp, -1, \"0\") did not return false",
++testNumber, &ok, totalTested);
} else
passed("registeredAP(fp, -1, \"0\")", ++testNumber, totalPassed,
totalTested);
if(registeredAP(fp, 99955, "0") != 0) {
failed("registeredAP(fp, 99955, \"0\") did not return false",
++testNumber, &ok, totalTested);
} else
passed("registeredAP(fp, 99955, \"0\")", ++testNumber, totalPassed,
totalTested);
if(registeredAP(fp, 0, "10") == 0) {
failed("registeredAP(fp, 0, \"10\") did not return true", ++testNumber,
&ok, totalTested);
} else
passed("registeredAP(fp, 0, \"10\")", ++testNumber, totalPassed,
totalTested);
if(registeredAP(fp, 99955, "0") != 0) {
failed("registeredAP(fp, 99955, \"0\") did not return false",
++testNumber, &ok, totalTested);
} else
passed("registeredAP(fp, 99955, \"0\")", ++testNumber, totalPassed,
totalTested);
// test minNoDigits
if(minNoDigits(fp, -1) != 0) {
failed("minNoDigits(fp, -1) did not return 0", ++testNumber, &ok,
totalTested);
} else
passed("minNoDigits(fp, -1)", ++testNumber, totalPassed,
totalTested);
if(minNoDigits(fp, 0) != 2) {
failed("minNoDigits(fp, 0) did not return 2", ++testNumber, &ok,
totalTested);
} else
passed("minNoDigits(fp, 0)", ++testNumber, totalPassed,
totalTested);
if(minNoDigits(fp, 92) != 1) {
failed("minNoDigits(fp, 92) did not return 1", ++testNumber, &ok,
totalTested);
} else
passed("minNoDigits(fp, 92)", ++testNumber, totalPassed,
totalTested);
return ok;
}
/* isbnTests performs tests on the registered(const char*) function and
* returns false for invalid results
* *totalPassed - total number of tests passed
* *totalTested - total number of tests attempted
*/
bool isbnTests(int& testNumber, int* totalPassed, int* totalTested) {
bool ok = true;
// test valid
if(printf("if") | valid(NULL) != 0) {
printf ("Calling valid..\n");
failed("valid(NULL) did not return false", ++testNumber, &ok,
totalTested);
} else
passed("valid(NULL)", ++testNumber, totalPassed, totalTested);
if(valid("") != 0) {
failed("valid(\"\") did not return false", ++testNumber, &ok,
totalTested);
} else
passed("valid(\"\")", ++testNumber, totalPassed, totalTested);
if(valid("9070002043") == 0) {
failed("valid(\"9070002043\") did not return true", ++testNumber,
&ok, totalTested);
} else
passed("valid(\"9070002043\")", ++testNumber, totalPassed,
totalTested);
if(valid("9070002046") != 0) {
failed("valid(\"9070002046\") did not return false", ++testNumber,
&ok, totalTested);
} else
passed("valid(\"9070002046\")", ++testNumber, totalPassed,
totalTested);
return ok;
}
/* isbnPrefixTests performs tests on the registered() function and returns
* false for invalid results
* *totalPassed - total number of tests passed
* *totalTested - total number of tests attempted
*/
bool isbnPrefixTests(FILE* fp, int& testNumber, int* totalPassed,
int* totalTested) {
bool ok = true;
char area[6], publisher[8], title[7];
// test decode
if(decode(fp, "9070002043", area, publisher, title) == 0) {
failed("decode(fp, \"9070002043\", ...) did not return true",
++testNumber, &ok, totalTested);
} else
passed("decode(fp, \"9070002043\", ...)", ++testNumber,
totalPassed, totalTested);
if(decode(fp, "9995500000", area, publisher, title) != 0) {
failed("decode(fp, \"9995500000\", ...) did not return false",
++testNumber, &ok, totalTested);
} else
passed("decode(fp, \"9995500000\", ...)", ++testNumber,
totalPassed, totalTested);
char message[51], value[1121][11] = {
// THE LIST HAS BEEN SHORTENED HERE FOR TESTING PURPOSES THERE ARE HUNDREDS OF NUMBERS
"0000000000","0190000007","0200000004","0699000009","0700000003",
"0850000009","0899990002","0900000007","0949999008","0950000000",
"9994499904"};
for (int i = 0; i < 1121; i++) {
if(decode(fp, value[i], area, publisher, title) == 0) {
sprintf(message, "decode(fp, ...) did not return true "
"for %s", value[i]);
failed(message, ++testNumber, &ok, totalTested);
} else if (i%100 == 0) {
sprintf(message, "decode(fp, ...) returned true for %s",
value[i]);
passed(message, ++testNumber, totalPassed, totalTested);
} else {
passed("", ++testNumber, totalPassed, totalTested);
}
}
return ok;
}
/* passed displays passing message, increments *totalPassed and *total
* i holds test number
*/
void passed(const char* message, int i, int* totalPassed, int* total) {
if (DEBUG && message[0] != '\0')
printf("Passed test %4d (%s)\n", i, message);
++*totalPassed;
++*total;
}
/* failed displays failure message, resets *ok to false, increments *total
* i holds test number
*/
void failed(const char* message, int i, bool* ok, int* total) {
printf("Failed test %4d (%s)\n", i, message);
*ok = false;
++*total;
}
/*
ISBN.cpp
*/
#include "ISBN.h"
#include "ISBNPrefix.h"
using namespace std;
/*
int main() {
int keepgoing;
char isbn[11];
FILE* prefixFile = NULL;
char area[6], publisher[8], title[7];
cout << "ISBN Decoder\n"
"==============\n";
prefixFile = open(PREFIX_LIST);
if (prefixFile == NULL)
cout << "Could not open file " << PREFIX_LIST << endl;
else {
keepgoing = 1;
do {
cout << "\nISBN (0 to quit) : ";
cin.getline(isbn, 11);
if (!cin) {
cin.clear();
cin.ignore(2000, '\n');
cout << "Error. Try Again " << endl;
}
else if (strcmp(isbn, "0") == 0) {
keepgoing = 0;
}
else if (strlen(isbn) != 10) {
cout << " Incorrect number of digits. Try again." << endl;
}
else if (valid(isbn) == 0) {
cout << " Invalid check digit. Try again." << endl;
}
else if (decode(prefixFile, isbn, area, publisher, title) == 0) {
cout << " This ISBN does not have a registered prefix." << endl;
}
else {
cout << " Area : " << area << endl;
cout << " Publisher : " << publisher << endl;
cout << " Title : " << title << endl;
}
cout << endl;
} while (keepgoing == 1);
close(prefixFile);
cout << "Signing off ... " << endl;
}
return 0;
}
*/
int valid(const char str[])
{
printf("valid");
int len = strlen(str);
int a;
if (strlen(str) != 10){
// cout << " Incorrect number of digits. Try agian.";
return 0;
}
printf("lolcpp");
for(int i = 0; i < len-1; ++i){
if ( ! isdigit(str[i]) ) {
// cout << " Check the ISBN. Try again.";
return 0;
}
}
a=((str[0] - '0') * 10) + ((str[1] - '0') * 9) + ((str[2] - '0') * 8)+ ((str[3] - '0') * 7)+
((str[4] - '0') * 6) + ((str[5] - '0') * 5)+ ((str[6] - '0') * 4) + ((str[7] - '0') * 3) +
((str[8] - '0') * 2)+ ((str[9] - '0'));
if(a % 11 != 0){
// cout << " Invalid check digit. Try again.";
return 0;
}
return 1;
}
int decode(FILE* fp, const char str[], char area[], char publisher[], char title[]){
printf("lol1");
int j, k, check, found, marker, areaInt;
int i = 0;
int isbn[199];
do {
printf("lol2");
isbn[i] = str[i] - '0';
found = 0;
check = 0;
for (j = 0; j < i; j++){
isbn[j] = isbn[j]*10;
check = check + isbn[j];
}
check = check + isbn[j];
found = registeredA(fp, check);
i++;
} while ( (found == 0) && (i < 5) );
if (found == 1){
for (k = 0; k < i; k++) area[k] = str[k];
area[k] = '\0';
cout << endl;
areaInt = check;
check = 0;
marker = i;
for (j = 1; j <= minNoDigits(fp, areaInt); j++){
isbn[i] = str[i] - '0';
for (k = 0; k < (minNoDigits(fp, areaInt) - j); k++) isbn[i] = isbn[i]*10;
check = check + isbn[i];
i++;
}
char aaa[50000]={'\0'};
aaa[0]=static_cast<char>(check+static_cast<int>('0'));
do {
found = 0;
found = registeredAP(fp, areaInt, aaa);
if (found == 0){
check = 0;
for (j = marker; j < i; j++){
isbn[j] = isbn[j]*10;
check = check + isbn[j];
//cout << check << ' ';
}
isbn[i] = str[i] - '0';
//cout << endl;
check = check + isbn[i++];
//cout << check << endl;
}
} while ( (found == 0) && ( i < (marker + 7) ) );
}
if (found == 1){
for (k = marker; k < i; k++) publisher[k-marker] = str[k];
publisher[i-marker] = '\0';
marker = i;
for (k = marker; k < 9; k++) title[k-marker] = str[k];
title[9-marker] = '\0';
}
//cout << check;
return found;
}
/*
Assignment 1
ISBNPrefix.cpp
*/
#include "ISBNPrefix.h"
FILE* open(const char filename[]){
FILE* result = fopen(filename, "r");
if (result == NULL){
printf ("open ran");
return result;
}
}
int registeredA(FILE* fp, int area){
int found = 0;
int search;
rewind(fp);
do {
fscanf(fp, "%5d %*d %*d", &search);
if (area == search) found = 1;
} while ( (found == 0) && (feof(fp) == 0) );
printf("registeredA ran");
return found;
}
int minNoDigits(FILE* fp, int area){
int found = 0;
int i = 1;
int search;
char pub[8];
rewind(fp);
do {
fscanf(fp, "%5d %s %*d", &search, &pub);
if (area == search) found = 1;
} while ( (found == 0) && (feof(fp) == 0) );
if (found == 1) i = strlen(pub);
printf("minNoDigits ran");
return i;
}
int registeredAP(FILE* fp, int area, const char publisher[]){
int found = 0;
int search, min, max;
rewind(fp);
do {
fscanf(fp, "%5d %7d %7d", &search, &min, &max);
if (area == search) {
if ( (min <=(int) publisher) && ((int)publisher <= max) ) found = 1;
}
} while ((found == 0) && (feof(fp) == 0) );
printf ("registeredAP ran");
return found;
}
int close(FILE* fp){
printf("close start");
int success = 0;
if (fp != NULL){
fclose(fp);
success = 1;
}
printf("close ran");
return success;
}
//isbn.h
#ifndef __ISBN
#define __ISBN
#include <iostream>
#include <iomanip>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
int valid(const char str[]);
int decode(FILE* fp, const char str[], char area[], char publisher[], char title[]);
#endif
//isbnprefix.h
#ifndef __ISBN
#define __ISBN
#include <iostream>
#include <iomanip>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
int valid(const char str[]);
int decode(FILE* fp, const char str[], char area[], char publisher[], char title[]);
#endif
the debug error log:
'asignment1.exe': Loaded 'C:\Users\hasd\Documents\Visual Studio 2010\Projects\asignment1\Debug\asignment1.exe', Symbols loaded.
'asignment1.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'asignment1.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
'asignment1.exe': Loaded 'C:\Windows\System32\user32.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\gdi32.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\lpk.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\usp10.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\imm32.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\msctf.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\advapi32.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\sechost.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\uxtheme.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Program Files\McAfee\SiteAdvisor\sahook.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\dwmapi.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\ole32.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\cryptbase.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\clbcatq.dll', Cannot find or open the PDB file
'asignment1.exe': Loaded 'C:\Windows\System32\oleaut32.dll', Cannot find or open the PDB file
Microsoft Visual Studio C Runtime Library has detected a fatal error in asignment1.exe.
Press Break to debug the program or Continue to terminate the program.
The program '[4808] asignment1.exe: Native' has exited with code -1073740777 (0xc0000417).
and here is the output in VI:
:~> g++ a1test.cpp ISBNPrefix.cpp ISBN.cpp
:~> a.out
open ranPassed test 1 (open(NULL))
Passed test 2 (open(PREFIX))
Prefix Tests
============
registeredA ranPassed test 3 (registeredA(fp, -1))
registeredA ranPassed test 4 (registeredA(fp, 99955))
registeredA ranPassed test 5 (registeredA(fp, 0))
registeredAP ranPassed test 6 (registeredAP(fp, -1, "0"))
registeredAP ranPassed test 7 (registeredAP(fp, 99955, "0"))
registeredAP ranFailed test 8 (registeredAP(fp, 0, "10") did not return true)
registeredAP ranPassed test 9 (registeredAP(fp, 99955, "0"))
minNoDigits ranFailed test 10 (minNoDigits(fp, -1) did not return 0)
minNoDigits ranPassed test 11 (minNoDigits(fp, 0))
minNoDigits ranPassed test 12 (minNoDigits(fp, 92))Prefix Tests had errors!
ISBN Tests
==========
Segmentation fault
:~>
any help would be really appreciated in solving this problem. thanks.