mercredi 28 mai 2014

c ++ - debug : obtenir l'erreur en sortie de programme EAN - Stack Overflow


I've been debugging my EAN program for over a day and still cannot find where the error is that's making the program crash. I've so far whittled it down to one function, the strwithstyle function, as when it's not called the program runs fine(albeit with no style characters added). When called however, the program displays its output in a garbled mess and the program triggers a breakpoint. If anyone can tell me why this may be happening that would be of great help!


    void EAN::toStrWithStyle(char* str) const {
if (isRegistered()) {
if (choice == '-') {
sprintf(str, "%s-%s-%s-%s-%c", prefix, area, publisher, title, ean[strlen(ean) - 1]);
}
else if (choice == ' ') {
sprintf(str, "%s %s %s %s %c", prefix, area, publisher, title, ean[strlen(ean) - 1]);
}
else {
toStr(str);
}
}
}

Here is the display function(so you can get an idea of where the parameter str comes from):


    void EAN::display(std::ostream& os) const {
char* strmem = nullptr;
if (isRegistered()) {
strmem = new char[strlen(ean) + 5];
toStrWithStyle(strmem);
}
else {
strmem = new char[strlen(ean) + 1];
toStr(strmem);
}
os << right << setw(17) << strmem;
delete [] strmem;
strmem = nullptr;
}


I've been debugging my EAN program for over a day and still cannot find where the error is that's making the program crash. I've so far whittled it down to one function, the strwithstyle function, as when it's not called the program runs fine(albeit with no style characters added). When called however, the program displays its output in a garbled mess and the program triggers a breakpoint. If anyone can tell me why this may be happening that would be of great help!


    void EAN::toStrWithStyle(char* str) const {
if (isRegistered()) {
if (choice == '-') {
sprintf(str, "%s-%s-%s-%s-%c", prefix, area, publisher, title, ean[strlen(ean) - 1]);
}
else if (choice == ' ') {
sprintf(str, "%s %s %s %s %c", prefix, area, publisher, title, ean[strlen(ean) - 1]);
}
else {
toStr(str);
}
}
}

Here is the display function(so you can get an idea of where the parameter str comes from):


    void EAN::display(std::ostream& os) const {
char* strmem = nullptr;
if (isRegistered()) {
strmem = new char[strlen(ean) + 5];
toStrWithStyle(strmem);
}
else {
strmem = new char[strlen(ean) + 1];
toStr(strmem);
}
os << right << setw(17) << strmem;
delete [] strmem;
strmem = nullptr;
}

0 commentaires:

Enregistrer un commentaire