2017-07-31 166 views
1

學校任務。必須基本上爲駕駛員考試創建一個測試鑰匙,然後驗證學生是否通過了考試。正確的答案將被存儲在一個數組中,學生的答案和全名將被存儲在用戶輸入的另一個數組中,並寫入一個文本文件。(C++)將數組內容寫入文件時出現問題

有一段時間我以爲我在做什麼是正確的,直到我去運行該程序,並注意到無論數組長度多長時間,它都會寫出一個奇怪的I字符,並且上面有一個口音。它還將數組的正確答案寫入我不想做的文件中。所有我需要寫入的文件是:(全名,學生答案(20),全名,學生答案(20)等)。

該文件的第一個字母也由於某種原因被切斷。 (例如「John Doe」變成「ohn Doe」)這個錯誤的原因是什麼?沒有cin.ignore()語句我不知道如何獲得我需要的全名到文件中。

我已經將數組大小設置爲常數,允許我將其更改爲4個空格而不是20個,以便進行快速測試。

任何幫助表示讚賞。編程極其新穎。有時享受它有時很難。

我的代碼:

#include <iostream> 
#include <string> 
#include <fstream> 
using namespace std; 

void Correct_Answers(const int SIZE, char correctAnswers[]); 
void Submitted_Answers(const int SIZE, char submittedAnswers[]); 
void Get_Name(string &fullName); 



int main() 
{ 
    const int SIZE = 4; 
    char correctAnswers[SIZE]; 
    char submittedAnswers[SIZE]; 
    string fileName; 
    string fullName; 
    char go = 'Y'; 
    ofstream outputObj; 

    Correct_Answers(SIZE, correctAnswers); 

    cout << "\nEnter the file name: "; 
    cin.ignore(); 
    getline(cin, fileName); 
    outputObj.open(fileName); 


    while (go == 'Y' || go == 'y') 
    { 
     Get_Name(fullName); 
     outputObj << fullName << endl; 

     Submitted_Answers(SIZE, submittedAnswers); 
     outputObj << submittedAnswers << endl; 

     cout << "\nTo process another user enter Y. To quit enter N: "; 
     cin >> go; 
    } 


    cout << "\n\n\n"; 
    system("Pause"); 
    return(0); 
} 

void Correct_Answers(const int SIZE, char correctAnswers[]) 
{ 
    int questionCounter = 0; 

    cout << "\nEnter the correct answers for the drivers exam.\n"; 

    for (int x = 0; x < SIZE; x++) 
    { 
     cout << "\tQuestion #" << ++questionCounter << ": "; 
     cin >> correctAnswers[x]; 
     while (correctAnswers[x] != 'A' && correctAnswers[x] != 'a' && correctAnswers[x] != 'B' && correctAnswers[x] != 'b' && correctAnswers[x] != 'C' && correctAnswers[x] != 'c' && correctAnswers[x] != 'D' && correctAnswers[x] != 'd') 
     { 
      cout << "\tInvalid entry. Re-enter answer: "; 
      cin >> correctAnswers[x]; 
     } 
    } 
} 

void Submitted_Answers(const int SIZE, char submittedAnswers[]) 
{ 
    int questionCounter = 0; 

    cout << "\nWelcome to the written portion of the drivers exam!"; 
    cout << "\nDo your best to answer the questions to the best of your knowledge."; 
    cout << "\n15 out of 20 are needed to pass the exam. Best of luck!\n\n"; 

    for (int x = 0; x < SIZE; x++) 
    { 
     cout << "\tQuestion #" << ++questionCounter << ": "; 
     cin >> submittedAnswers[x]; 
     while (submittedAnswers[x] != 'A' && submittedAnswers[x] != 'a' && submittedAnswers[x] != 'B' && submittedAnswers[x] != 'b' && submittedAnswers[x] != 'C' && submittedAnswers[x] != 'c' && submittedAnswers[x] != 'D' && submittedAnswers[x] != 'd') 
     { 
      cout << "\tInvalid. Re-enter answer: "; 
      cin >> submittedAnswers[x]; 
     } 

    } 

} 

void Get_Name(string &fullName) 
{ 
    cout << "\nEnter the users name: "; 
    cin.ignore(); 
    getline(cin, fullName); 
} 
+1

你是否嘗試用調試器逐句通過你的代碼? –

+0

我沒有。我會嘗試,我老實說,只是不知道如何使用調試器,但我想只有一種方法來學習。我只是使用編譯器錯誤消息來解決大部分問題。 –

+2

僅當您編寫的代碼無法轉換爲可執行文件時纔會發出編譯器錯誤。並非所有編譯的程序都能正常工作,甚至可以定義的方式工作。您可以使用調試器逐行檢查它是否正常工作。 –

回答

0

當你寫的文件你得到的垃圾的原因是,當你在文件中,ofstream的對象使用>>操作,實際上是傳遞一個指針,它只是在閱讀字符,直到找到行尾或空白。例如:

int main() 
{ 
    char submittedAnswers[4] = { 'a', 'b', 'c', 'd' }; 
    char * memptr = submittedAnswers; // Points to "abcdÌÌÌ̤ònÉÈø+=" 
    ofstream outputObj ("d:/filetest.txt"); 

    outputObj << submittedAnswers << endl; 
} 

這就是爲什麼它將垃圾打印到文件。

爲什麼你失去「李四」的第一個字母的原因是因爲你做的事:

cin.ignore(); // This discards the first character. 
getline(cin, fullName); 

那麼,爲什麼你做cin.ignore()?因爲你覺得你需要添加cin.ignore()讀取線爲它工作之前:

cout << "\nEnter the file name: "; 
cin.ignore(); // You've added this because otherwise getline(cin, fileName) 
       // ends up empty 
getline(cin, fileName); 
outputObj.open(fileName); 

爲什麼函數getline()返回一個空字符串,如果你離開了cin.ignore()?因爲在調用cin緩衝區時,仍然存在'\ n'換行符。

當你把你的答案:

cin >> correctAnswers[x]; 

在一個循環。每當該行執行時,默認跳過空格和新行。當你輸入一個答案時,你輸入一些內容然後按回車。當你按回車鍵時,它會向緩衝區添加一個'\ n'。所以命令行輸入被插入到correctAnswers []中,但是在最後一個cin >>中,最後以'\ n'結尾,這就是爲什麼在調用getline()之前必須調用cin.ignore的原因。 ,在緩衝區中跳過這個'\ n'字符。