help; segmentation fault, vectors, getline what do I do?

I have been up for 30 hours, help me solve this problem. The problem is in the readfile function. I don't think getline is actually pulling anything

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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Assignment : 
// File : 
// Author : 
// Date : 2/6/2012
// Description : This program reads in an input file and returns a report on 
// the contents of the file. The file will be all text or else it will exit
// without printing a report. The report will contain a vowel, digit, special
// character, and consonant count.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;
const int MAXASSIGNMENTS = 50;

struct Date 
{ 
  string mm; // month 
  string dd; // day 
  string yyyy; // year 
};
struct assignment 
{
  string type;
  string title;
  string score;
  string maxscore;
  Date date;
  string edad;
};
 
void dotsplit(string line, string delimiter, vector<string>* words);
int ReadFile(ifstream & input, assignment G[]);
void WriteReport (ostream & output, assignment G[], int count);
void Swap (assignment & a, assignment & b);
void TypeSort (assignment G[], int N);
void DateSort (assignment A[], int N);
string convertInt(int number);

int main()
{
  ifstream input ("grades.txt");
  if (input.fail())
    {
      cout << "File: words.txt not found\n";
      exit(1);
    }
	assignment Grade[MAXASSIGNMENTS];
	int count = ReadFile (input, Grade);

	//DateSort(Grade, count);
	//TypeSort(Grade, count);	
	WriteReport(cout, Grade, count);
	return 0;
}

int ReadFile (ifstream & input, assignment G[])
{

  vector<string> words;
  vector<string> ymd; 
  int c= 0;
  string line;
  while(true)
    { 

      getline(input,line);
      dotsplit(line, " ", &words);
      dotsplit(words[1], "/", &ymd);
      
      G[c].type = words[0];
      G[c].date.yyyy = ymd[0];
      G[c].date.mm = ymd[1];
      G[c].date.dd = ymd[2];
      G[c].score = words[2];
      G[c].maxscore = words[3];
      G[c].edad = ymd[0]+ymd[1]+ymd[2];
      int y = 0;

      while (y < words.size())
	{
	  G[c].title = "";
	  G[c].title = G[c].title + words[y];
	  y++;
	}
      c++;
    }
  input.close();
  return c;
  
}
void WriteReport(ostream & output, assignment G[], int count)
{
	int x;
	for( x = 0; x<count;x++)
	  {
	    G[x].edad = G[x].date.mm +'/'+G[x].date.dd +'/'+G[x].date.yyyy;
	    cout << left << G[x].title;
	    setw(15);
	    cout << left << G[x].edad; 
	    setw(15);
	    cout << left << G[x].score;
	    setw(15);
	    cout << left << G[x].maxscore;
	    setw(15);
	  }
}

void Swap (assignment & a, assignment & b) // Swap function, t is a temp var
{
  assignment t = a;
  a = b;
  b = t;
}

void TypeSort (assignment G[], int N)
{
  for (int next = 1; next < N; next++)
    {
      for (int p = next-1; p>0 && G[p].type>G[p+1].type; p--)
	{
	  Swap(G[p],G[p+1]);
	}		
      
    }
}
  void DateSort (assignment A[], int N)
  {	
    for (int next = 1; next < N; next++)
      {
	for (int p = next-1; p > 0 && A[p].edad>A[p+1].edad;p--)
	  {
	    Swap(A[p],A[p+1]); 
	  }
      }
  }      

void dotsplit(string line, string delimiter, vector<string>* words)
{
    int x;
    x = line.find_first_of(delimiter);
    while(x != string::npos)
      {
	if(x > 0)
	  {
	    words->push_back(line.substr(0,x));
	  }	
	line = line.substr(x+1);
	x = line.find_first_of(delimiter);
      }
    if(line.length() > 0)
      {
	words->push_back(line);
      }

}
You don't make any kind of checks in your code, do you? For instance:

1
2
3
4
5
6
7
8
9
10
11
  while(true)
    { 

      getline(input,line); // what happens if there are no more lines to read and this fails?
      dotsplit(line, " ", &words); // what happens if the line read is an empty line?
      dotsplit(words[1], "/", &ymd); // what happens if the word does not contain a '/'?
      
      G[c].type = words[0];
      G[c].date.yyyy = ymd[0];
      G[c].date.mm = ymd[1];
      G[c].date.dd = ymd[2]; 


You have been extremely lucky; a segmentation fault is the best thing that could have happened if any of those ifs in those what happens questions turned out to be true.
1
2
while(true)
//... 


Followed by what JLBorges said, this is what is causing your problem, you have no end condition.
Topic archived. No new replies allowed.