1

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.

Priyank
  • 341
  • 2
  • 11
  • @CinCout When I try to initialize info_book and sheet1 in the constructor, I get the error - info_book was not declared in the scope. – Priyank Dec 12 '18 at 06:03
  • @CinCout you were right. The function writeNum accpeted the third argument as double and not int. Made the change. But the execution still crashes. – Priyank Dec 12 '18 at 06:06
  • Here's the piece of code: – Priyank Dec 12 '18 at 06:07
  • 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"); //error: info_book was not declared in the scope – Priyank Dec 12 '18 at 06:10
  • @CinCout I don't have enough reputation to post a picture directly on my question buddy ! For the code, I am commenting inside my existing code. – Priyank Dec 12 '18 at 06:13
  • @CinCout Check it out now buddy – Priyank Dec 12 '18 at 06:26
  • Also did that. Added them to the member variables (I know how the basics work, just got out of my mind). Now, the program crashes with the same error the moment I execute it. – Priyank Dec 12 '18 at 06:33

1 Answers1

0

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 ....
    }

}
tunglt
  • 1,022
  • 1
  • 9
  • 16
  • cannot convert 'libxl::ISheetT::name' from type 'const char* (libxl::ISheetT::)() const' to type 'const wchar_t*'| – Priyank Dec 12 '18 at 15:49
  • @Priyank as you used only char (not windows wide char), I edited my answer in order to make it works with your application. – tunglt Dec 12 '18 at 16:15
  • @LightnessRacesinOrbit: Thanks for pointing that out. I edited my answer. – tunglt Dec 12 '18 at 16:55
  • @tunglt Thanks man - Compiles without any errors. The issue comes in execution - Just crashes incessantly. I will try using another IDE if it helps. – Priyank Dec 13 '18 at 02:19
  • @Priyank you are welcome. You should use a debuger to debug your program, it's a part of our developer life :) Your CodeBlock IDE comes with gdb, you should give it a try. – tunglt Dec 13 '18 at 08:33
  • I will certainly @tunglt. Thanks a lot once again for showing such an amazing support. People like you feed the courage in the souls of beginners :) – Priyank Dec 13 '18 at 13:44