Why am I getting a segfault?

My current assignment tasks me with using fstream, which I now finally understand to an extent, but now I have a problem which I haven't seen since I learned Python. How can I stop this program from segfaulting? breakfasts.txt lists the name of a food item on one line, then the cost of the food item on the next, and my program should be taking, line by line, a string equal to the line on the text file, though instead I get an error, segmentation fault. What piece of illegal memory am I accessing here?

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    ifstream inFile;
    cout << fixed << showpoint << setprecision(2);
    
    inFile.open("breakfasts.txt");

    if(!inFile)
    { cout << "Cannot open the input file. Program Terminates!" << endl; return 1; }
    
    string foodName[8];
    string foodCost[8];

    //We need to record the value by using getline(inFile,foodName[i]);
    //Now we need to skip the first line, and get getline(inFile,foodCost[i]);
    //Repeat this, incrementing the number of lines skipped by 1 each time.  
    
    for(int i = 0; i < 16; i++)
    {
        if(16 % (i+1) != 0)
        {
            getline(inFile,foodName[i]);
        }
        else if(16 % (i+1) == 0)
        {
            getline(inFile,foodCost[i]);
        }
    }
    return 0;
}
Last edited on
Your arrays have 8 elements. Valid indices are thus 0..7.
The indices that you do use, are in range 0..15.
Why is my code starting at line 10?
Here is the breakfasts.txt file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Plain Egg
1.45
Bacon and Egg
2.45
Muffin
0.99
French Toast
1.99
Fruit Basket
2.49
Cereal
0.69
Coffee
0.50
Tea
0.75


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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    ifstream inFile;
    cout << fixed << showpoint << setprecision(2);
    
    inFile.open("breakfasts.txt");

    if(!inFile)
    { cout << "Cannot open the input file. Program Terminates!" << endl; return 1; }
    
    string line[16];  //we'll later turn these into foodName[8] and foodCost[8]
    string foodName[8];
    string foodCost[8]; 
    //if statement evaluates odd/even, if odd j++, if even, k++
    for(int i = 0, j = 0, k = 0; i < 16; i++)
    {
        getline(inFile,line[i]);
        cout << "Line " << i << ":  " << line[i] << endl;
    }
    //We need to record the value by using getline(inFile,foodName[i]);
    //Now we need to skip the first line, and get getline(inFile,foodCost[i]);
    //Repeat this, incrementing the number of lines skipped by 1 each time.  
    //Get value[0], get value2[0], 
    return 0;
}

Last edited on
Why is my code starting at line 10?

??? Please explain.


Yes, your input data has 16 lines. However, it contains only 8 entries, where an entry is a pair of name and cost.

1
2
3
4
5
6
std::string name;
std::string cost;

// this reads one entry (but two lines):
std::getline( inFile, name );
std::getline( inFile, cost );


This loop reads at most 8 entries, but will stop earlier if any read fails:
1
2
3
4
5
size_t entry = 0;
while ( entry < 8 and std::getline( inFile, name ) and std::getline( inFile, cost ) ) {
  std::cout << "Entry " << entry << ": " << name << ' ' << cost << '\n';
  ++entry;
}



The wording in your instructions is a bit peculiar (the "we need to skip the first line" that makes no real sense).
Topic archived. No new replies allowed.