2015-10-31 19 views
0

快速提要:我可以正確地將數據寫入文件(我已經檢查過.txt文件,它完全按照它的樣子呈現),但我只能讀回最後一個輸入的號碼。所以,如果我輸入的最後一個測驗成績是85,那麼只會讀回來。 (學生號讀回正常。) 謝謝大家!仍然在這裏學習......學習C++ - 從文件中讀取數字

while (answer == 1) 
{ 
    cout << "What is the student ID?\n"; 
    cin >> studentNumber; 
    cout << "\n"; 
    cout << "How many quizzes did you take?\n"; 
    cin >> numQuiz; 
    outputFile << "\n"; 
    outputFile << studentNumber << "\n"; 
    outputFile << "Number of quizzes: " << numQuiz << "\t" "Grades: "; 
    for (int quiz = 1; quiz <= numQuiz; quiz++) 

    { 


     cout << "Please enter the score for quiz " << quiz << "\t"; 
     cin >> score; 
     total += score; 
     // cout << "The score is " << score << " .\n"; 
     outputFile << score << "\t"; } 

    // outputFile << total; 
    cout << "Do you have a student's grades to input?\nIf yes, type 1. If no, type 0.\n"; 
    cin >> answer; 
    cin.ignore(); 


} 

outputFile.close(); 
fstream inputFile; 



inputFile.open("grades2.txt"); 
inputFile >> studentNumber; 
cout << studentNumber << "\n"; 

inputFile >> score; 
cout << score << "\n"; 

回答

1

您正在使用錯誤的操作符來讀取數據。取而代之的

inputFile << studentNumber; 

你需要使用

inputFile >> studentNumber; 
+0

我固定的和問題依然存在。無論我輸入的是循環中的最後一個分數,它都會讀出唯一的分數。我需要能夠獲得全部3個(並且最終更多,因爲它是)。 – brkville42ny