2017-04-20 45 views
0

免責聲明:我是一個初學者到編程,所以我說聽起來很愚蠢C++陣不採取正確的輸入,從文件

我必須做一個「電話簿」學校。該程序尚未完成,但在繼續之前,我需要修正一些問題。數字TelephoneNumbers要麼不正確地存儲文件中的數字,要麼不顯示它們。對於SeaerchRecords功能,文件中的第一個數字顯示正確,第二個數字顯示爲「2147483647」,其餘數字顯示爲「0」。修改函數也不會改變數字,我在函數中用while來確認它。然而,字符串數組工作得很好。有人可以解釋我做錯了嗎?

#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 

string TelephoneNames[100]; 
int TelephoneNumbers[100]; 

void ModifyRecords(); //Function to Modify Records 
void SearchRecords(); //Function to Search Records 
void DeleteRecords(); //Function to Delete Records 

int main() 
{ 
    fstream inputFile; 
    fstream outputFile; 
    char choice; 

    inputFile.open("Telephone Names.txt"); //To store 
    for (int count=0;count<100;count++)  //file names 
    {          //into a 
     inputFile >> TelephoneNames[count]; //string 
    } 
    inputFile.close(); 

    inputFile.open("Telephone Numbers.txt");//To store 
    for (int count=0;count<100;count++)  //file #'s 
    {          //into a 
     inputFile >> TelephoneNumbers[count];//string 
    } 
    inputFile.close(); 
    //Display options available 
    cout << " Hello, do you want to:\n"; 
    cout << " ======================\n"; 
    cout << "-Modify Records|Enter M\n"; 
    cout << "-Search Records|Enter S\n"; 
    cout << "-Delete Records|Enter D\n"; 
    //Store choice 
    cin >> choice; 
    //Send to different function 
    if (choice=='M'||choice=='m') 
    { 
     ModifyRecords(); 
    } 
    if (choice=='S'||choice=='s') 
    { 
     SearchRecords(); 
    } 
    return 0; 
} 

void ModifyRecords() 
{ 
    string name; 
    string newname; 
    int newnumber; 
    int count=0; 
    cout << "Enter the name of the person: "; 
    cin >> name; 
    for (count=0;TelephoneNames[count]!=name;count++)//To determine where in     the strings the new numbers need to be 
    { 

    } 
    cout << "Enter the new name of the person: "; 
    cin >> newname; 
    cout << "Enter the new number of the person: "; 
    cin >> newnumber; 
    TelephoneNames[count]={newname}; 
    TelephoneNumbers[count]={newnumber}; 
    count=0; 
    while (count<6) 
    { 
     cout << TelephoneNames[count] << endl; 
     cout << TelephoneNumbers[count] << endl; 
     cout << endl; 
     count++; 
    } 
} 

void SearchRecords() 
{ 
    string name; 
    int count=0; 
    cout << "Enter the name of the person you would like to find: "; 
    cin >> name; 
    for (count=0;TelephoneNames[count]!=name;count++)//To determine where in   the strings the new numbers need to be 
    { 

    } 
    cout << "Name: " << TelephoneNames[count] << endl; 
    cout << "Number: " << TelephoneNumbers[count] << endl; 
} 
+1

你能包括文本文件嗎? –

+0

把一些輸入文件內容 –

回答

0

由於沒有任何回答依然和我沒有看到確切的問題在這一點上,我將提供一些建議您如何找到代碼中的一個問題。 在任何編程情況下,如果找不到錯誤,首要任務就是儘可能精確地找到它,並檢查所有輸入數據和假設。通常,調試器用於這種目的,但是您可以在創建程序的最終版本之前在控制檯中輸出文本。 首先,你必須檢查你真的收到您的文件名稱和電話:當檢查

inputFile.open("Telephone Names.txt"); //To store 
for (int count=0;count<100;count++)  //file names 
{          //into a 
    inputFile >> TelephoneNames[count]; //string 
    cout << TelephoneNames[count] << endl; //WE MUST SEE WHAT IS REALLY STORED IN TelephoneNames 
} 
inputFile.close(); 

inputFile.open("Telephone Numbers.txt");//To store 
for (int count=0;count<100;count++)  //file #'s 
{          //into a 
    inputFile >> TelephoneNumbers[count];//string 
    cout << TelephoneNumbers[count] << endl; //WE MUST SEE WHAT IS REALLY STORED IN TelephoneNumbers 
} 
inputFile.close(); 

好吧,你是defenitely肯定有您的數據沒有問題,我們可以移動到SeaerchRecords功能執行相同的程序。當您搜索時,我們必須檢查發生了什麼:

for (count=0;TelephoneNames[count]!=name;count++)//To determine where in   the strings the new numbers need to be 
{ 
    cout << "Search step: " << count << " name " << name << " found name " << TelephoneNames[count] << " number " << TelephoneNumbers[count] << endl; 
} 

這樣做,您會很快找到您的錯誤。問題可以在輸入文件格式中,以「名稱」和存儲名稱格式等的差異來表示。

我會提供一些額外的建議,告訴您如何改進代碼。 1)嘗試使用const聲明來處理常用的記錄數(const int NUMBER_OF_RECORDS = 100;放在每個地方都放上'100'),它會減少工作量和可能的錯誤。 2)嘗試檢查你程序可能遇到的所有可能的問題,如果數據有問題。如果您現在的文件中的記錄少於100條,會發生什麼情況?程序粉碎或無聲讀取更糟糕的不適當數據。請檢查您是否有任何閱讀步驟到達文件末尾,以及當前檢查是否已達到您的記錄數量並在遇到不適當的數據時執行某些操作。 3)檢查循環中可能存在的問題,不要無限次運行。現在你的條件for(count=0;TelephoneNames[count]!=name;count++) 將永久執行,如果沒有這樣的名字或者只是在100或更多的數量上粉碎程序。您應該檢查計數是否不超過該值。祝你好運!

+0

謝謝!我找到了程序的錯誤。 – Andi