奇怪的错误c ++ xcode - 线程1:EXC_BAD_ACCESS

时间:2016-07-27 07:07:15

标签: c++ xcode

请忽略我的代码制作有多么糟糕,对于一个项目我想要尽快完成。我尝试在xcode上运行我的代码,我得到一个我不知道的错误。在这个程序中,我应该导入带有电话记录的文本文件,并根据信息显示成本。文件路径是正确的,我不知道我的代码有什么问题导致此错误:

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

以下是代码:

//  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;
}

2 个答案:

答案 0 :(得分:1)

我认为您的错误是由访问表中的超出范围的索引引起的。 在c ++表中,索引从0开始,因此当您声明string arr[18]时,无法通过调用arr[18]来获取值。换句话说,第一个元素是arr[0],最后一个元素是arr[17]

答案 1 :(得分:0)

你的阵列超出了它的范围。 复制此代码以查看它是否有效。

#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;
}