Hi there,

I'am asked for a HW write a code:

* read some data of unknown number of students with their grades of 4 courses from a file
* calculate the gpa, honor/warning status, and course average for each course,
* write them into an output file (for testing I cout them only here) in columns: student ID column, average column, honor/warning status column, after all these triple columns end with full of their data, for the following line I should print two more columns of course name and course average (calculated with sum of grades of all student on this course and divide by student number) and their data under the headings.
**I am asked to center all data in their columns

I tried the code below but I have problem that when I use a center() function to center data, it goes to next line, thus I have only one, the only first, column with full of repeating set of data, which means I couldn't manage to create columns because of the center() funstion.

Can anybody help me on this problem please?

I looked for to stop cursor at the last position before it calls the center() function but I couldn't find a method for it.

Thnx.

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

void printHeader(int, int, int);
double calcGPA(double, double, double, double);
void printSpecialNote(double);
double calcCourseAverage();
void printEmptyFileMessage();
void center(string, int);
//void centernum (double num, int wdth);
//int IDfind();


int main ()
{  
    int ID, temp0, leng, wdth, xy;
    double GPA, temp1, temp2, temp3, temp4;
    string line, id, s1, s2, s3, s4, sGPA; 
    stringstream ss (stringstream::in | stringstream::out);
    
    ifstream readF;
    ofstream writeF;
    
    readF.open("H:\\Users\\Oliver\\Desktop\\C++\\read77run1.txt");
    
    if(!readF) 
    {
    cout << endl << "Failed to open the input file.\n\n";
    system("PAUSE");
    return 1;
    }
    
    writeF.open("H:\\Users\\Oliver\\Desktop\\C++\\HW6\\write77.txt");

    if(getline(readF, line))
    {
        printHeader(6,6,12);
        readF.close();
        readF.open("H:\\Users\\Oliver\\Desktop\\C++\\read77run1.txt");
        
        while(readF >> ID >> temp1 >> temp2 >> temp3 >> temp4)
        {
        cout      << "ID: "   << ID    << endl
                  << "Grd1: " << temp1 << endl
                  << "Grd2: " << temp2 << endl
                  << "Grd3: " << temp3 << endl
                  << "Grd4: " << temp4 << endl;
                  
        ss.clear();         
        ss << ID;    ss >> id; ss.clear(); //cout << s0 << endl;
        ss << temp1; ss >> s1; ss.clear(); //cout << s1 << endl;
        ss << temp2; ss >> s2; ss.clear(); //cout << s2 << endl;
        ss << temp3; ss >> s3; ss.clear(); //cout << s3 << endl;
        ss << temp4; ss >> s4; ss.clear(); //cout << s4 << endl;
    
        GPA=calcGPA(temp1, temp2, temp3, temp4);
        ss << GPA; ss >> sGPA; ss.clear();
        center(id,6);
        center(sGPA,6);
        printSpecialNote(GPA);
        }
        
        cout << endl << endl;
        
        readF.close();
        readF.open("H:\\Users\\Oliver\\Desktop\\C++\\read77run1.txt");
        calcCourseAverage();
       
    }
    
    else
    {
        printEmptyFileMessage();
    }
    
    writeF.close();
    readF.close();
    
    cout << endl << endl;
	system("PAUSE");
	return 0;
}

void center(string txt, int wdth)
{
     int leng, xy;
     leng=txt.length();
     xy=(wdth-leng)/2;
     for (int count=0; count<xy; count++)
         cout   << " ";
     cout << txt << endl;
 }


//Header printing
void printHeader(int clmn1, int clmn2, int clmn3)
{
   //int clmn1=6, clmn2=6, clmn3=12;
   center("Student", clmn1);
   center("GPA", clmn2);
   center("Special Note", clmn3);
}

//Printing empty file message
void printEmptyFileMessage()
{
     cout << "**EMPTY FILEā€¦NO DATA TO PROCESS**" << endl;
}

//Printing special note
void printSpecialNote(double gpa)
{
       if (gpa>=3.5) center("Honors",12);
       else if (gpa<1.5) center("Warning",12);
       else center("       ",12);
       cout << endl;
}

//GPA calculator
double calcGPA(double t1, double t2, double t3, double t4)
{
       double sum;
       sum=t1+t2+t3+t4;
       return sum/4;
}


//Course average calculation
double calcCourseAverage()
{
       int ID, count;
       double sum1, sum2, sum3, sum4, temp1, temp2, temp3, temp4;
       double Av1, Av2, Av3, Av4;
       string line, s1, s2, s3, s4;
       stringstream ss (stringstream::in | stringstream::out);
       
       ifstream readF;
       ofstream writeF;
       readF.open("H:\\Users\\Oliver\\Desktop\\C++\\read77run1.txt");
      
       count=0;
       while(readF >> ID >> temp1 >> temp2 >> temp3 >> temp4)
       {
        count++;
        cout      << "ID: "   << ID    << endl
                  << "Grd1: " << temp1 << endl
                  << "Grd2: " << temp2 << endl
                  << "Grd3: " << temp3 << endl
                  << "Grd4: " << temp4 << endl;
           
                   sum1+=temp1;
                   sum2+=temp2;
                   sum3+=temp3;
                   sum4+=temp4;
       }
       
       Av1=sum1/count;
       Av2=sum2/count;
       Av3=sum3/count;
       Av4=sum4/count;
       
       ss << Av1; ss >> s1; ss.clear(); //cout << s0 << endl;
       ss << Av2; ss >> s2; ss.clear(); //cout << s1 << endl;
       ss << Av3; ss >> s3; ss.clear(); //cout << s2 << endl;
       ss << Av4; ss >> s4; ss.clear(); //cout << s3 << endl;
       
       center("Course 1 average: ",19);
       center(s1,5);
       cout << endl;
       
       center("Course 2 average: ",19);
       center(s2,5);
       cout << endl;
       
       center("Course 3 average: ",19);
       center(s3,5);
       cout << endl;
       
       center("Course 4 average: ",19);
       center(s4,5);
       cout << endl << endl;
       
       writeF.close();
       readF.close();
    
       cout << endl << endl;
       return 0;
}

Why do you need to call the center() function at all? If I was you, I would attempt to get all the data outputted in the right order first and then tidy the formatting up at the end. I suspect that it's more important that you present the right information than have things centred :o)

There's a very suspicious "endl" being output in your center() function. Instead, I think you want to output a certain number of spaces, then the passed-in string, then another certain number of spaces, so that you've filled the width of the column.

You are right raptr. I resolved problem. Thnx all for taking time.

No trouble at all. Please mark your thread as "solved". :)

Ah yea, OK :)

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.