Exc bad access error in xcode?

closed account (zT4NhbRD)
So the code I'm writing is to read a file, sort it, then output back into the file. It was working fine the first time I did it, made changes my TA recommended, and now it's not working. I encountering the same breakpoint in xcode and then the rest of my code won't output. What's strange is my TA says it outputs just fine for him.

I did some research and figured out that it maybe it's the code trying to access something that hasn't been initialized yet? Would someone mind taking a look and possibly running it to maybe help me figure this out? I've got my main cpp, then two class cpps and headers. I'll post each section in a separate post.

Main code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include "Account.h"
#include "Accounts.h"


using namespace std;

int main(int argc, char* argv[])
{
    //Initialize function parameters
    char *file = argv[1];
    char *outputFile = argv[2];
    Account list;
    Accounts bank;
    //Print all arguments in command line
    for(int index = 0; index < argc; index++)
        cout << argv[index] << endl;
    //Function calling
    if(argc != 2){
        cout << "Number of arguments passed incorrect. Please check then try again" << endl;
        return 0;
    }
    else{
        bank.add(file);
        bank.print();
        bank.sort();
        bank.display(outputFile);
        return 0;
    }
}


closed account (zT4NhbRD)
Sets each account then returns it

Last edited on
closed account (zT4NhbRD)
Stores accounts in array, reads file, and outputs file

Last edited on
One problem I see is that at line 17 you're accessing argv[2]. argv[0] is your program file name. At line 24, you check if argc is 2. This means you can only run your program successfully if you specify one argument. argv[argc] normally contains null to indicate the end of the argument list.
Therefore at line 32, you're passing null to bank.display. Accounts::Display is expecting a string, but is being passed a const char * which is null. string's constructor will try and copy the string pointed at by that null pointer causing a trap or exception.
closed account (zT4NhbRD)
I was taught that the command line has 3 arguments: 1 for input and 1 for output. If I used argv[1] twice I'd be overriding the the input file. Also, I was told by my TA to change the char parameters in my display function to a string. Either way it opens the file just fine.

But anyways that isn't the issue. I'm still getting breakpoints. To be more specific I'm getting them in my Account function. The first breakpoint is when it tries to access setLastName and it looks like specifically when trying to read the last name?
Topic archived. No new replies allowed.