Roei Burstein Roei Burstein - 21 days ago 8
C++ Question

Strange error c++ xcode - Thread 1: EXC_BAD_ACCESS

Please ignore how awfully my code is made, it's for a project and I wanted to finish as fast as possible. I try to run my code on xcode and I get an error I have no idea about. In this program, I am supposed to import a text file with phone records and display a cost based on the information. The file path is correct, and I don't know what's wrong with my code to cause this error:

https://drive.google.com/file/d/0Bysk6_x4B46uU2pmWFhRYWd6d1E/view?usp=sharing

Here's the code:

// phoneRecords.cpp
// PhoneRecords
//
// Created by Roei Bustein on 7/19/16.
// Copyright © 2016 Roei Bustein. All rights reserved.
//

/*
* This program reads records from a file and parse each
* record into variables as it goes
*/

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

using namespace std;

string makeTimeIntoInt(string line[]) {
string temp = "";
string time = line[2];
for (char a : time) {
if (a == ':')
continue;
else
temp += a;
}

return temp;
}

float calculateCost(string line[]) {
int time = stoi(line[2]);
int duration = stoi(line[3]);
if (line[1] == "Mo" || line[1] == "Tu" || line[1] == "We" ||
line[1] == "Th" || line[1] == "Fr") {
if (time < 800 || time > 1800) {
return duration * 0.25;
} else
return duration * 0.4;

}

else if (line[1] == "Sa" || line[1] == "Su") {
return duration * 0.15;
}

return 0;
}

int main() {
ifstream inputFile;
string line;
int cnt = 0;
inputFile.open(
"/Users/roeibustein/Documents/C++ "
"Workspace/PhoneRecords/PhoneRecords/calls1.txt");
string arr[18];
string line1[3], line2[3], line3[3], line4[3], line5[3], line6[3];
float costLine1, costLine2, costLine3, costLine4, costLine5, costLine6;

if (inputFile.is_open()) {
while (getline(inputFile, line)) {
istringstream split(line);

while (split) {
split >> arr[cnt];
cnt++;
split >> arr[cnt];
cnt++;
split >> arr[cnt];
cnt++;
}
}
}

line1[1] = arr[1];
line1[2] = arr[2];
line1[3] = arr[3];
line2[1] = arr[4];
line2[2] = arr[5];
line2[3] = arr[6];
line3[1] = arr[7];
line3[2] = arr[8];
line3[3] = arr[9];
line4[1] = arr[10];
line4[2] = arr[11];
line4[3] = arr[12];
line5[1] = arr[13];
line5[2] = arr[14];
line5[3] = arr[15];
line6[1] = arr[16];
line6[2] = arr[17];
line6[3] = arr[18];

line1[2] = makeTimeIntoInt(line1);
line2[2] = makeTimeIntoInt(line2);
line3[2] = makeTimeIntoInt(line3);
line4[2] = makeTimeIntoInt(line4);
line5[2] = makeTimeIntoInt(line5);
line6[2] = makeTimeIntoInt(line6);

costLine1 = calculateCost(line1);
costLine2 = calculateCost(line2);
costLine3 = calculateCost(line3);
costLine4 = calculateCost(line4);
costLine5 = calculateCost(line5);
costLine6 = calculateCost(line6);

cout << line1[1] << " " << line1[2] << " " << line1[3] << " " << costLine1
<< "\n";

cout << line2[1] << " " << line2[2] << " " << line2[3] << " " << costLine2
<< "\n";

cout << line3[1] << " " << line3[2] << " " << line3[3] << " " << costLine3
<< "\n";

cout << line4[1] << " " << line4[2] << " " << line4[3] << " " << costLine4
<< "\n";

cout << line5[1] << " " << line5[2] << " " << line5[3] << " " << costLine5
<< "\n";

cout << line6[1] << " " << line6[2] << " " << line6[3] << " " << costLine6
<< "\n";

return 0;
}

Answer

Your array is beyond it's bounds . Copy this code to see if it work.

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

using namespace std;

string makeTimeIntoInt(string line[]) {
    string temp = "";
    string time = line[1];
    for (char a : time) {
        if (a == ':')
            continue;
        else
            temp += a;
    }

    return temp;
}

float calculateCost(string line[]) {
    int time = stoi(line[1]);
    int duration = stoi(line[2]);
    if (line[0] == "Mo" || line[0] == "Tu" || line[0] == "We" ||
        line[0] == "Th" || line[0] == "Fr") {
        if (time < 800 || time > 1800) {
            return duration * 0.25;
        } else
            return duration * 0.4;

    }

    else if (line[0] == "Sa" || line[0] == "Su") {
        return duration * 0.15;
    }

    return 0;
}

int main() {
    ifstream inputFile;
    string line;
    int cnt = 0;
    inputFile.open("/Users/lhmac/abc.txt");
    string arr[18];
    string line1[3], line2[3], line3[3], line4[3], line5[3], line6[3];
    float costLine1, costLine2, costLine3, costLine4, costLine5, costLine6;

    if (!inputFile.is_open()) {
        return 0;
    }

    while (getline(inputFile, line) && cnt < 18) {
        istringstream split(line);
        split >> arr[cnt];
        cnt++;
        split >> arr[cnt];
        cnt++;
        split >> arr[cnt];
        cnt++;
    }

    inputFile.close();

    line1[0] = arr[0];
    line1[1] = arr[1];
    line1[2] = arr[2];
    line2[0] = arr[3];
    line2[1] = arr[4];
    line2[2] = arr[5];
    line3[0] = arr[6];
    line3[1] = arr[7];
    line3[2] = arr[8];
    line4[0] = arr[9];
    line4[1] = arr[10];
    line4[2] = arr[11];
    line5[0] = arr[12];
    line5[1] = arr[13];
    line5[2] = arr[14];
    line6[0] = arr[15];
    line6[1] = arr[16];
    line6[2] = arr[17];

    line1[2] = makeTimeIntoInt(line1);
    line2[2] = makeTimeIntoInt(line2);
    line3[2] = makeTimeIntoInt(line3);
    line4[2] = makeTimeIntoInt(line4);
    line5[2] = makeTimeIntoInt(line5);
    line6[2] = makeTimeIntoInt(line6);

    costLine1 = calculateCost(line1);
    costLine2 = calculateCost(line2);
    costLine3 = calculateCost(line3);
    costLine4 = calculateCost(line4);
    costLine5 = calculateCost(line5);
    costLine6 = calculateCost(line6);

    cout << line1[0] << " " << line1[1] << " " << line1[2] << " " << costLine1
    << "\n";

    cout << line2[0] << " " << line2[1] << " " << line2[2] << " " << costLine2
    << "\n";

    cout << line3[0] << " " << line3[1] << " " << line3[2] << " " << costLine3
    << "\n";

    cout << line4[0] << " " << line4[1] << " " << line4[2] << " " << costLine4
    << "\n";

    cout << line5[0] << " " << line5[1] << " " << line5[2] << " " << costLine5
    << "\n";

    cout << line6[0] << " " << line6[1] << " " << line6[2] << " " << costLine6
    << "\n";

    return 0;
}