Segmentation Fault

Discussion in 'C++' started by didier2309, May 9, 2006.

  1. didier2309

    didier2309 Guest

    I get segemntation fault when I enter that function it executes till
    wcpcpy(lacc_num, L"000-00-0001");
    Here is the function:
    It executes till the statement wcpcpy(laccnum, L"000-00-0001");

    int acopen::eek:pen_acc()
    {
    int accnum;
    char stname[80];
    FILE *getaccnum;
    getaccnum = fopen("numbers", "r");
    if(getaccnum == NULL){
    cout << "No account has been openned yet\n";
    wcpcpy(lacc_num, L"000-00-0001");
    }
    else{
    fseek(getaccnum, 11, SEEK_END);
    fscanf(getaccnum, "%S", lacc_num);
    int sec[9], accnum;
    sec[0] = wctob(lacc_num[0]);
    sec[1] = wctob(lacc_num[1]);
    sec[2] = wctob(lacc_num[2]);
    sec[3] = wctob(lacc_num[4]);
    sec[4] = wctob(lacc_num[5]);
    sec[5] = wctob(lacc_num[7]);
    sec[6] = wctob(lacc_num[8]);
    sec[7] = wctob(lacc_num[9]);
    sec[8] = wctob(lacc_num[10]);
    for(int i = 0; i < 9; i--){
    accnum = accnum*10 +sec ;
    }

    int tempaccnum;

    accnum = accnum++;
    tempaccnum = accnum;
    for(int i = 8; i >= 0; i--){
    sec = tempaccnum - (tempaccnum/10)*10;
    tempaccnum = tempaccnum/10;
    }
    lacc_num[0] = btowc(sec[0]);
    lacc_num[1] = btowc(sec[1]);
    lacc_num[2] = btowc(sec[2]);
    lacc_num[3] = '-';
    lacc_num[4] = btowc(sec[3]);
    lacc_num[5] = btowc(sec[4]);
    lacc_num[6] = '-';
    lacc_num[7] = btowc(sec[5]);
    lacc_num[8] = btowc(sec[6]);
    lacc_num[9] = btowc(sec[7]);
    lacc_num[10] = btowc(sec[8]);
    lacc_num[11] = '\0';

    }
    fclose(getaccnum);
    FILE *putn;
    putn = fopen("numbers","ab");
    if(putn == NULL){
    cout << "\ncannnot open file\n";
    return 1;
    }
    FILE *out;
    out = fopen("account","ab");
    if(out == NULL) {
    cout << "\ncannot open file";
    return 1;
    }

    cout <<"\nthe account number is: ";
    printf("%S\n",lacc_num);
    cout << "enter name: \n";
    cin >> sname;

    cout << "enter last name\n";
    cin >> slastname;

    cout << "birthday? Day, month and year seperately (in
    numbers)...\n";
    do{
    cin >> sbirth_day ;
    if(!getint(sbirth_day))
    badent();//call badebt()
    }while(!getint(sbirth_day));

    do{
    cin >> sbirth_month ;
    if(!getint(sbirth_month))
    badent();
    }while(!getint(sbirth_month));

    do{
    cin >> sbirth_year;
    if(!getint(sbirth_year))
    badent();
    }while(!getint(sbirth_year));

    cout << "street address?(house number and street name only:
    Enter [space]~ to end address) \n";
    cin >> shousenum ;
    do{
    cin >> stname;
    strcat(sstreetname, stname);
    strcat(sstreetname, " ");
    }while(strcmp(stname,"~"));

    cout << "city?\n";
    cin >>scity;

    do{
    cout <<"zipcode?\n";
    cin >> szip;
    }while(!getlong(szip));

    cout << "phone and area code?\n";
    do{
    cin >> sphone;
    if(!getlong(sphone))
    badent();
    }while(!getlong(sphone));

    do{
    cin >> sarea ;
    if(!getint(sarea))
    badent();
    }while(!getint(sarea));

    do{
    cout << "social security?\n";
    cin >>ssocial;
    }while(!getlong(ssocial));

    do{
    cout << "deposit amount?\n";
    cin >>sbalance;
    }while(!(getfloat(sbalance)));
    fprintf(out, "%S\n", lacc_num);
    fprintf(putn, "%S\n", lacc_num);
    fprintf(out ,"%s\n", sname);
    fprintf(out ,"%s\n", slastname);
    fprintf(out, "%s\n", sbirth_day);
    fprintf(out, "%s\n", sbirth_month);
    fprintf(out, "%s\n", sbirth_year);
    fprintf(out, "%s\n", sstreetname);
    fprintf(out, "%s\n", shousenum);
    fprintf(out, "%s\n", scity);
    fprintf(out, "%s\n", szip);
    fprintf(out, "%s\n", sphone);
    fprintf(out, "%s\n", sarea);
    fprintf(out, "%s\n", ssocial);
    fprintf(out, "%s\n", sbalance);
    fclose(putn);
    fclose(out);
    return 0;
    }
    -
    didier2309, May 9, 2006
    #1
    1. Advertising

  2. didier2309

    mlimber Guest

    didier2309 wrote:
    > I get segemntation fault when I enter that function it executes till
    > wcpcpy(lacc_num, L"000-00-0001");
    > Here is the function:
    > It executes till the statement wcpcpy(laccnum, L"000-00-0001");


    Executes up to and including that line or up to and then crashes on
    that line?

    >
    > int acopen::eek:pen_acc()
    > {
    > int accnum;
    > char stname[80];
    > FILE *getaccnum;
    > getaccnum = fopen("numbers", "r");
    > if(getaccnum == NULL){
    > cout << "No account has been openned yet\n";
    > wcpcpy(lacc_num, L"000-00-0001");
    > }

    [snip]

    There's no obvious problem here. However, we don't know what wcpcpy
    does or what type lacc_num is. Looks like it's time to use your
    debugger. Look for memory over-runs somewhere in your program before it
    reaches this point. Wayward pointers can cause strange errors in
    unrelated parts of the program.

    Of course, since this is comp.lang.c++, I would suggest you use
    fstreams instead of C-style files.

    Cheers! --M
    mlimber, May 9, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Hunsley
    Replies:
    17
    Views:
    864
  2. Pud
    Replies:
    0
    Views:
    574
  3. Replies:
    0
    Views:
    527
  4. Ivan Vecerina
    Replies:
    0
    Views:
    482
    Ivan Vecerina
    Jun 29, 2003
  5. Vasileios Zografos

    Re: segmentation fault exception handling

    Vasileios Zografos, Jun 30, 2003, in forum: C++
    Replies:
    5
    Views:
    15,604
    Pete Becker
    Jul 1, 2003
Loading...

Share This Page