tags:

views:

211

answers:

5

I've been trying to make my code work on Windows (moved from the Mac) and for some reason I get a runtime error related to my strcpy call.

Please help!!

Cust.h

/*
 *  Cust.h
 *  Project 3
 *
 *  Created by Anthony Glyadchenko on 11/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include <iostream>
#include <string>

using namespace std;

#ifndef CUST_H
#define CUST_H

class Cust{
public:
    char * getAcctNum();
    void setAcctNum(char num[]);
    double getCurrBalance();
    void setCurrBalance(double balance);
    void addToCurrBalance(double amount);
    void subFromCurrBalance(double amount);
    void setAcctFN(char firstName[]);
    void setAcctLN(char lastName[]);
    char * getAcctFN();
    char * getAcctLN();
    void setPIN(int pin);
    int getPIN();

private:
    char acctNum[255];
    char acctFN[255];
    char acctLN[255];
    double currBalance;
    int pin;
    char fileName[255];
};
#endif

Cust.cpp

/*
 *  Cust.cpp
 *  Project 3
 *
 *  Created by Anthony Glyadchenko on 11/17/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */
#include <fstream>
#include <string>
#include <sstream>
#include "Cust.h"

using namespace std;

char * Cust::getAcctNum(){
    return acctNum;
}

void Cust::setAcctNum(char num[]){
    strcpy(acctNum,num);
}

double Cust::getCurrBalance(){
    return currBalance;
}

void Cust::setCurrBalance(double balance){
    currBalance = balance;
}

void Cust::addToCurrBalance(double amount){
    currBalance += amount;
}

void Cust::subFromCurrBalance(double amount){
    currBalance -= amount;
}

void Cust::setAcctFN(char firstName[]){
    strcpy(acctFN,firstName);
}

void Cust::setAcctLN(char lastName[]){
    strcpy(acctLN,lastName);

}

char * Cust::getAcctFN(){
    return acctFN;
}

char * Cust::getAcctLN(){
    return acctLN;
}

void Cust::setPIN(int pin){
    Cust::pin = pin;
}

int Cust::getPIN(){
    return pin;
}

main.cpp

#include <iostream>
#include <string>
#include <fstream>
#include "Cust.h"
using namespace std;

int findNumLines(char file[]){
    ifstream tempInput(file);
    char ch;
    int lineCount = 0;
    while (!tempInput.eof()){
     tempInput.get(ch);
     if (ch == '\n') lineCount++;
    }
    tempInput.close();
    return lineCount;
}
int main (int argc, char * const argv[]) {
    Cust customers[500];
    char tmpString[70] = " ";
    char pch[255];
    string tmpAcctFN = " ";
    string tmpAcctLN = " ";
    ifstream input("P3_custData.txt");
    for (int idx = 0; idx < 130; idx++){
     input.getline(tmpString, 70, '\n');
     strcpy(pch,strtok(tmpString," "));
     customers[idx].setAcctNum(pch);
     cout << pch << endl;
     strcpy(pch, strtok(NULL," "));;
     customers[idx].setAcctFN(pch);
     cout << pch << endl;
     strcpy(pch, strtok(NULL," "));;
     customers[idx].setAcctLN(pch);
     cout << pch << endl;
     strcpy(pch, strtok(NULL," "));;
     customers[idx].setCurrBalance(atol(pch));
     cout << pch << endl;
     strcpy(pch, strtok(NULL," "));;
     customers[idx].setPIN(atoi(pch));
     cout << pch << endl;
    }
    input.close();
    return 0;
}

P3_custData.txt

10000     Alicia    Jones          1005.00   1234
10010     Mary      Gonzalez       2040.55   8472
10020     Bill      Henry          5340.20   7840
10030     Alex      Brown         10010.50   8202
10040     Becca     Kingman         983.00   9201
10050     Oliver    Stone         12001.74   2382
10060     Robert    Reich          3010.30   8137
10070     Judith    Johnson         540.98   8203
10080     Jeremy    Brice           672.10   8472
10090     Andrew    Aziz           4041.50   2456
10100     Alicia    Jones         10010.00   8264
10110     Mary      Gonzalez       2050.51   6252
10120     Bill      Henry          5340.20   3658
10130     Audrey    Samuels         536.78   7462
10140     Marion    Sams           9788.19   3266
10150     Richard   Rubens         3265.90   6237
10160     Russell   Townsend        123.00   5324
10170     Carolyn   Tanner         4210.60   3256
10180     Corey     Brill            77.40   4356
10190     Randall   North            44.50   6346
10200     James     Jackson       10020.00   2457
10210     Martin    Gallagher      2041.50   2345
10220     William   Walker         7340.20   2345
10230     Ellen     Jacobson        433.99   1234
10240     Angela    Bryer         15010.10   4321
10250     Steven    Bond            960.00   9876
10260     Sally     Stevens          23.10   2834
10270     Alan      Fuller         7858.00   7294
10280     Peter     Prentice        697.00   7618
10290     Paula     Smith          1020.00   7349
10300     Alice     Johnson       10030.00   7364
10310     Gail      Green          3040.55   6717
10320     Gene      Harold         8340.20   5162
10330     Lois      Lane            100.00   7234
10340     Debby     Dewhurst       8765.34   1382
10350     Louise    Talent          350.00   8193
10360     Louis     Bragg         10091.22   6738
10370     Alexander Gibson          540.70   7392
10380     Gertrude  Ring           9030.00   7390
10390     John      Johnson        3299.99   6329
10400     Alice     Johannsen      2009.80   8273
10410     Marty     Gordon         2040.55   6712
10420     William   Hurst           540.20   1273
10430     Barry     True            278.50   3247
10440     Maxwell   Smart           800.66   2119
10450     Owen      Burton         5261.00   3749
10460     Diane     Walters        6004.44   3794
10470     Georgina  Trump          7083.00   9283
10480     Erica     Applegate     12007.00   3649
10490     Walter    Wonkers       15789.40   1639
10500     Alicia    Rogers         1009.00   6392
10510     Emmanuel  Evans           220.50   2803
10520     Robert    Bachman         760.25   9999
10530     Richard   Rogers         2345.10   8888
10540     Roberta   Maxwell        6666.66   6238
10550     Gregory   Ichan           521.30   1111
10560     Lars      Jensen          497.80   7239
10570     Roberta   Peters        20004.10   3333
10580     Ali       Masterson      3980.00   8304
10590     Laurence  Leonard        6732.12   3684
10600     Tracy     Jones           500.00   6382
10610     Michael   Gonzalez       2040.57   3649
10620     Alexander Henry          5368.10   7389
10630     Leo       Palmer        21900.00   6283
10640     Esther    Richman         300.00   3684
10650     Harold    Pinter         6783.10   3648
10660     Eva       Burton         5355.55   7639
10670     William   Shakespeare       0.00   6384
10680     Russell   Carlson        4455.77   1384
10690     Janice    Klein          3965.15   2738
10700     Henry     Adams          4050.00   2374
10710     George    Gonzalez       2040.55   2739
10720     Jose      Enrique        5340.20   2376
10730     Jane      Eakins          657.90   8209
10740     Justin    Prince         8000.12   7394
10750     Ed        True           5978.00   4798
10760     Emily     Prentice         34.00   8220
10770     Olivia    Callahan        231.21   5374
10780     Peter     Cabot          5478.20   8293
10790     Andrew    Austin         1110.10   3792
10800     Oliver    Owens           100.00   8201
10810     Monty     Wood            200.55   3748
10820     Terrance  Thomas          340.20   6239
10830     Barry     Brown           105.00   6387
10840     Harrison  Huston          299.78   6384
10850     Robin     Young          8655.30   9734
10860     Ishmael   Green         10101.10   9246
10870     Fiona     Fein            257.20   2836
10880     Florence  Gregson        5699.60   6374
10890     Wilma     Flinstone        78.00   5478
10900     Nancy     Drew           2001.00   2536
10910     Captain   Kirk           2444.44   7364
10920     Allie     McGraw          540.20   6483
10930     Frederick Campbell       1050.00   6492
10940     Paula     Prescott       5134.44   7483
10950     Ursula    Unger           789.00   6482
10960     Betty     Banker         4500.34   3567
10970     Elizabeth Young          1022.00   6489
10980     Maria     Manners         510.00   5463
10990     Tracy     Austin          674.10   6834
11000     Alex      Andrews         300.00   1245
11010     Mike      Matire         4040.55   7234
11020     Oscar     Grouch         5340.20   9326
11030     Jennifer  Young           823.33   6593
11040     Walter    True            444.00   3485
11050     Hudson    Haliburton      953.10   8465
11060     Ursula    Angel           321.00   6583
11070     Zackery   Brown          7666.60   9123
11080     Carole    King          10000.00   6382
11090     Tracy     Burton          955.00   6654
11100     Arthur    Jones           100.00   7893
11110     Andrew    Jackson        4040.55   9173
11120     Samuel    Barber           50.20   2874
11130     George    Gregrory        643.00   7392
11140     Quentin   Larson           21.00   9277
11150     Dorothy   Pace            777.23   4270
11160     Frieda    Flowers        9000.99   6483
11170     Howard    Alexander        78.00   2743
11180     Henry     Aldritch         55.00   2084
11190     Beatrice  Snow             99.99   2987
11200     Kelly     Klark           200.00   3874
11210     Mary      Gonzalez        440.51   2480
11220     Elly      Hand            555.20   2479
11230     Gregory   George          431.44   4756
11240     Nancy     Alexander      6220.90   9274
11250     Sargent   Pepper        16870.50   7777
11260     Linda     Gale          20000.70   2974
11270     Charles   Reilly          544.45   5973
11280     Chuck     Mangers          10.00   5555
11290     Wilson    Beckett        6010.10   6666
A: 

The most likely reason is that one of the five strtok() sequence calls is returning NULL because there aren't enough fields on the line.

wallyk
There are 5 columns aren't there?
Anthony Glyadchenko
Perhaps the input has tab characters, not spaces?
wallyk
When I run his code I don't get any errors under valgrind or with mudflaps (gnu g++ 4.3.2)
David Claridge
They are spaces.
Anthony Glyadchenko
Is that a space character between the quotes in `strcpy(pch,strtok(tmpString," "));`?
wallyk
Yes, it is a space.
Anthony Glyadchenko
+2  A: 

It would help if you would post the error message, but since you're saying you're going from Mac to Windows, it would point to a line ending issue. Convert your custData.txt file to have Windows end-of-lines (CR + LF) and retry it that way.

JRL
It crashes at strcpy(acctNum,num); in the setAcctNum method.
Anthony Glyadchenko
Which line of input is it processing?
wallyk
Actually, sorry, it crashes at int main(): strcpy(pch,strtok(tmpString," "));
Anthony Glyadchenko
What if strtok returns a null pointer ;)
Billy ONeal
+1  A: 

It could be that your line

input.getline(tmpString, 70, '\n');

is just looking for the \n when it should be looking for a \r\n or something similar because of the Windows line ending being different the Mac line ending.

John Boker
It's Windows encoding.
Anthony Glyadchenko
A: 

I don't get a runtime error, but I get a compile time error (on Linux) because you didn't

#include <cstring>
#include <cstdlib>

in main.cpp

or

#include <cstring>

in Cust.cpp

After adding those it compiled and ran fine for me...

What precisely is the error you get?

David Claridge
Unhandled exception at 0x6848f693 in P3_WIN.exe: 0xC0000005: Access violation reading location 0x00000000.
Anthony Glyadchenko
That's definitely a null pointer exception.
Anon.
I have them all and it still doesn't compile.
Anthony Glyadchenko
A: 

You didn't specify in which line of the output file the crash happens. Could it be that the last line is missing the carriage return (\n) at the end?

Nikola Gedelovski