Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    embedded sql with C help..segmentation fault?

    when i run my code i keep getting a segmentation fault, do u see anything wrong? it's embedded sql with C

    code--------------------------
    if (menu == 5) {



    EXEC SQL SELECT sysdate
    into :timestamp
    from dual;

    temp2 = atoi(timestamp);

    printf("\n date is %i", temp2);

    printf("\nenter book id: ");
    fgets(temp, 11, stdin);
    m_bookid = temp;
    printf("\nbook id is %s",m_bookid);

    printf("\nenter branch id: ");
    fgets(temp3, 11, stdin);
    m_branchid = temp3;
    printf("\nbranch id is %s", m_branchid);

    EXEC SQL DECLARE book_loan CURSOR FOR
    SELECT DueDate
    FROM BOOK_LOANS
    WHERE BookId = :m_bookid;


    EXEC SQL OPEN book_loan;

    EXEC SQL SELECT DueDate
    INTO m_duedate
    FROM BOOK_LOANS;



    temp4 = atoi(m_duedate);




    if(temp2 <= temp4) {

    printf("\nreturn ok--no fine due\n");
    goto BEGIN;

    } else if (temp2 >= temp4) {
    fines = temp2 - temp4;

    if(fines <=99) {
    printf("\nfine --$%i-- owed by user id %s\n", fines, uid);

    } else if (fines < 100) {
    printf("\nfine --$99-- owed by user id %s", uid);
    }

    }



    ------------runtime-------------
    bash-2.03$ ! menu2
    MAIN MENU
    1. search for title
    2. search for author
    3. get borrower info
    4. process book loan
    5. process book return
    6. quit

    which one would you like to do?: 5

    date is 1075576832
    enter book id: 1234567890
    Segmentation Fault
    bash-2.03$


    vaiables--------
    EXEC SQL BEGIN DECLARE SECTION;
    char uid[20];
    int menu;
    int menu2;
    char m_bookid;
    char m_branchid;
    char m_dateout;
    char m_duedate;
    exec sql end declare section;
    EXEC SQL INCLUDE SQLCA;

    main() {
    char ans[3];
    int fines;
    char temp[21];
    float temp2;
    char temp3[21];
    float temp4;
    char timestamp[11];

    sorry it's long...i've just been stuck for a while...i also know the date's wrong but that's how i want it to come out as

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You should run your code in a debugger to find problems like this.

    The runtime output you posted gives a good clue where to look. One thing I see is you are using the %s printf format specifier to convert a char variable. This will alway cause a crash. With a char variable you should be using the %c format specifier instead. Fixing that will solve the crash problem, but you should rething your methods here. Why would you try to assign a character array to a single char variable? The result will most likely just be 0. You should assign it to a char pointer instead.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •