Thread: Help with C++ program

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    2

    Help with C++ program

    Okay I have a c++ program due tomorrow and I am stuck with a part of it and I have tried many different ways of doing it but nothing seems to work. Any help would be greatly appreciated.

    well idea of the program is to transfer information from an input file which I named grades.txt to an output file which is called grades2.txt. The information in the grades.txt file contains a last name then first name then right beside it 10 grades. I'll give you and example:

    Dunn Larry 100 85 75 68 90 80 75 89 80 100

    Watts Sue 100 89 90 76 89 90 87 78 80 100

    and it keeps on going like that.

    My output file is suppose to have all the information from the grades.txt file plus the average at the end of each persons grade.

    My problem is that I am not able to get all the variable,the average at the end of each persons grade, and the spacings correct.

    I will post my c++ code to show what I am and to see if anybody can correct what I'm doing wrong.



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


    void grade_calc (ifstream& fin, ofstream& fout);

    int main()
    {

    ifstream fin;
    ofstream fout;
    char grades1;
    fin.open("c:\\grades.txt");
    if (fin.fail())
    {
    cout<<"Input file failed to open.\n";
    exit(1);
    }
    fout.open("c:\\grades2.txt");
    if (fout.fail())
    {
    cout<<"Output file failed to open.\n";
    exit(1);
    }


    grade_calc(fin, fout);



    return 0;
    }

    void grade_calc (ifstream& fin, ofstream& fout)
    {
    char symbol[20], symbol2[20];
    double next,average, sum=0;
    int count;
    fin>>symbol>>symbol2;
    fout<<symbol<<symbol2;
    while (fin>>next)
    {
    fin>>next;
    fout<<next;
    }



    return;
    }

  2. #2
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    couldn't u have it add all the grades and divide by how many?

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    2
    I did try that i got my loop in my function to look like this:

    fin<<last_name<<first_name;

    fout<<last_name<<first_name

    while (fin>>next)
    {
    fout<<next;
    fout<<" ";
    sum=sum+next;
    count++;
    average=sum/count;
    }

    fout<<average;


    loop does well for only the first line of grades but I dont know how to make it repeat for the rest of the lines of grades. Any suggestions?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    reset your variables after you are finished with the first line...

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Try and follow this it should work

    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <cstdlib>
    void calcScore(ifstream& input, ofstream& output);
    int main()
    {
    	ifstream Gradebook1;
    	ofstream Gradebook2;
    
    	Gradebook1.open("grades.dat");
    	if(Gradebook1.fail())
    	{
    		cout<<"Error opening Input File.\n";
    		exit(1);
    	}
    
    	Gradebook2.open("grades2.dat");
    	if (Gradebook2.fail())
    	{
    		cout<<"Error opening Output File.\n";
    	}
    
    	calcScore (Gradebook1, Gradebook2);
    	Gradebook1.close();
    	Gradebook2.close();
    	return 0;
    }
    
    
    void calcScore(ifstream& input, ofstream& output)
    {
    	char full_name [40];
    	int mark;
    	int counter=0;
    	double sum=0;
    
    	while (counter < 5)
    	{
    		for (int i=0;i<12;i++)
    		{
    			if(i<=1)
    			{
    					input>>full_name;
    					output<<full_name<<" ";
    			}
    			else
    			{
    				input>>mark;
    				sum+=mark;
    				output<<mark;
    			}
    		}
    		output<< (sum/10)<<endl;
    		sum =0;
    		counter++;
    	}
    }
    
    
    
    
    And for the files try using
    grades.dat  (Before program is run)
    
    Parker Peter 80 76 90 56 72 65 78 98 84 62
    Kent Clark 65 76 82 45 92 83 72 75 80 69 
    Lang Lana 98 92 89 83 76 77 65 62 88 76
    Wayne Bruce 90 98 65 45 78 92 92 58 67 74
    Kyle Helena 65 64 82 54 58 52 62 45 90 79
    
    
    	Grades2.dat (After program is run)
    
    Parker Peter 80 76 90 56 72 65 78 98 84 62 76.1
    Kent Clark 65 76 82 45 92 83 72 75 80 69 73.9
    Lang Lana 98 92 89 83 76 77 65 62 88 76 80.6
    Wayne Bruce 90 98 65 45 78 92 92 58 67 74 75.9
    Kyle Helena 65 64 82 54 58 52 62 45 90 79 65.1
    don't ask about the names!
    C++ can hurt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM