My Computer Forum Computer Science Forum

Go Back   My Computer Forum > Computer Science Forum > Programming

Programming Programming Languages, Interpreters, and Compilers - Assembly, Declarative, Machine, Object Oriented, Iterative


Reply
 
LinkBack Thread Tools Display Modes
May 9th, 2008, 07:36 AM   #1
 
Joined: Apr 2008
Posts: 8
output data to a file cannot work!!

sorry.. my code below got no compilation errors.. but after i compile and run my codes.. the output file is empty.. i would like to output the course info including the course name, course number and the number of students enroll on the particular course into another text file. hope somebody can help me out with this.. thanks alot!!!

Code:
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;
class personType
{
      public:
             void printName()const;
             void setName(string first,string last);
             string getFirstName();
             string getLastName();
             personType(string first,string last);
      private:
             string firstName;
             string lastName;
};

void personType::printName()const
{
     cout<<firstName<<" "<<lastName;
}
 
void personType::setName(string first,string last)
{
     firstName=first;
     lastName=last;
}

string personType::getFirstName()
{
     return firstName;
}

string personType::getLastName()
{
     return lastName;
}

personType::personType(string first,string last)
{
     firstName=first;
     lastName=last;
}
                         
class courseType
{
      public:
             void setCourseInfo(string cName,string cNo,int credits);
             void printCourse();
             int getCredits();
             string getCourseNumber();
             string getCourseName();
             courseType(string cName="",string cNo="",int credits=0);
      private:
             string courseName;
             string courseNo;
             int courseCredits;
};

void courseType::setCourseInfo(string cName,string cNo,int credits)
{
    courseName=cName;
    courseNo=cNo;
    courseCredits=credits;
}

void courseType::printCourse()
{
    cout<<left<<setw(8)<<courseNo<<"   ";
    cout<<setw(15)<<courseName<<right;
    cout<<setw(3)<<courseCredits<<"   "; 
}

int courseType::getCredits()
{
    return courseCredits;
}

string courseType::getCourseNumber()
{
    return courseNo;
}

string courseType::getCourseName()
{
    return courseName;
}

courseType::courseType(string cName,string cNo,int credits)
{
    courseName=cName;
    courseNo=cNo;
    courseCredits=credits;
}

class studentType : public personType
{
      public:
             void setInfo(string fName,string lName,int ID,int nOfCourse,bool isTPaid,
                          courseType courses[],char cGrades[]);
             void print(ostream& outF,double tuitionRate);
             int getHoursEnrolled();
             double getGpa();
             double billingAmount(double tuitionRate);
             studentType():personType("a","b")
             {
               numberOfCourses=0;
               sId=0;
               isTuitionPaid=false;
    
               for(int i=0;i<6;i++)
               coursesGrade[i]='*';
             };
             //~studentType();
             
      private:
             void sortCourses();
             int sId;
             int numberOfCourses;
             bool isTuitionPaid;
             courseType coursesEnrolled[6];
             char coursesGrade[6];
};

void studentType::setInfo(string fName,string lName,int ID,int nOfCourse,bool isTPaid,
                          courseType courses[],char cGrades[])
{
    setName(fName,lName);
    sId=ID;
    isTuitionPaid=isTPaid;
    numberOfCourses=nOfCourse;
    
    for(int i=0;i<numberOfCourses;i++)
    {
       coursesEnrolled[i]=courses[i];
       coursesGrade[i]=cGrades[i];
    }
    
    sortCourses();
}                      

void studentType::print(ostream& outF,double tuitionRate)
{
    cout<<"Student Name: "<<getFirstName()<<" "<<getLastName()<<endl;
    cout<<"Student ID: "<<sId<<endl;
    cout<<"Number of courses enrolled: "<<numberOfCourses<<endl<<endl;
    
    cout<<left;
    cout<<"Course No"<<setw(15)<< " Course Name"<<setw(8)<<"Credits"
        <<setw(6)<<"Grade"<<endl;
    cout<<right;
    
    for(int i=0;i<numberOfCourses;i++)
    {
       coursesEnrolled[i].printCourse();
       
       if(isTuitionPaid)
         cout<<setw(4)<<coursesGrade[i]<<endl;
       else
         cout<<setw(4)<<"***"<<endl;
    }
    cout<<endl;
    
    cout<<"Total number of credit hours: "<<getHoursEnrolled()<<endl;
    cout<<fixed<<showpoint<<setprecision(2);
    
    if(isTuitionPaid)
      cout<<"Mid Semester GPA: "<<getGpa()<<endl;
    else
    {
        cout<<"*** Grades are being held for not paying the tuition. ***"<<endl;
        cout<<"Amount Due: $"<<billingAmount(tuitionRate)<<endl;
    }
    
    cout<<"*********************************************"<<endl<<endl;
}

int studentType::getHoursEnrolled()
{
    int totalCredits=0;
    
    for(int i=0;i<numberOfCourses;i++)
       totalCredits+=coursesEnrolled[i].getCredits();
       
    return totalCredits;
}

double studentType::getGpa()
{
    double sum=0.0;
    
    for(int i=0;i<numberOfCourses;i++)
    {
       switch(coursesGrade[i])
       {
          case 'A':
               sum+=coursesEnrolled[i].getCredits() *4;
               break;
          case 'B':
               sum+=coursesEnrolled[i].getCredits() *3;
               break;
          case 'C':
               sum+=coursesEnrolled[i].getCredits() *2;
               break;
          case 'D':
               sum+=coursesEnrolled[i].getCredits() *1;
               break;               
          case 'F':
               sum+=coursesEnrolled[i].getCredits() *0;
               break;
          default:
               cout<<"Invalid COurse Grade."<<endl;
       }
    }
    
    return sum/getHoursEnrolled();
}

double studentType::billingAmount(double tuitionRate)
{
    return tuitionRate*getHoursEnrolled();
}

void studentType::sortCourses()
{
    int i,j,minIndex;
    courseType temp;
    char tempGrade;
    string course1,course2;
    
    for(i=0;i<numberOfCourses-1;i++)
    {
       minIndex=i;
       
       for(j=0;j<numberOfCourses;j++)
       {
          course1=coursesEnrolled[minIndex].getCourseNumber();
          course2=coursesEnrolled[j].getCourseNumber();
          
          if(course1>course2)
          {         
            minIndex=j;}
          }
       
       temp=coursesEnrolled[minIndex];
       coursesEnrolled[minIndex]=coursesEnrolled[i];
       coursesEnrolled[i]=temp;
       
       tempGrade=coursesGrade[minIndex];
       coursesGrade[minIndex]=coursesGrade[i];
       coursesGrade[i]=tempGrade;
    }
}
     
//const int MAX_NO_OF_STUDENTS=10;

void getStudentData(ifstream& infile,studentType studentList[],
                    int numberOfStudents);
void printGradeReports(ofstream&,studentType studentList[],
                       int numberOfStudents,double tuitionRate);

int main()
{
    studentType studentList[10];
    
    int noOfStudents;
    double tuitionRate;
    ifstream infile;
    ofstream outfile1;
    ofstream outfile2;
    
    infile.open("stData.txt");
    if(!infile)
    {
      cout<<"Error";
      return 1;
    }
    
    outfile1.open("stDataOut.txt");
    outfile2.open("courseinfo.txt");
    
    infile>>noOfStudents;
    infile>>tuitionRate;
    getStudentData(infile,studentList,noOfStudents);
    //printGradeReports(outfile1,studentList,noOfStudents,tuitionRate);
    outfile1<<studentList;
    outfile<<cNo<<cName<credits;
    
    system("pause");
    return 0;
} 

void getStudentData(ifstream& infile,studentType studentList[],
                    int numberOfStudents)
{
    string fName;
    string lName;
    int ID;
    int noOfCourses;
    char isPaid;
    bool isTuitionPaid;
    string cName;
    string cNo;
    int credits;
    courseType courses[6];
    char cGrades[6];
    
    for(int count=0;count<numberOfStudents;count++)
    {
       infile>>fName>>lName>>ID>>isPaid;
       
       if(isPaid=='Y')
         isTuitionPaid=true;
       else
         isTuitionPaid=false;
       
       infile>>noOfCourses;
       
       for(int i=0;i<noOfCourses;i++)
       {
          infile>>cName>>cNo>>credits>>cGrades[i];
          courses[i].setCourseInfo(cName,cNo,credits);
       }
       
       studentList[count].setInfo(fName,lName,ID,noOfCourses,isTuitionPaid,courses,cGrades);
    }
} 

void printGradeReports(ofstream& outfile1,studentType studentList[],
                       int numberOfStudents,double tuitionRate)
{
    for(int count=0;count<numberOfStudents;count++)
       studentList[count].print(outfile1,tuitionRate);
}
jacksonwee is offline  
 

My Computer Forum is free to register and we welcome everyone!

May 11th, 2008, 12:59 PM   #2
 
Joined: Dec 2007
Posts: 232
Re: output data to a file cannot work!!

I note that you commented out printGradeReports. Is that, perhaps, related?
CRGreathouse is offline  
May 11th, 2008, 08:06 PM   #3
 
Joined: Apr 2008
Posts: 8
Re: output data to a file cannot work!!

Yeah.. That is the part that canno work
jacksonwee is offline  
May 12th, 2008, 05:48 PM   #4
 
Joined: Dec 2007
Posts: 232
Re: output data to a file cannot work!!

Will it not compile with that, or what? Why is it commented out?

Regardless, you should put some test code in there so we can see what's happening. Print out the values of key variables at various intervals and see if they match the expected values. Post anything you find here.
CRGreathouse is offline  
April 30th, 2010, 01:03 PM   #5
 
Joined: Apr 2010
Posts: 96
Re: output data to a file cannot work!!

Rule of thumb:
after each line print all changed variables, and the value expected...that way we can help you much easier.
asbo is offline  
Reply

  My Computer Forum > Computer Science Forum > Programming

Tags
data, file, output, work



Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does Anti Virus Work? ellawilliam3 Computer Hardware 1 May 31st, 2013 10:22 AM
My printer won't work Vicony22 Computer Peripherals 1 November 10th, 2012 12:50 AM
Can't get new computer to work... CRGreathouse OS Architecture 9 March 2nd, 2010 05:26 PM
Will this video card and ram work with what i already have? tyonna Computer Hardware 2 November 15th, 2009 09:57 AM
What is file system? tangyan Computer Science 0 December 31st, 1969 04:00 PM





Copyright © 2018 My Computer Forum Forum. All rights reserved.