Search code examples
c++libxl

Error in executing code of C++ (CodeBlocks) when using LibXL


I am trying to execute this code for my Mini project using a third party library called LibXL[for C++] on CodeBlocks.

#include "libxl.h"
#include <iostream>
#include<string>
#include<tchar.h>

#ifdef _UNICODE
typedef WCHAR TCHAR;
#else
typedef char TCHAR;
#endif


using namespace libxl;
using namespace std;
    //Ignore this block of comments //Specifically for CinCout 
/*
class Student_Information
{
    string First_Name, Last_Name,Mobile, Course;
    double bd_day, bd_month, bd_year,Gr_No;

   // Format* format1 = info_book->addFormat();


public:

Student_Information()
{

Book* info_book = xlCreateBook();
Sheet* sheet1 = info_book->addSheet("Sheet1");


}
    void insert_info()
    {
        int num;
        info_book->load("student_information.xls");
}*/

class Student_Information
{
    string First_Name, Last_Name,Mobile, Course;
    int bd_day, bd_month, bd_year,Gr_No;
     Book* info_book = xlCreateBook();  //Throws are warning "non-static data member initializers only available with -std=c++11 or -std=gnu++11"
    Sheet* sheet1 = info_book->addSheet("Sheet1"); //Throws the same warning even over here


public:

    void insert_info()
    {
        int num;
        info_book->load("student_information.xls");

        do
        {
            cout<<"Enter 1 to add a record | Enter 0 to exit";
            cin>>num;

            cout<<"Please enter the GR Number of the Student: ";
            cin>>Gr_No;
            sheet1->writeNum(sheet1->lastRow(),0,Gr_No);

            cout<<"Please Enter your FIRST NAME [In Capitals]: ";
            cin>>First_Name;
            sheet1->writeStr(sheet1->lastRow(),1,First_Name.c_str());

            cout<<"Please Enter your LAST NAME [In Capitals]: ";
            cin>>Last_Name;

            sheet1->writeStr(sheet1->lastRow(),2,Last_Name.c_str());

            sheet1->writeStr(sheet1->lastRow(),3,"BCA");

            cout<<"Please enter your 10 Digit mobile number: ";
            cin>>Mobile;

            sheet1->writeStr(sheet1->lastRow(),4,Mobile.c_str());

            info_book->save("student_information.xls");
        }
        while(num!=0);
       // info_book->load("student_information.xls");


       // info_book->release();

    }
};

int main()
{
    Student_Information ob1;
    ob1.insert_info();
}

When I try to compile the code, I get two warnings which I mentioned in the code, but the compiler shows no ERRORS

When the program is executed, the program asks for 2 options. When the user chooses option 1, the program asks for GR number. After entering the GR number, it crashes displaying this message on screen :

enter image description here

I am pretty confused about this error. I tried cleaning the Project and rebuilding it but doesn't help. I am using Codeblocks with the Encoder UTF-8 & GNU GCC COMPILER.

Apologies if I offended the Geeks of the 21st Century by mentioning things inappropriately. I am quite unfamiliar with LibXL and additionally not so much familiar to coding in general.


Solution

  • After the book is loaded by load() function, its old contents were deleted and were replaced by the new ones loaded from the file, the sheet1 will point to something that is not valid and makes the program crashed.

    A quick solution is to find the new Sheet1 value each time you load a book as :

    Sheet* getSheetByName(Book* book, const TCHAR * name)
    {
       for(unsigned short i = 0; i < book->sheetCount(); ++i)
       {
           if(_tcscmp(book->getSheet(i)->name(), name) == 0)
           {
               return book->getSheet(i);
           }
       }
       return NULL;
    }
    
    void insert_info()
    {
        int num;
        info_book->load("student_information.xls");
        sheet1 = getSheetByName(info_book, "Sheet1");
    
        if( sheet1 == NULL) { //"Sheet1" not found, add a new one.
            sheet1 = info_book->addSheet("Sheet1");
        }
    
        do
        {
            //insert data to sheet1 ....
        }
    
    }