+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Member Duke523's Avatar
    Join Date
    Dec 2000
    Location
    Beech Grove, IN, USA
    Posts
    404

    I have a C++ program assignment

    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
    Samsung SyncMaster 2343bwx
    XFX 680i LT
    Intel Core 2 Quad Q6600
    4x OCZ SLI-Ready DDR2 800MHz 2048MB
    2x eVGA GeForce GTX 465 SC SLI
    Seagate Barracuda 7200.12 ST31000528AS 1TB
    LiteON DH20A3H
    LG GGW-H20L
    LG GSA-H10L
    Windows 7 Home Premium 64-bit

  2. #2
    Member Duke523's Avatar
    Join Date
    Dec 2000
    Location
    Beech Grove, IN, USA
    Posts
    404
    Oh by the way I am using Sun's compiler
    Samsung SyncMaster 2343bwx
    XFX 680i LT
    Intel Core 2 Quad Q6600
    4x OCZ SLI-Ready DDR2 800MHz 2048MB
    2x eVGA GeForce GTX 465 SC SLI
    Seagate Barracuda 7200.12 ST31000528AS 1TB
    LiteON DH20A3H
    LG GGW-H20L
    LG GSA-H10L
    Windows 7 Home Premium 64-bit

  3. #3
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    I know I am late, but I can help you. I will post some code as soon as I can!

  4. #4
    Ultimate Member nothing's Avatar
    Join Date
    Oct 2002
    Location
    Brazil
    Posts
    1,413
    This is my version of your program. It is not exactly similar, but hopefully it will help you.

    Code:
    #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:
    Code:
    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:
    Code:
    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

  5. #5
    Member Duke523's Avatar
    Join Date
    Dec 2000
    Location
    Beech Grove, IN, USA
    Posts
    404
    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.
    Samsung SyncMaster 2343bwx
    XFX 680i LT
    Intel Core 2 Quad Q6600
    4x OCZ SLI-Ready DDR2 800MHz 2048MB
    2x eVGA GeForce GTX 465 SC SLI
    Seagate Barracuda 7200.12 ST31000528AS 1TB
    LiteON DH20A3H
    LG GGW-H20L
    LG GSA-H10L
    Windows 7 Home Premium 64-bit

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts







New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke