2013-08-02 68 views
0

我得到這個程序來計算相應的字母等級的等級,並且我得到了它循環多次的用戶想要輸出。但是,由於某些原因,它只會將最後已知的輸出寫入其文本文件。任何人都可以告訴我我在這裏做錯了什麼?如何從循環寫入多個輸出?

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

int weighted1 = 0; 
int weighted2 = 0; 
int weighted3 = 0; 
int weighted4 = 0; 
int weighted_average = 0; 

const int MAX = 20; 

int flag = 0; 
int choice; 
double sum = 0; 
double average = 0; 
char name [10]; 
char letter; 
char file [MAX]; 

int num = 0; 
int count = 0; 

int main() 
{ 

//Initiate input/output stream 
std::ifstream in_stream; 
std::ofstream out_stream; 

in_stream.open("grade.txt"); 
out_stream.open("finalgrade.dat"); 

double first, second, third, fourth; 
in_stream >> first >> second >> third >> fourth >> name; 
//std::cout >> " 1: " >> first >> " 2: " >> second >> 
double grade = 0.0; 
grade = (first + second + third + fourth)/4; 

//Gives user the choice of reading student records from keyboard or file 
bool menu = true; 
while (menu != false) 
{ 
     std::cout << "Would you like to open as keyboard or file?" << '\n'; 
std::cout << "1. keyboard" << '\n'; 
std::cout << "2. file" << '\n'; 

std::cin >> choice; 

switch (choice) 
{ 
//Enter the number students the grades will enter 
case 1: 
std::cout << "How many students? "; 
std::cin >> num; 
for(count =0; count < num; count++) 
{ 
    { 
    std::cout << "Student's Name: "; 
    std::cin >> name; 
    } 
do 
{ 
flag = 0; 
std::cout << "Please input your first exam grade and press enter: \n"; 
    std::cin >> first; 
    if ((first < 0) || (first > 100)) 

    { 
     std::cout << "You've entered invalid data!" << '\n'; 
     flag = 1; 
    } 
}while (flag == 1); 

do 
{ 
flag = 0; 
std::cout << "Please input your second exam grade and press enter: \n"; 
    std::cin >> second; 
    if ((second < 0) || (second > 100)) 

    { 
     std::cout << "You've entered invalid data!" << '\n'; 
     flag = 1; 
    } 
}while (flag == 1); 

do 
{ 
flag = 0; 
std::cout << "Please input your third exam grade and press enter: \n"; 
    std::cin >> third; 
    if ((third < 0) || (third > 100)) 

    { 
     std::cout << "You've entered invalid data!" << '\n'; 
     flag = 1; 
    } 
}while (flag == 1); 

do 
{ 
flag = 0; 
std::cout << "Please input your final exam grade and press enter: \n"; 
    std::cin >> fourth; 
    if ((fourth < 0) || (fourth > 100)) 

    { 
     std::cout << "You've entered invalid data!" << '\n'; 
     flag = 1; 
    } 

}while (flag == 1); 

//Formulas that calculate student average 
grade = (first + second + third + fourth)/4; 
sum = first + second + third + fourth; 
average = sum/4; 

//Letter grade and it's weighted averages 
letter = 'A'; 
letter = 'B'; 
letter = 'C'; 
letter = 'D'; 
letter = 'F'; 

if(grade >= 90) 
{ 
letter = ('A'); 
std::cout<<letter<<'\n'; 
} 
else if(grade >= 80) 
{ 
letter = ('B'); 
std::cout<<letter<<'\n'; 
} 
else if(grade >= 70) 
{ 
letter = ('C'); 
std::cout<<letter<<'\n'; 
} 
else if(grade >= 60) 
{ 
letter = ('D'); 
std::cout<<letter<<'\n'; 
} 
else if (grade < 60) 
{ 
letter = ('F'); 
std::cout<<letter<<'\n'; 
} 
weighted1 = (first * .20); 
weighted2 = (second * .20); 
weighted3 = (third * .20); 
weighted4 = (fourth * .40); 
weighted_average = (weighted1 + weighted2 + weighted3 + weighted4); 

//Output 
std::cout << "Exam Grades: " << first << "," << second << "," << third << "," << fourth << '\n'; 
std::cout << "This is the average for " << name << ": " << weighted_average << '\n'; 
std::cout << "This is the letter grade: " << letter << '\n'; 
    } 
    { 
//Writing the grade into grades.txt 
    for(count =0; count < num; count++) 
    { 
    std::ofstream myfile; 
    myfile.open ("grades.txt"); 
    myfile << "Writing this to a file: "; 
    myfile << name << ' '; 
    myfile << weighted_average << ' '; 
    myfile << letter << '\n'; 
    myfile << "****"; 
    myfile.close(); 

    } 
    break; 
    } 

//Here we open "grade.txt" to output grade to screen 
case 2: 

in_stream.open("grade.txt"); 
out_stream.open("finalgrade.dat"); 

letter = 'A'; 
letter = 'B'; 
letter = 'C'; 
letter = 'D'; 
letter = 'F'; 

if(grade >= 90) 
letter = ('A'); 

else if(grade >= 80) 
letter = ('B'); 

else if(grade >= 70) 
letter = ('C'); 

else if(grade >= 60) 
letter = ('D'); 

else if (grade < 60) 
letter = ('F'); 

weighted1 = (first * .20); 
weighted2 = (second * .20); 
weighted3 = (third * .20); 
weighted4 = (fourth * .40); 
weighted_average = (weighted1 + weighted2 + weighted3 + weighted4); 

std::cout << "Enter file name: "; 
    std::cin >> file; 
if(file != "grade.txt") 
{ 
    std::cout << std::fixed << "The average grade for: " << name << '\n'; 
    std::cout << "average in grade.txt is: "<< weighted_average << std::setprecision(2) << '\n'; 
    std::cout << "and the letter grade is: " << letter << '\n'; 
} 
else 
{ 
    return 0; 
} 

in_stream.close(); 
out_stream.close(); 
} 

return 0; 
} 
} 
+2

嘗試打開和關閉文件之外循環。我懷疑它是覆蓋,而不是在每次迭代中重新打開文件時追加。 – Michelle

+0

@Michelle爲什麼不把它作爲一個實際的答案,所以我可以upvote呢? – doctorlove

+0

@doctorlove完成。 – Michelle

回答

0

如果你想輸出很多東西我建議你通過循環

迭代將您想要的輸出給一個變量,循環完成輸出後變量 例

var output 

while(true) { 
    add to output 
} 

print output 
1

編輯:這裏更嚴重的問題是您只存儲最後一次輸入。您應該創建一個對象來存儲每個學生的所有數據(例如一個Student對象),創建一個學生數組,然後循環訪問該數組以在輸入完所有信息後打印信息。我已經將下面的代碼更新爲對象數組的樣子。如果你不知道任何面向對象的編程概念,你也可以把每一塊數據(名字,字母等級,平均等等)放在一個數組中,其中每一個數據中的第0個元素都代表一個數組(每個元素都代表一個數組)學生,第一名代表另一位等,這不是好的做法;創建一個對象來存儲關於學生的信息是一個更好的主意。

原文:您正在覆蓋您的文件,而不是通過在循環的每次迭代中打開和關閉它來追加它。

取而代之的是,打開您的文件在循環之前和之後關閉它,就像這樣:

{ 
    //Writing the grade into grades.txt 
    std::ofstream myfile; 
    myfile.open ("grades.txt"); 
    for(count =0; count < num; count++) 
    { 
    myfile << "Writing this to a file: "; 
    myfile << students[count].name << ' '; 
    myfile << students[count].weighted_average << ' '; 
    myfile << students[count].letter << '\n'; 
    myfile << "****"; 
    } 
    myfile.close(); 
} 
+0

這有助於多次寫入該行,但現在它會複製上次已知的輸入。我會試着看看這是爲什麼。 – hello

+0

哦,我剛剛意識到你根本沒有使用'count'的值。 – Michelle

+0

@hello我已更新我的回答 – Michelle