Hey guys,

I'm trying to write something to a .txt file from within my program using ofstream.

I have this function names show_results() which is a bunch of cout statements that are connected to a class and a bunch of member functions.

But the function show_results is all cout statements. How do I write the all the the function (what you would see when running the program) to a .txt file.

Thanks

Hey if anyone can help. Im trying to put the contents (cout statements) of the function show_scores to an external file named student.txt the function show_results is connected to a bunch of class member variables and functions.

i tried something like

out_stream>> show_scores();
#include <iostream>
#include <cstdlib>
#include <fstream>
using std::ofstream;
using namespace std;

double class_total;
class student
{
      public:
             void get_info(); // Acquires input for student id, name 
             void get_scores(); // Acquires input for student quiz and test scores
             void output_info(); // Displays student info and scores and grade summary
             void calc_total(); // calculates class total points
             void calc_grade(); // calculates student total points and grade
             double calc_max(double the_max, double total_points);// calculates highest points achieved
             double calc_min(double the_min, double total_points);// calculates lowest points
             double get_points();// acqures private member "total_points"
             
             
                          
      private:
             string name_first, name_last;
             char letter_grade;
             double student_num, quiz_1, quiz_2, mid_exam, final_exam;
             double total_points, percent_total;
};

student record1, record2, record3, record4, record5;
student record6, record7, record8, record9, record10;// student records
int n=0;
double average_score;
char average_grade;
void show_scores();// Displays output_info 
void class_average();// Displays class average summary
void calc_class(); // calculates class total points for grade and average
void show_max();// displays highest points
void show_min();// displays lowest points

ofstream report;//ouputs the contents of show_results to student.txt file

int main()
    {
    char ans;
    
    report.open("student.txt");
    
            n++;
            record1.get_info();
            record1.get_scores();
            record1.calc_grade();
            class_total=0;
            record1.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;

            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;       
            }
            
            n++;
            record2.get_info();
            record2.get_scores();
            record2.calc_grade();
            record2.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;
            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            }

            n++;
            record3.get_info();
            record3.get_scores();
            record3.calc_grade();
            record3.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;
            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            }
            
            n++;
            record4.get_info();
            record4.get_scores();
            record4.calc_grade();
            record4.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;
            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            }

            n++;
            record5.get_info();
            record5.get_scores();
            record5.calc_grade();
            record5.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;
            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            }

            n++;
            record6.get_info();
            record6.get_scores();
            record6.calc_grade();
            record6.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;
            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            }

            n++;
            record7.get_info();
            record7.get_scores();
            record7.calc_grade();
            record7.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;
            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            }
            
            n++;
            record8.get_info();
            record8.get_scores();
            record8.calc_grade();
            record8.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;
            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            }

            n++;
            record9.get_info();
            record9.get_scores();
            record9.calc_grade();
            record9.calc_total();
            cout << "Is there another student's scores that need to be processed?";
            cout << endl;
            cout << "Press 'y' or 'Y' for yes or 'n' for Class Summary.";
            cin >> ans;
            cout << endl;
            while (ans == 'n' || ans == 'N')
            {
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            }

            n++;
            record10.get_info();
            record10.get_scores();
            record10.calc_grade();
            record10.calc_total();
    
            show_scores();
            calc_class();
            class_average();
            system("PAUSE");
            return 0;
            
}
    
void student::get_info()
{
     cout << "Please enter the following:\n" << endl;
     cout << "Student ID number:";
     cin >> student_num;
            while (student_num < 1 || student_num > 99999)
                   {
                         cout << "Invalid Student ID number.\n";
                         cout << "Please enter again.\n";
                         cin >> student_num;
                         }
     cout << "First name:";
     cin >> name_first;
     cout << "Last name:";
     cin >> name_last;
     }
     
void student::get_scores()
{
     cout << "Quiz #1 score:";
     cin >> quiz_1;
         while (quiz_1 < 0 || quiz_1 > 25)
                   {
                         cout << "Invalid quiz score.\n";
                         cout << "Please enter again.\n";
                         cin >> quiz_1;
                         }
     cout << "Quiz #2 Score:";
     cin >> quiz_2;
         while (quiz_2 < 0 || quiz_2 > 25)
                   {
                         cout << "Invalid quiz score.\n";
                         cout << "Please enter again.\n";
                         cin >> quiz_2;
                         }
     cout << "Midterm exam score:";
     cin >> mid_exam;
         while (mid_exam < 0 || mid_exam > 50)
                   {
                         cout << "Invalid exam score.\n";
                         cout << "Please enter again.\n";
                         cin >> mid_exam;
                         }
     cout << "Final exam score:";
     cin >> final_exam;
         while (final_exam < 0 || final_exam > 100)
                   {
                         cout << "Invalid exam score.\n";
                         cout << "Please enter again.\n";
                         cin >> final_exam;
                         }
                         }
void student::calc_grade()
{
      total_points = (quiz_1 + quiz_2 + mid_exam + final_exam);
      
      
      percent_total = (100)*((quiz_1 + quiz_2 + mid_exam + final_exam)/200);
      
      
      if (percent_total >= 90)
         {
          letter_grade = 'A';
          }
      else if (percent_total >= 80)
           { 
            letter_grade = 'B';
            }
      else if (percent_total >= 70)
           {
            letter_grade = 'C';
            }
      else if (percent_total >= 60)
           {
            letter_grade = 'D';
            }
      else 
           {
            letter_grade = 'F';
                        }
            }
void student::output_info()
{
      cout << endl << "Summary:" << endl;
      cout << "ID number:";
      cout << student_num << endl;
      cout << "Name:";
      cout << name_first <<" "<< name_last << endl;
      cout << "Quiz #1 score:";
      cout << quiz_1 << endl;
      cout << "Quiz #2 score:";
      cout << quiz_2 << endl;
      cout << "Midterm exam score:";
      cout << mid_exam << endl;
      cout << "Final exam score:";
      cout << final_exam << endl;
      cout << "Total points earned:";
      cout << total_points << endl;
      cout << "Percent Total:";
      cout << percent_total << "%" << endl << endl;
      cout << "Grade:";
      cout << letter_grade << endl << endl;
      }
      

void calc_class()
{
     double percent;
     
     average_score = class_total/n;
     
     
     
     percent = 100*(average_score/200);
     
     if (percent >= 90)
         {
          average_grade = 'A';
          }
      else if (percent >= 80)
           { 
            average_grade = 'B';
            }
      else if (percent >= 70)
           {
            average_grade = 'C';
            }
      else if (percent >= 60)
           {
            average_grade = 'D';
            }
      else 
           {
            average_grade = 'F';
                        }
      }
void show_scores()
{
      record1.output_info();
      if (n>1)
      {
      record2.output_info();
      }
      if (n>2)
      {
      record3.output_info();
      }
      if (n>3)
      {
      record4.output_info();
      }
      if (n>4)
      {
      record5.output_info();
      }
      if (n>5)
      {
      record6.output_info();
      }
      if (n>6)
      {
      record7.output_info();
      }
      if (n>7)
      {
      record8.output_info();
      }
      if (n>8)
      {
      record9.output_info();
      }
      if (n>9)
      {
      record10.output_info();
      }
     }
     

void student::calc_total()
{
     class_total = class_total + total_points;
}

double student::calc_max(double the_max, double points)
{
     if (points > the_max)
     {
     the_max = points;
     }
     return the_max;
     }

double student::calc_min(double the_min, double points)
{
     if (points < the_min && points > 0)
     {
     the_min = points;
     }     
     return the_min;
     }

void class_average()
{
      cout << endl << "Class Summary" << endl << endl;
      cout << "Number of students processed: " << n << endl;
      cout << "Highest total points achieved: ";
      show_max();
      cout << endl;
      cout << "Lowest total points achieved: ";
      show_min();
      cout << endl;
      cout << "Average total points achieved: " << average_score << endl;
      cout << "Average letter grade achieved: " << average_grade << endl << endl;
      }
      
void show_max()
{
     double max = 0;
     max = record1.calc_max(max, record1.get_points());
     max = record2.calc_max(max, record2.get_points());
     max = record3.calc_max(max, record3.get_points());
     max = record4.calc_max(max, record4.get_points());
     max = record5.calc_max(max, record5.get_points());
     max = record6.calc_max(max, record6.get_points());
     max = record7.calc_max(max, record7.get_points());
     max = record8.calc_max(max, record8.get_points());
     max = record9.calc_max(max, record9.get_points());
     max = record10.calc_max(max, record10.get_points());
     cout << max;
     }
void show_min()
{
     double min = 200;
     min = record1.calc_min(min, record1.get_points());
     min = record2.calc_min(min, record2.get_points());
     min = record3.calc_min(min, record3.get_points());
     min = record4.calc_min(min, record4.get_points());
     min = record5.calc_min(min, record5.get_points());
     min = record6.calc_min(min, record6.get_points());
     min = record7.calc_min(min, record7.get_points());
     min = record8.calc_min(min, record8.get_points());
     min = record9.calc_min(min, record9.get_points());
     min = record10.calc_min(min, record10.get_points());
     cout << min;
}

double student::get_points()
{
return total_points;
}

First, declare ofstream report; within main() so it's not global. Then, for your function output_info, pass report in by reference (so you'll have to change your declaration of output_info to report this). Make sure you do the opening of your file within main() first before you pass it into anything.
Now anywhere there is a cout at the present (so cout << "ID number:"; becomes report <<"ID number:"; . Any other functions that need it, do the same thing, pass that ofstream report in by reference.

The direction of your << or >> operator should reflect whether you are taking data into a variable from a stream or outputting to a stream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.