Segmentation fault. Confused.

Hey guys, just wondering if someone can take a look at this and possibly tell me why i'm getting a segmentation fault. I've tried putting cout statements to see where the file is failing, and it looks like it's failing at line 39 when I do the file open. It's not going anywhere past 39, even to give me the failure message. It just stops cold at 39 and gives me Segmentation Fault. I'm kinda pulling my hair out over this one, if anyone can guide me in the right direction it would be much, much appreciated.

Thanks!

Jade

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

void getSomeData (ifstream&, string&, string&, double[], double[], double[], double[], int&, int&, int&, int&, int&);
void doMathSon (double[], double[], double[], double[], int&, int&, int&, int&, int&, double&, double&, double&, double&, double&);
void writeSomeStuff (double&);

int main ()
{

    int numHW, numQuiz, numProg, numExam, numLab;
    double arrHW[20], arrQuiz[10], arrProg[5], arrExam[3];
    double sumHW, sumQuiz, sumProg, sumExam, weightedAvg;
    ifstream fin;
    string fileName;
    string studentName;

    getSomeData (fin, fileName, studentName, arrHW, arrQuiz, arrProg, arrExam, numLab, numHW, numQuiz, numProg, numExam);
    doMathSon (arrHW, arrQuiz, arrProg, arrExam, numLab, numHW, numQuiz, numProg, numExam, sumHW, sumQuiz, sumProg, sumExam, weightedAvg);
    writeSomeStuff (weightedAvg);

}


void getSomeData (ifstream&inFile, string&fName, string&studentName, double hwArray[20],
double quizArray[10], double progArray[5], double examArray[3], int&labNum, int&hwSize,
int&quizSize, int&progSize, int&examSize)

{

    cout << "Enter the name of the file you want to open: ";
    getline (cin, fName);
    cout << "I hate this program.";
    inFile.open(fName.c_str());

    if (inFile.fail())
    {
        cout << "Could not open.";
    }

    cout << "Hi.";



    getline(inFile, studentName);

    cout << studentName;

    inFile >> hwSize;
    inFile.ignore (20, '\n');

    inFile >> hwArray[hwSize];

    inFile >> quizSize;
    inFile.ignore (20, '\n');

    inFile >> quizArray[quizSize];

    inFile >> progSize;
    inFile.ignore (20, '\n');

    inFile >> progArray[progSize];

    inFile >> examSize;
    inFile.ignore (20, '\n');

    inFile >> examArray[examSize];

    inFile >> labNum;
    inFile.ignore (25, '\n');

    cout << "hw: " << hwSize << "quiz: " << quizSize << "prog: " << progSize << "exam: " << examSize << "lab: " << labNum;
}


void doMathSon (double hwArr[], double quizArr[], double progArr[], double examArr[],
int&labNum, int&hwSize, int&quizSize, int&progSize, int&examSize,
double&hwSum, double&quizSum, double&progSum, double&examSum, double&weightedAverage)

{
    int hwCount, quizCount, progCount, examCount;
    double hwPercent, quizPercent, progPercent, examPercent, labPercent;

    for (hwCount = 0; hwCount < hwSize; hwCount++)
    {
        hwSum = hwSum + hwArr[hwCount];
    }
    for (quizCount = 0; quizCount < quizSize; quizCount++)
    {
        quizSum = quizSum + quizArr[quizCount];
    }

    for (progCount = 0; progCount < progSize; progCount++)
    {
        progSum = progSum + progArr[progCount];
    }

    for (examCount = 0; examCount < examSize; examCount++)
    {
        examSum = examSum + examArr[examCount];
    }

    hwPercent = hwSum / (hwSize * 10);
    quizPercent = quizSum / (quizSize * 10);
    progPercent = progSum / (progSize * 20);
    examPercent = examSum / (examSize * 100);
    labPercent = (labNum * 3.5) / (labNum * 5);

    weightedAverage = (hwPercent * 5) + (quizPercent * 10) + (progPercent * 20) + (examPercent * 60) + (labPercent * 5);
}

void writeSomeStuff (double&wtdAvg)
{

    cout << wtdAvg << endl << endl;

    if (wtdAvg >= 90)
    {
        cout << "Your weighted average for the class is " << wtdAvg << "%. You currently have an A in the class.";
    }

    else if (wtdAvg >=80)
    {
        cout << "Your weighted average for the class is " << wtdAvg << "%. You currently have a B in the class.";
    }

    else if (wtdAvg >= 70)
    {
        cout << "Your weighted average for the class is " << wtdAvg << "%. You currently have a C in the class.";
    }
    else if (wtdAvg >= 60)
    {
        cout << "Your weighted average for the class is " << wtdAvg << "%. You currently have a D in the class.";
    }

    else
    {
        cout << "Your weighted average for the class is " << wtdAvg << "%. You are currently failing the course.";
    }

}
It ran past line 39 for me so sorry can't help.

Maybe there is something amiss with the file you open?
Last edited on
.c_str() is for string data types and not char []. c_str() is converting your string into char[]

CMIIW
You don't validate the file inputs. I get no segfaults if I use a file that have size values less than the size of the arrays.
chipp - I'm confused.... are you saying that my "string fileName;" should be declared a different way?

Peter87 - The file that I'm using has very few size values in it. Here's what I've been using:

1
2
3
4
5
6
7
8
9
10
11
Jade L. Scholz
3 Homeworks
9.5 10 10
4 Quizzes
9 9 9.5 10
2 Programs
20 19.5
1 Exam
96
5 Week Lab Attendance
3.5
With that files quizSize gets the value 10. When you later try to access quizArray[quizSize] it is out of bounds. Remember that arrays indices starts at zero. quizArray only has 10 elements but quizArray[10] is the 11th element.
Last edited on
@filly: sorry, i just wrongfully saw your variable type (i thought it was char [])
inFile >> hwArray[hwSize];
These lines only put one value into the array, at index hwSize in this case.

You need a loop put all the values on the line into the array.

Even so, these errors occur after line 39 so you should see "Hi" and studentName in the console at least.

What is the filename you enter at the prompt?
Last edited on
I think I got it. Vin's response plus Peter87's response combined in my brain to make sense. I realized that I was only loading one value into the array at location hwSize, which put it outside the array's defined size. Thank you both so much guys. I think I'm in serious need of sleep.

:)

Jade
This may clarify it further.

1
2
3
4
5
6
7
Jade L. Scholz	studentName = "Jade L. Scholz"
3 Homeworks	hwSize = 3		
9.5 10 10	hwArray[3] = 9.5, quizSize = 10
4 Quizzes	quizArray[10] = 4, progSize = "Quizzes" --> inFile fails
		progSize value remains original uninitialized value 
		progArray[possibly huge value] --> this is what probably causes 
		segmentation error not quizArray[10]


Also in this line you should initialise the sum variables to 0 or otherwise you won't get correct answer.

double sumHW, sumQuiz, sumProg, sumExam, weightedAvg;
Last edited on
Topic archived. No new replies allowed.