8 Replies - 548 Views - Last Post: 11 May 2013 - 09:56 PM Rate Topic: -----

#1 ShankarLall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-April 13

Programme Crash without error

Posted 09 May 2013 - 05:12 PM

Not sure what happening with my code.
every time I try to add a customer it will crash.
I am getting some warning about missing braces which i dont really understand.


#include <stdio.h>
#include <stdlib.h>

//Customer Structure
struct customerStr
{
    int accNum;
    char fName[150];
    char lName [150];
    char gender[5];
    double balance;
};

//Transaction Structure
struct transactionStr
{
    int accNum;
    char fName[150];
    char lName [150];
    char details [3];
    double balance;
};

//Customer Function
void addCustomer(FILE *fPtr);
void updateCustomer(FILE *fPtr);
void displayCustomer(FILE *fPtr);
void deleteCustomer(FILE *fPtr);

int openfile(char filename[], FILE *fPtr);

//Transaction Function
void transactionD (FILE *fPtr);
void transactionW (FILE *fPtr);
void transactionP (FILE *fPtr);


void mainMenu(void);


int main()
{
    FILE *custometFPtr;
    FILE *transactionFPtr;

    char custometFile [50] = "customer.dat";
    char transactionFile [50]= "tranaction.dat";

    mainMenu();
    int selection;
    scanf("%d", &selection);

    while(selection !=0)
    {
        switch(selection)
        {
        case 1: //add customer
            openfile(custometFile, custometFPtr);

            addCustomer(custometFPtr);

            fclose(custometFPtr);

            system("cls");
            mainMenu();
            scanf("%d", &selection);
            break;

        case 2: // update
            openfile(custometFile, custometFPtr);

            updateCustomer(custometFPtr);

            fclose(custometFPtr);

            system("cls");
            mainMenu();
            scanf("%d", &selection);
            break;

        case 3://display
            openfile(custometFile, custometFPtr);
            openfile(transactionFile, transactionFPtr);

            displayCustomer(custometFPtr);
            transactionW (transactionFPtr);

            fclose(custometFPtr);
            fclose(transactionFPtr);

            system("cls");
            mainMenu();
            scanf("%d", &selection);
            break;

        case 4://delete
            openfile(custometFile, custometFPtr);

            deleteCustomer(custometFPtr);

            fclose(custometFPtr);

            system("cls");
            mainMenu();
            scanf("%d", &selection);
            break;

        case 5://deposit
            openfile(custometFile, custometFPtr);
            openfile(transactionFile, transactionFPtr);

            transactionD(transactionFPtr);
            updateCustomer(custometFPtr);


            fclose(custometFPtr);
            fclose(transactionFPtr);

            system("cls");
            mainMenu();
            scanf("%d", &selection);
            break;

        case 6://withdraw
            openfile(custometFile, custometFPtr);
            openfile(transactionFile, transactionFPtr);

            transactionW(transactionFPtr);
            updateCustomer(custometFPtr);

            fclose(custometFPtr);
            fclose(transactionFPtr);

            system("cls");
            mainMenu();
            scanf("%d", &selection);
            break;

        default:
            system("cls");
            printf("INVALID SELECTION \n");
            mainMenu();
            scanf("%d", &selection);

            break;

        }

    }

}

void mainMenu(void)
{
    printf("\n\tMAIN MENU\n");
    printf("\t1.   Add\n");
    printf("\t2.   Update\n");
    printf("\t3.   Display\n");
    printf("\t4.   Delete\n\n");
    printf("\t5.   Deposit\n");
    printf("\t6.   Withdraw\n");
    printf("\t0.   EXIT\n");
    printf("\nEnter Your Selection:\n");
}


int openfile(char filename[], FILE *fPtr)
{
    if ( ( fPtr = fopen(filename, "wb" ) ) == NULL )
        printf( "File could not be opened\n" );

}

void addCustomer(FILE *fPtr)
{
    struct customerStr info = {0, " ", " ", 0, " ", 0.0};
    int accNum;
    printf("?Account Number: ");
    scanf("%d", &accNum);
    printf("0000");

    fseek( fPtr, ( accNum - 1 ) * sizeof( struct customerStr ), SEEK_SET );
    fread( &info, sizeof( struct customerStr ), 1, fPtr );
    if (info.accNum != 0)
    {
        printf("\nDuplicated Account Number\n");
    }
    else
    {
        info.accNum = accNum;
        printf("?First Name: ");
        scanf( "%s", &info.fName);
        printf("??Last Name: ");
        scanf("%s", &info.lName);
        printf("?Gender: ");
        scanf("%s", &info.gender);
        printf("Starting Amount: ");
        scanf("%lf", &info.balance);

        fseek(fPtr, (info.accNum -1) *sizeof (struct customerStr), SEEK_SET);
        fwrite (&info, sizeof (struct customerStr), 1, fPtr );

        printf("\nCustomer Added\n");

    }
}
void updateCustomer(FILE *fPtr)
{
    struct customerStr info = {0, "", "", 0, "", 0};
    int accNum;
    printf("Account Number: ");
    scanf("%d", &accNum);

    fseek( fPtr, ( accNum - 1 ) * sizeof( struct customerStr ), SEEK_SET );
    fread( &info, sizeof( struct customerStr ), 1, fPtr );
    if (info.accNum == 0)
    {
        printf("\nInvalid Account Number\n");
    }
    else
    {
        printf("?First Name: ");
        scanf( "%s", &info.fName);
        printf("??Last Name: ");
        scanf("%s", &info.lName);
        printf("?Gender: ");
        scanf("%s", &info.gender);
        printf("Starting Amount: ");
        scanf("%lf", &info.balance);

        fseek(fPtr, (info.accNum -1) *sizeof (struct customerStr), SEEK_SET);
        fwrite (&info, sizeof (struct customerStr), 1, fPtr );

        printf("Customer Updated Completed\n");
    }

}
void displayCustomer(FILE *fPtr)
{
    struct customerStr info = {0, "", "", 0, "", 0};
    int accNum;
    printf("?Account Number: ");
    scanf("%d", &accNum);

    fseek( fPtr, ( accNum - 1 ) * sizeof( struct customerStr ), SEEK_SET );
    fread( &info, sizeof( struct customerStr ), 1, fPtr );
    if (info.accNum == accNum)
    {
        printf("Account #\t%d\n", info.accNum);
        printf("Full Name\t%s  %s\n", info.fName, info.lName);
        printf("Gender\t%s\n", info.gender);
        printf("Balance\t%2.lf\n", info.balance);
    }
    else
    {
        printf("\nInvalid Account Number\n");
    }

}
void deleteCustomer(FILE *fPtr)
{
    struct customerStr info = {0, "", "", 0, "", 0};
    int accNum;
    printf("?Account Number: ");
    scanf("%d", &accNum);

    fseek( fPtr, ( accNum - 1 ) * sizeof( struct customerStr ), SEEK_SET );
    fread( &info, sizeof( struct customerStr ), 1, fPtr );
    if (info.accNum == 0)
    {
        printf("\nInvalid Account Number\n");
    }
    else
    {
        fseek(fPtr, (info.accNum -1) *sizeof (struct customerStr), SEEK_SET);
        fwrite (&info, sizeof (struct customerStr), 1, fPtr );
    }

}
void transactionD (FILE *fPtr)
{
    struct customerStr info = {0, "", "", 0, "", 0};
    int accNum;
    double deposit;
    printf("Enter Customer Account#: ");
    scanf("%d", &accNum);

    fseek( fPtr, ( accNum - 1 ) * sizeof( struct customerStr ), SEEK_SET );
    fread( &info, sizeof( struct customerStr ), 1, fPtr );
    if (info.accNum == accNum)
    {
        printf("Customer Name\t%s %s\n", info.fName, info.lName);
        printf("Customer Current Balance\t%2.lf\n", info.balance);
        printf("Enter Deposited Amount:\n");
        scanf("%lf", &deposit);
        info.balance += deposit;
        printf("New Balance is %2.lf", info.balance);

        //update customer balance
        fseek(fPtr, (info.accNum -1) *sizeof (struct customerStr), SEEK_SET);
        fwrite (&info, sizeof (struct customerStr), 1, fPtr );

        struct transactionStr transactionDetails = {0, "", "", "", 0};

        info.accNum = transactionDetails.accNum;
        info.fName == transactionDetails.fName;
        info.lName == transactionDetails.lName;
        char details = "+";
        details == transactionDetails.details;
        info.balance = transactionDetails.balance;

        FILE *cfPtr;

        if((cfPtr = fopen ("tranaction.dat", "r+"))==NULL)
            printf("File Error!!\n");
        else
        {
            fwrite (&transactionDetails, sizeof (struct transactionStr), 1, cfPtr );
        }
        fclose(cfPtr);
    }
    else
    {
        printf("\nInvalid Account Number\n");
    }

}
void transactionW (FILE *fPtr)
{
    struct customerStr info = {0, "", "", 0, "", 0};
    struct transactionStr transactionDetails = {0, "", "", "", 0};
    int accNum;
    double withdraw;
    printf("Enter Customer Account#: ");
    scanf("%d", &accNum);

    fseek( fPtr, ( accNum - 1 ) * sizeof( struct customerStr ), SEEK_SET );
    fread( &info, sizeof( struct customerStr ), 1, fPtr );
    if (info.accNum == accNum)
    {
        printf("Customer Name\t%s %s\n", info.fName, info.lName);
        printf("Customer Current Balance\t%2.lf\n", info.balance);
        printf("Enter Deposited Amount:\n");
        scanf("%lf", &withdraw);
        if(info.balance >= withdraw)
        {
            info.balance -= withdraw;
            printf("New Balance is %2.lf", info.balance);

            //update customer balance
            fseek(fPtr, (info.accNum -1) *sizeof (struct customerStr), SEEK_SET);
            fwrite (&info, sizeof (struct customerStr), 1, fPtr );

            struct transactionStr transactionDetails = {0, "", "", "", 0};

            info.accNum = transactionDetails.accNum;
            info.fName == transactionDetails.fName;
            info.lName == transactionDetails.lName;
            char details = "-";
            details == transactionDetails.details;
            info.balance = transactionDetails.balance;

            FILE *cfPtr;

            if((cfPtr = fopen ("tranaction.dat", "r+"))==NULL)
                printf("File Error!!\n");
            else
            {
                fwrite (&transactionDetails, sizeof (struct transactionStr), 1, cfPtr );

                fclose(cfPtr);
            }
        }
        else
        {
            printf("\nInadequate Funds!!!\n");
        }

    }
    else
    {
        printf("\nInvalid Account Number\n");
    }

}
void transactionP (FILE *fPtr)
{
    struct transactionStr transactionDetails = {0, "", "", "", 0};
    int accNum;
    scanf("%d", &accNum);

    accNum = transactionDetails.accNum;
    while ( !feof( fPtr ) )
    {
        fread( &transactionDetails, sizeof( struct transactionStr ), 1, fPtr );

        if(transactionDetails.accNum !=0)
        {
            printf("%-10d\t %-25s %-25s %-5s %7.2f\n",
                   transactionDetails.accNum, transactionDetails.lName, transactionDetails.fName,
                   transactionDetails.details, transactionDetails.balance);
        }
        else
        {
            printf("\nInvalid Account #\n");
        }
    }

}








Warning Massage

main.c||In function 'addCustomer':|
main.c|176|warning: missing braces around initializer [-Wmissing-braces]|
main.c|176|warning: (near initialization for 'info.gender') [-Wmissing-braces]|
main.c|176|warning: initialization makes integer from pointer without a cast [enabled by default]|
main.c|176|warning: (near initialization for 'info.gender[1]') [enabled by default]|
main.c|192|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[150]' [-Wformat]|
main.c|194|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[150]' [-Wformat]|
main.c|196|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[5]' [-Wformat]|
main.c||In function 'updateCustomer':|
main.c|209|warning: missing braces around initializer [-Wmissing-braces]|
main.c|209|warning: (near initialization for 'info.gender') [-Wmissing-braces]|
main.c|209|warning: initialization makes integer from pointer without a cast [enabled by default]|
main.c|209|warning: (near initialization for 'info.gender[1]') [enabled by default]|
main.c|223|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[150]' [-Wformat]|
main.c|225|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[150]' [-Wformat]|
main.c|227|warning: format '%s' expects argument of type 'char *', but argument 2 has type 'char (*)[5]' [-Wformat]|
main.c||In function 'displayCustomer':|
main.c|240|warning: missing braces around initializer [-Wmissing-braces]|
main.c|240|warning: (near initialization for 'info.gender') [-Wmissing-braces]|
main.c|240|warning: initialization makes integer from pointer without a cast [enabled by default]|
main.c|240|warning: (near initialization for 'info.gender[1]') [enabled by default]|
main.c||In function 'deleteCustomer':|
main.c|262|warning: missing braces around initializer [-Wmissing-braces]|
main.c|262|warning: (near initialization for 'info.gender') [-Wmissing-braces]|
main.c|262|warning: initialization makes integer from pointer without a cast [enabled by default]|
main.c|262|warning: (near initialization for 'info.gender[1]') [enabled by default]|
main.c||In function 'transactionD':|
main.c|282|warning: missing braces around initializer [-Wmissing-braces]|
main.c|282|warning: (near initialization for 'info.gender') [-Wmissing-braces]|
main.c|282|warning: initialization makes integer from pointer without a cast [enabled by default]|
main.c|282|warning: (near initialization for 'info.gender[1]') [enabled by default]|
main.c|306|warning: statement with no effect [-Wunused-value]|
main.c|307|warning: statement with no effect [-Wunused-value]|
main.c|308|warning: initialization makes integer from pointer without a cast [enabled by default]|
main.c|309|warning: comparison between pointer and integer [enabled by default]|
main.c|309|warning: statement with no effect [-Wunused-value]|
main.c||In function 'transactionW':|
main.c|330|warning: missing braces around initializer [-Wmissing-braces]|
main.c|330|warning: (near initialization for 'info.gender') [-Wmissing-braces]|
main.c|330|warning: initialization makes integer from pointer without a cast [enabled by default]|
main.c|330|warning: (near initialization for 'info.gender[1]') [enabled by default]|
main.c|357|warning: statement with no effect [-Wunused-value]|
main.c|358|warning: statement with no effect [-Wunused-value]|
main.c|359|warning: initialization makes integer from pointer without a cast [enabled by default]|
main.c|360|warning: comparison between pointer and integer [enabled by default]|
main.c|360|warning: statement with no effect [-Wunused-value]|
main.c|331|warning: unused variable 'transactionDetails' [-Wunused-variable]|
main.c||In function 'openfile':|
main.c|172|warning: control reaches end of non-void function [-Wreturn-type]|
main.c||In function 'main':|
main.c|151|warning: control reaches end of non-void function [-Wreturn-type]|
main.c|58|warning: 'custometFPtr' may be used uninitialized in this function [-Wuninitialized]|
main.c|83|warning: 'transactionFPtr' may be used uninitialized in this function [-Wuninitialized]|
||=== Build finished: 0 errors, 45 warnings (0 minutes, 0 seconds) ===|




Is This A Good Question/Topic? 0
  • +

Replies To: Programme Crash without error

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,467
  • Joined: 05-May 12

Re: Programme Crash without error

Posted 09 May 2013 - 08:38 PM

Look at the number of members in your struct and compare against the number of literals you have between your braces on line 176. Do you see any discrepancy?
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Programme Crash without error

Posted 09 May 2013 - 08:55 PM

It also looks like you need to study the documentation for scanf(). Remember the address of a C-string is the variable name, you don't need an ampersand. You should also never use scanf() for a C-string without specifying the maximum number of characters it will accept. When studying the documentation for scanf() be sure to pay close attention to the width modifier.

Jim

This post has been edited by jimblumberg: 09 May 2013 - 08:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Programme Crash without error

Posted 09 May 2013 - 11:38 PM

Did you not try and compile your code periodically while writing it? Warnings are not something you can just sweep under the rug, you need to address each one while you are writing your code. Take a look at what scanf accepts as it's first parameter, then look at the parameters you are passing to it.
Was This Post Helpful? 0
  • +
  • -

#5 ShankarLall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-April 13

Re: Programme Crash without error

Posted 10 May 2013 - 09:03 PM

Ok I saw I had one too many variable in my structure.

But it still crash after input account number.
there is something about line 26 and 27 which causing it to crash.


struct customerStr
{
    int accNum;
    char fName[75];
    char lName [75];
    char gender[15];
    double balance;
};

void addCustomer(FILE *fPtr);

int main()
{

FILE *custometFPtr;
 addCustomer(custometFPtr);
}

void addCustomer(FILE *fPtr)
{
    struct customerStr info = {0, "", "", "", 0.0};
    int accNum;
    printf("?Account Number: ");
    scanf("%d", &accNum);

    fseek( fPtr, ( accNum - 1 ) * sizeof( struct customerStr ), SEEK_SET );
    fread( &info, sizeof( struct customerStr ), 1, fPtr );

    if (info.accNum != 0)
    {
        printf("\nDuplicated Account Number\n");
    }
    else
    {
        printf("?First Name: ");
        scanf("%s", &info.fName);
        printf("??Last Name: ");
        scanf("%s", &info.lName);
        printf("?Gender: ");
        scanf("%s", &info.gender);
        printf("Starting Amount: ");
        scanf("%lf", &info.balance);

        info.accNum = accNum;

        fseek(fPtr, (info.accNum -1) *sizeof (struct customerStr), SEEK_SET);
        fwrite (&info, sizeof (struct customerStr), 1, fPtr );

        printf("\nCustomer Added\n");

    }
}



Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,439
  • Joined: 25-December 09

Re: Programme Crash without error

Posted 10 May 2013 - 09:52 PM

What compiler are you using to compile your program?

Do you get any warning messages?

Did you study the documentation for scanf()?

There is a problem with this line:
        scanf("%s", &info.fName);

Your compiler should be generating a message to tell you why. Please re-read post number three carefully and make the corrections.

Jim

This post has been edited by jimblumberg: 10 May 2013 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 ShankarLall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-April 13

Re: Programme Crash without error

Posted 11 May 2013 - 03:55 AM

I am using CodeBlock v12 - GNC-GCC Compiler.

mingw32-gcc.exe    -c "C:\Users\Shankar Lall\Documents\text.c" -o "C:\Users\Shankar Lall\Documents\text.o"
mingw32-g++.exe  -o "C:\Users\Shankar Lall\Documents\text.exe" "C:\Users\Shankar Lall\Documents\text.o"   
Process terminated with status 0 (0 minutes, 2 seconds)
0 errors, 0 warnings (0 minutes, 2 seconds)



Was This Post Helpful? 0
  • +
  • -

#8 ShankarLall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 15-April 13

Re: Programme Crash without error

Posted 11 May 2013 - 04:21 AM

OK I figure out what was causing the problem.

it was

int openfile(char filename[], FILE *fPtr)
	{
	    if ( ( fPtr = fopen(filename, "wb" ) ) == NULL )
	        printf( "File could not be opened\n" );
	 
	}



no value was returning.

Now my second problem is that I cant read from the file nor can i compare account number.

void displayCustomer(FILE *fPtr)
{
    struct customerStr info = {0, "", "", "", 0};
    int accNum;
    printf("?Account Number: ");
    scanf("%d", &accNum);

    fseek( fPtr, ( accNum - 1 ) * sizeof( struct customerStr ), SEEK_SET );
    fread( &info, sizeof( struct customerStr ), 1, fPtr );
    if (info.accNum == accNum)
    {
        printf("Account #\t%d\n", info.accNum);
        printf("Full Name\t%s  %s\n", info.fName, info.lName);
        printf("Gender\t%s\n", info.gender);
        printf("Balance\t%2.lf\n", info.balance);
    }
    else
    {
        printf("\nInvalid Account Number\n");
    }

}


Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,467
  • Joined: 05-May 12

Re: Programme Crash without error

Posted 11 May 2013 - 09:56 PM

So you found one of the problems. How did you fix it? Can you show us the updated code with your fix?

Part of the issue with your code is that you are making I/O calls and not checking the return values to see if they succeeded. For example, you do an fseek() and don't check the return value. You call fread() and don't check the return value. You simply assume that the call succeeds. What happens if your assumptions are incorrect? Is your code robust enough to handle your assumptions being blown out of the airlock?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1