program terminates with status -1073741510
Apr 28, 2010 at 7:46pm UTC
I can't seem to figure out why my program won't run. compiles with no errors but windows error pops up and says that program has stopped working. Needless to say the program never really starts running. Could someone point me in the right direction?
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
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#include <fstream>
using namespace std;
const int MAX=25;
struct employee
{
string fname;
string lname;
int id_num;
char status;
double pay_rate;
double hrs_worked;
double amt_earned;
};
void fixame(string&);
void employee_data(employee[],int &);
void print_report(employee[],int );
int main()
{
employee database[MAX]; //list of employees
int n;
employee_data(database,n); //number of employees
print_report(database,n);
return 0;
}
void fixname(string& word)
{
int wlen;
wlen=word.length();
word[0]=toupper(word[0]);
for (int i=1; i<wlen; i++)
word[i]=tolower(word[i]);
}
void employee_data(employee list[],int & n)
{
ifstream infile;
string filename;
cout << "Please enter name of employee data file:" << endl;
cin >> filename;
infile.open(filename.c_str());
n=0;
infile >> list[n].fname;
fixname(list[n].fname);
while (!infile.eof())
{
infile >> list[n].lname;
fixname(list[n].lname);
infile >> list[n].id_num;
infile >> list[n].status;
infile >> list[n].pay_rate;
infile >> list[n].hrs_worked;
infile >> list[n].amt_earned;
n++;
infile >> list[n].fname;
}
infile.close();
}
void print_report(employee list[],int n)
{
for (int i=0; i<n; i++)
{
cout << list[n].lname << " " << list[n].fname
<< " " << list[n].id_num << " " << list[n].status
<< " " << list[n].pay_rate << " " << list[n].hrs_worked
<< " " << list[n].amt_earned << endl;
}
}
Apr 28, 2010 at 10:21pm UTC
Hey there... first off... It's really bad practice to pass an uninitialized int to a function and then in the function set it to zero ;-)
There seems to be a segmentation fault in fixname...
You'll have to check whether the file has opened correctly... else you will pass an empty string which produces a segfault at line 39...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt" );
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file" ;
return 0;
}
(code actually from
http://www.cplusplus.com/doc/tutorial/files/)
That's how it should look like...
Hope that helped...
Topic archived. No new replies allowed.