I changed the second parameter to 25 as std100093 said, and that seemed to fix the problem. Also, thanks for the tip laserlight I changed my code accordingly.
Unfortunatley, now I've run into another problem...It's throwing this error "Unhandled exception at 0x778615de in Zip Code.exe: 0xC0000005: Access violation." It seems to be occurring at line 17 in main.
This is the code in my main.cpp file:
Code:
#include <iostream>
#include <iomanip>
#include "ZipCode.h"
using namespace std;
int main() {
ZipCode zip1(99504);
ZipCode zip2(12345);
ZipCode zip3(67890);
ZipCode zip4("100101010011100001100110001");
ZipCode zip5("110100001011100001100010011");
ZipCode zip6("100011000110101000011100101");
cout << "Digits" << " " << "Bar Code" << endl;
cout << zip1.getZipCode() << setw(35) << zip1.getBarCode() << endl;
cout << zip2.getZipCode() << setw(35) << zip2.getBarCode() << endl;
cout << zip3.getZipCode() << setw(35) << zip3.getBarCode() << endl;
cout << endl;
cout << zip4.getZipCode() << setw(35) << zip4.getBarCode() << endl;
cout << zip5.getZipCode() << setw(35) << zip5.getBarCode() << endl;
cout << zip6.getZipCode() << setw(35) << zip6.getBarCode() << endl;
return 0;
}
My header file for the ZipCode class:
Code:
#ifndef _ZIP_
#define _ZIP_
#include <iostream>
#include <string>
using namespace std;
class ZipCode {
private:
int zip;
int bar[26];
string barCode;
void barToZip(string barCode);
void zipToBar(int zip);
public:
ZipCode(int zip); //Constructor to initialize zip code
ZipCode(string barCode); //Constructor to initialize bar code
int getZipCode(); //Returns zip code
string getBarCode(); //Returns bar code
};
#endif
My ZipCode.cpp file:
Code:
#include <string>
#include "ZipCode.h"
ZipCode::ZipCode(int zip) : zip(zip) {
cout << "zip code constructor" << endl;
}
ZipCode::ZipCode(string barCode) {
cout << barCode << endl;
barToZip(barCode);
cout << "bar code Constructor" << endl;
}
int ZipCode::getZipCode() {
if (zip == 0)
barToZip(barCode);
return zip;
}
string ZipCode::getBarCode() {
if (barCode == "")
zipToBar(zip);
return barCode;
}
void ZipCode::barToZip(string barCode) {
barCode = barCode.substr(1,25);
int bar[25],
zipArr[5] = {0},
zip = 0,
*zipOne,
*zipTwo,
*zipThree,
*zipFour,
*zipFive;
char currentChar;
for(int i = 0; i < barCode.length(); i++) { //Converts string barCode to int array bar[]
currentChar = barCode.at(i); //Sets currentChar to the character in barCode at index i
bar[i] = (int)currentChar-'0';
};
zipOne = bar;
zipTwo = bar + 5;
zipThree = bar + 10;
zipFour = bar + 15;
zipFive = bar + 20;
zipArr[0] = (7 * zipOne[0]) + (4 * zipOne[1]) + (2 * zipOne[2]) + (1 * zipOne[3]); //Converts bar code numbers into zip code digit
zipArr[1] = (7 * zipTwo[0]) + (4 * zipTwo[1]) + (2 * zipTwo[2]) + (1 * zipTwo[3]);
zipArr[2] = (7 * zipThree[0]) + (4 * zipThree[1]) + (2 * zipThree[2]) + (1 * zipThree[3]);
zipArr[3] = (7 * zipFour[0]) + (4 * zipFour[1]) + (2 * zipFour[2]) + (1 * zipFour[3]);
zipArr[4] = (7 * zipFive[0]) + (4 * zipFive[1]) + (2 * zipFive[2]) + (1 * zipFive[3]);
for (int i = 0; i < 5; i++) { //Changes any zip digits that are 11 to 0
if (zipArr[i] == 11)
zipArr[i] = 0;
};
zip = 10000 * zipArr[0]; //Converts array into a single zip code integer
zip += 1000 * zipArr[1];
zip += 100 * zipArr[2];
zip += 10 * zipArr[3];
zip += 1 * zipArr[4];
cout << zip << endl;
}
void ZipCode::zipToBar(int zip) {
int zipArr[5];
string ZERO = "11000";
string ONE = "00011";
string TWO = "00101";
string THREE = "00110";
string FOUR = "01001";
string FIVE = "01010";
string SIX = "01100";
string SEVEN = "10001";
string EIGHT = "10010";
string NINE = "10100";
for (int i = 4; i >= 0; i++) {
zipArr[i] = zip % 10;
zip /= 10;
};
for (int i = 0; i < 5; i++) {
if (zipArr[i] = 0)
barCode += ZERO;
else if (zipArr[i] = 1)
barCode += ONE;
else if (zipArr[i] = 2)
barCode += TWO;
else if (zipArr[i] = 3)
barCode += THREE;
else if (zipArr[i] = 4)
barCode += FOUR;
else if (zipArr[i] = 5)
barCode += FIVE;
else if (zipArr[i] = 6)
barCode += SIX;
else if (zipArr[i] = 7)
barCode += SEVEN;
else if (zipArr[i] = 8)
barCode += EIGHT;
else if (zipArr[i] = 9)
barCode += NINE;
};
}
Not sure what this means...
Just for the record I'm new to C++ and need to practice using pointers, algorithms, etc. better; I'm sure all this code could be very much condensed and neater.
Wherever you see lines like
Code:
cout << "zip code constructor" << endl;
I was just checking to see how far the program had gone until throwing the error.