Funny C++ problem

Hey guys. Having a big of an odd error which I can't get my head around.

After altering my dynamic array in the function fDetails, I try and display each line of its contents, strings, but a get a EXC_BAD_ACCESS error.

The for loop in main is where I am having the difficulty, the rest seems to work.

Any idea why it does this, as I am just using the functionality of C++.

I am coding in XCode, and just a console application.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
//  main.cpp
//  3. CPPTutorialA Slides 8-11
//
//  Created by Adam Thoseby on 09/09/2012.
//  Copyright (c) 2012 Adam Thoseby. All rights reserved.
//

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>

using namespace std;

int fDetails(string arrayTokens[]);

int main(int argc, const char * argv[])
{
    string arrayTokens[2];
    int nTokens;
    string * pStart, * pFinish;

    nTokens = fDetails(arrayTokens);
    
    for (int i=0; i<nTokens; i++) {
        cout<<arrayTokens[i]<<endl;
    }
    
    pStart = &arrayTokens[0];
    pFinish = &arrayTokens[nTokens];
    
    cout<<pStart<<endl<<pFinish;
    return 0;
}


int fDetails (string arrayTokens[]){
    int nTokens = 0, i = 0;
    string fname, number;
    
    
    //cout<<"Please enter in the address, file name and the extension so we can open the file: "<<endl;
    //cin>>fname;
    fname="/Users/ANCT/Desktop/num.txt";
    
    ifstream fin;
    
    fin.open(fname.c_str());
    if (!fin) {
        cout<<"\nPlease ensure that the file is located in the default directory.\nAlso please ensure that the file is spelt correctly, including the extension.\n"<<endl;
        return 0;
    }
    
    while (!fin.eof()) {
        i++;
        fin>>number;
    }
    
    fin.close();
    fin.open(fname.c_str());
    
    arrayTokens = new string[i];
    
    for (int n=0; n<i; n++) {
        getline(fin,arrayTokens[n]);
    }
    
    nTokens = i;
    
    return nTokens;
}
yeah ur passing an empty array on line 24
Topic archived. No new replies allowed.