2010-10-11 66 views
0

新代碼時,排隊的事情了....如何輸出到C++中的文件

#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include <iomanip> 

using namespace std; 

void gradeg (double & average, string & grade) 
{ 
    if (average >= 90) 
     { 
      grade = "A"; 
     } 
     else if ((average < 90) & (average >= 80)) 
     { 
      grade = "B"; 
     } 
     else if ((average < 80) & (average >= 70)) 
     { 
      grade = "C"; 
     } 
     else if ((average < 70) & (average >= 60)) 
     { 
      grade = "D"; 
     } 
     else 
     { 
      grade = "F"; 
     } 
} 

void totalg (int & total, int & s1, int & s2, int & s3) 
{ 
    total=(s1+s2+s3); 
} 

void averageg (double & average, int & total) 
{ 
    average=total/3.0; 
} 

int main() 
{ 
    ifstream fin; 
    ofstream fout; 

    fout.setf (ios::fixed); 
    fout.setf (ios::showpoint); 
    fout.precision (2); 

    char filename[15]; 

    fin.open("input.txt"); 

    cout << "Please enter the name of your output file: "; 
    cin >> filename; 

    fout.open(filename); 

    if (fin.fail()) 
    { 
     cout << "Failed to open INPUT file." << endl; 
     exit(1); 
    } 
    if (fout.fail()) 
    { 
     cout << "Failed to open OUTPUT file." << endl; 
     exit(1); 
    } 

    string grade, name; 
    int s1, s2, s3, total=0; 
    double average=0.0; 


    fout << left << "Names" << setw(24) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade \n"; 

    cout << "Reading from input files....."; 

    while (! fin.eof()) 
    { 
     fin >> name >> s1 >> s2 >> s3; 

     totalg (total, s1, s2, s3); 

     averageg (average, total); 

     gradeg (average, grade); 


     fout << left << name << setw(24) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n"; 

    } 

    cout << "Your output file has been created and the computation results have been stored."; 

    return 0; 
} 

新的輸出...

NamesScore 1     Score 2 Score 3 Total  Average Grade 
    DAN100      70  85  255  85.00  B   
JANE78      82  90  250  83.33  B   
PETER82      84  91  257  85.67  B   
MINIE98      100  75  273  91.00  A   
JOSEPH71      62  100  233  77.67  C   
CHRISTOPHER91      75  82  248  82.67  B   
BEN54      84  77  215  71.67  C   

回答

3

你正在做一個很簡單的錯誤,這是一個格式化操縱(如setw)具有之前來它格式化數據,就像這樣:

fout << left << setw(24) << "Names" << setw(10) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade \n"; 
fout << left << setw(24) << name << setw(10) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n"; 
//    ^^^^^^^^^^^ NOTE THIS PART 

Names將佔用5個字符,並且之後將有19個空格。接下來,您將看到Score 1(7個字符),後面跟着3個空格,等等。

Names     Score 1 Score 2 
Ken Bloom    75  100  
Alec     100  75   
+0

好吧,我更新了我的代碼並輸出了...仍然有問題...任何想法? – Zud 2010-10-11 01:08:54

+0

你沒有給名稱 – 2010-10-11 01:11:09

+0

稱呼setw非常感謝。 – Zud 2010-10-11 01:13:30

3

使用「左」機械手的名字,那麼「正確的」機械手的數字:

fout << left << setw(19) << "Names" << right << setw(10) << "Score 1" << setw(10) << "Score 2" << setw(10) << "Score 3" << setw(10) << "Total" << setw(10) << "Average"<< setw(10) << "Grade" << '\n'; 

fout << left << setw(19) << name << right << setw(10) << s1 << setw(10) << s2 << setw(10) << s3 << setw(10) <<total << setw(10) << average << setw(10) << grade << "\n"; 

這是我得到的輸出:

Names     Score 1 Score 2 Score 3  Total Average  Grade 
DAN      100  70  85  255  85.00   B 
JANE      78  82  90  250  83.33   B 
PETER      82  84  91  257  85.67   B 
MINIE      98  100  75  273  91.00   A 
JOSEPH      71  62  100  233  77.67   C 
CHRISTOPHER    91  75  82  248  82.67   B 
BEN      54  84  77  215  71.67   C 
BEN      54  84  77  215  71.67   C 
+0

這什麼也沒做,當我進入它林不知道爲什麼.... – Zud 2010-10-11 00:47:01

+0

這是奇怪的,因爲我複製你的代碼,並改變了輸出線,以礦(以上更新),它一字排開完美。也許你犯了一個錯字?嘗試複製我的線路。 – 2010-10-11 01:06:50

+0

得到它謝謝! – Zud 2010-10-11 01:11:36