//flex table opened by JP

Click to See Complete Forum and Search --> : I have a C++ program assignment


Duke523
04-11-2004, 11:52 PM
I have a C++ program assignment and when I run my program it gives me a core dump. I need some help big time especially since it is due monday. :(

Here is a copy of my program and its input file

//**********This is the comments section********************************
// Author: Chris Duke
// Email: cduke@butler.edu
// Date due: April 7th, 2004
// Program 5: This program will take an input file
// calculate the average of grades and store output
// in a output file.
//**********My comments end here****************************************

#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

int main()
{
char cont = 'y', name;
while(cont == 'y')
{
char input_file[15];
ifstream in_s;
cout<< "Please input the input file name \n";
cin>> input_file;
in_s.open(input_file);
if(in_s.fail())
{
cout<<"Input file opening failed. \n";
exit(1);
}

char output_file[15];
ofstream out_s;
cout<< "Please input the output file name \n";
cin>> output_file;
out_s.open(output_file);
if(out_s.fail())
{
cout<<"Output file opening failed. \n";
exit(1);
}

do
{
int sum=0,aver=0,grade=0;
char lastname[25], firstname[25];

in_s>>lastname>>firstname;
out_s<<lastname<<" "<<firstname<<" ";

for(int count=0; count<= 9; count++)
{
in_s>>grade;
sum = sum + grade;
out_s<<grade<<" ";
}

aver = sum / 10;
out_s<<aver<<endl;

}while(!in_s.eof());

in_s.close();
out_s.close();

cout<< "Would you like to compute another set of grades? <y/n>" <<endl;
if(cont == 'y'||'n')
{
cin>> cont;
}
else
{
cout<< "You have entered an invalid choice." <<endl;
cout<< "Would you like to compute another set of grades? <y/n>" <<endl;
cin>> cont;
}
}

cout<< "Thanks for using"<<endl<<"*^*THE GRADE COMPUTEING PROGRAM*^*" <<endl;

return 0;
}




And input file reads:

Smith John 75 86 72 90 66 82 95 57 88 80
Peterson Mary 65 80 78 91 76 72 90 50 87 85
Dalton Nick 55 46 79 98 68 82 99 87 58 100
Robinson Carla 83 76 32 80 96 62 95 77 98 81
Hamilton Petter 45 76 71 90 61 89 94 58 68 89
Gilbert Jeffrey 72 83 74 95 67 86 98 59 80 77
Bush John 95 96 82 69 76 82 95 97 98 90

Duke523
04-11-2004, 11:54 PM
Oh by the way I am using Sun's compiler

nothing
04-16-2004, 07:32 PM
I know I am late, but I can help you. I will post some code as soon as I can!

nothing
04-17-2004, 10:30 AM
This is my version of your program. It is not exactly similar, but hopefully it will help you.


#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;

int main()
{
int grade;
int total;
int grade_count;
int second_space;
double average;
string line;
string name;
string average_string;

ofstream output("output.txt", ios::out);
ifstream input("input.txt", ios::in);

if(!input){
cerr << "Error opening input.txt...";
cin.get();
exit(1);
}

output << "Name" << setw(34) << "Grade average\n" << endl;

while(!input.eof()){
getline(input, line);

second_space = line.find_first_of(' ', line.find_first_of(' ') + 1);

istringstream iss(line.substr(second_space));

name = line.substr(0, second_space);

average = 0;
grade_count = 0;
total = 0;

while(iss >> grade){
grade_count++;
total += grade;
average = static_cast<double>(total) / grade_count;
}

ostringstream oss;
oss << average;
average_string = oss.str();

output << name << setw(24 - name.length() + average_string.length()) << average << endl;
}

input.close();
output.close();

cout << "Grade averages were successfully calculated and saved to output.txt" << endl;

cin.get();


return 0;
}


This is the input file:

Smith John 75 86 72 90 66 82 95 57 88 80
Peterson Mary 65 80 78 91 76 72 90 50 87 85
Dalton Nick 55 46 79 98 68 82 99 87 58 100
Robinson Carla 83 76 32 80 96 62 95 77 98 81
Hamilton Petter 45 76 71 90 61 89 94 58 68 89
Gilbert Jeffrey 72 83 74 95 67 86 98 59 80 77
Bush John 95 96 82 69 76 82 95 97 98 90


And this is the output file:

Name Grade average

Smith John 79.1
Peterson Mary 77.4
Dalton Nick 77.2
Robinson Carla 78
Hamilton Petter 74.1
Gilbert Jeffrey 79.1
Bush John 88


If you have any questions, don't resitate to ask :t

Duke523
04-17-2004, 11:01 PM
Thanks, I do like to see others code. I finaly got it to work by extending the maximum character input for my streams and adding back the bracked lentth back to the names.