2014-03-02 16 views
0

好的,所以我必須編寫一個程序,當用戶輸入"load filename"其中"filename"是要打開的文件的象徵(例如,"input.txt")。這將讀入所有數據並存儲學生姓名,成績並計算平均值。然後,用戶可以輸入"display filename"這將顯示學生的姓氏,名字,以及他們在考勤,作業,項目,期中和決賽中的得分。輸入文件將始終具有相同的前5行與他們看起來像這樣:使用結構和ifstream創建成績簿

Attendance: 5 
    Midterm: 20 
    Final: 20 
    Homework: 15 
    Projects: 40 
    Henry, Patrick 
    Attendance: 12 15 
    Midterm: 80 100 
    Homework: 50 100 
    Homework: 60 100 
    Homework: 80 100 
    Project: 90 100 
    Project: 80 100 
    Project: 75 100 
    Final: 80 100 

第一個數字是什麼收到的學生,二是他們能有什麼可能得分。家庭作業和項目總是會超過100分,但你不知道每個人會有多少。但是,您將擁有其他類別的其中一個。最後一個類別總是最終的,它可能會或可能不會被另一個學生跟隨。將不會有超過100名學生。我寫了這個代碼:

這是我的頭文件:

struct student 
{ 
    string last; 
    string first; 
    double attgrade; 
    double midgrade; 
    double hwgrade; 
    double projgrade; 
    double fingrade; 
    int attendpos; 
    int attendgot; 
    int midpos; 
    int midgot; 
    int finpos; 
    int fingot; 
    int hw[20]; 
    int proj[20]; 
    double average; 
}; 

我有這個問題,但是,該代碼將停止輸入"load filename"我穿上後運行」不知道爲什麼。另外,我還沒有完成分類部分,所以如果聲明不要擔心。我已經包括了所有必要的庫。但是,爲什麼在你輸入第一個命令後程序停止運行?

編輯: 此外,這個問題,它也只存儲一個學生的信息出於某種原因。請幫幫我!

任何幫助將不勝感激!

謝謝!

+0

它看起來像你的代碼需要'加載'和文件名分開輸入 – Tharwen

+0

你可以在同一行或不同行輸入 – chris1995

+0

調試器是你的朋友,它可以在幾分鐘內回答你的問題 –

回答

0

我試過你的程序。 我懷疑它只是不打開所需的輸入文件。 嘗試加入以下in.open後(...

if(in.good()) 
{ 
cout << "file opened"<<endl; 
} 
else 
{ 
cout << "file not opened"<<endl; 
} 

我編譯使用msvc2008 ...並使用調試器的下面,確認你的預期程序行爲。

// test1.cpp : Defines the entry point for the console application. 
#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <fstream> 

using namespace std; 

struct student 
{ 
    string last; 
    string first; 
    double attgrade; 
    double midgrade; 
    double hwgrade; 
    double projgrade; 
    double fingrade; 
    int attendpos; 
    int attendgot; 
    int midpos; 
    int midgot; 
    int finpos; 
    int fingot; 
    int hw[20]; 
    int proj[20]; 
    double average; 
}; 

int main(int argc, char* argv[]) 
{ 

    int numStudents = 0; 
    int projCount = 0; 
    int hwCount = 0; 
    student students[100]; 
    string command, filename; 
    cout << "What do you want to do?" << endl; 
    cin >> command; 
    ifstream in ; 
    cin >> filename; 
    string Attendance, Midterm, Final, Homework, Projects; 
    int A, M, F, H, P; 
    while (! in .fail()) 
    { 
     if (command == "Load" || command == "load" || command == "l") 
     { in.open(filename.c_str()); 
     if(in.good()) 
     { 
       cout << "file opened"<<endl; 
     } 
        else 
        { 
        cout << "file opened"<<endl; 
        } 
      in >> Attendance ; 
      in >> A ; 
      in >> Midterm >> M >> Final >> F >> Homework >> H >> Projects >> P; 
      string lname, fname; in .ignore(200, '\n'); 
      getline(in , lname, ','); 
      getline(in , fname, '\n'); 
      while (! in .fail()) 
      { 
       string cat; 
       int score, possible; in >> cat; in >> score >> possible; 
       while (cat != "Final:") 
       { 
        students[numStudents].last = lname; 
        students[numStudents].first = fname; 
        if (cat == "Attendance:") 
        { 
         students[numStudents].attendpos = possible; 
         students[numStudents].attendgot = score; 
         students[numStudents].attgrade = (score * A)/possible; 
        } 
        else if (cat == "Midterm:") 
        { 
         students[numStudents].midpos = possible; 
         students[numStudents].midgot = score; 
         students[numStudents].midgrade = (score * M)/possible; 
        } 
        else if (cat == "Homework:") 
        { 
         students[numStudents].hw[hwCount] = score; 
         hwCount++; 
         students[numStudents].hwgrade = (score * H)/100; 
        } 
        else if (cat == "Project:") 
        { 
         students[numStudents].proj[projCount] = score; 
         projCount++; 
         students[numStudents].projgrade = (score * P)/100; 
        } in >> cat; in >> score >> possible; 
       } in >> score >> possible; 
       students[numStudents].finpos = possible; 
       students[numStudents].fingot = score; 
       students[numStudents].fingrade = (score * F)/possible; 
       students[numStudents].average = students[numStudents].fingrade + students[numStudents].hwgrade + students[numStudents].projgrade + students[numStudents].attgrade + students[numStudents].midgrade; 
       numStudents++; in .ignore(200, '\n'); 
       getline(in , lname, ','); 
       getline(in , fname, '\n'); 
      } 
      for (int i = 0; i < numStudents; i++) 
      { 
       cout << students[i].average << endl; 
      } 
     } 
     else if (command == "Display" || command == "display" || command == "d") 
     { 
      cout << "Displayed: " << filename << endl; 
      cout << "#Students:" << numStudents << endl; 
      cout << "Last Name" << " First Name" << " Attendance"; 
      for (int i = 0; i < hwCount; i++) 
      { 
       cout << "HW" << i << " "; 
      } 
      for (int i = 0; i < projCount; i++) 
      { 
       cout << "Proj" << i << " "; 
      } 
      cout << " Midterm"; 
      cout << " Final" << endl; 
      for (int i = 0; i < numStudents; i++) 
      { 
       cout << students[i].last << " " << students[i].first << " " << students[i].attendgot; 
       int j = 0; 
       while (j < numStudents) 
       { 
        for (int i = 0; i < hwCount; i++) 
        { 
         cout << students[j].hw[hwCount] << " "; 
        } 
        for (int i = 0; i < projCount; i++) 
        { 
         cout << students[i].proj[projCount] << " "; 
        } 
        j++; 
       } 
       cout << students[i].midgot << " " << students[i].fingot << " " << students[i].average << endl; 

      } 
     } 
     else if (command == "Sort" || command == "sort" || command == "s") 
     { 
      cout << "A" << endl; 
     } 
     else 
     { 
      cout << "I didn't recognize that" << endl; in >> command; 
     } 
    } 
} 
+0

它打開正確的文件,因爲它輸出正確平均,程序只是因爲某種原因停止運行 – chris1995