2017-04-20 2 views
0

免責を正しい入力を取っていない:私は、プログラミングの初心者ですので、私はC++配列は、ファイルから

本当に愚かに聞こえるかもしれない言う私は学校のために、「電話帳」を確認する必要があります。プログラムは完全ではありませんが、移動する前に修正する必要があるものがいくつかあります。 TelephoneNumbers配列は、ファイルからの数値を正しく格納していないか、またはそれらを表示していません。 SeaerchRecords関数の場合、ファイル内の最初の数字が正しく表示され、2番目の数字は "2147483647"と表示され、残りの数字は "0"と表示されます。また、modify関数は数値を変更しません。関数の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

がまだ任意の答えはないと私は私はあなたのコードで問題を見つけることができますどのようにいくつかの提案を提供します。この時点で、問題を正確に表示されませんので。 バグを見つけることができないプログラミングの状況では、できるだけ正確に見つけて、すべての入力データと仮定をチェックすることです。通常、デバッガはそのような目的で使用されますが、プログラムの最終バージョンを作成する前にコンソールでテキストを出力するだけです。それがチェックされ、あなたは私たちがSeaerchRecords関数に移動することができ、あなたのデータに問題がないdefenitely確信している場合は、[OK]を

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(); 

:あなたはあなたが本当にあなたのファイルから名前と電話を受けたことを確認しなければならない、と起動するには 同じ手順を実行します。

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 int NUMBER_OF_RECORDS = 100;どこにでも '100'を置く)のような一般的なものにconst宣言を使用すると、作業量やバグの可能性が減ります。 2)sometingがデータに間違っている場合、プログラムで発生する可能性のあるすべての問題をチェックしてみてください。ファイルに100個未満のレコードがある場合はどうなりますか?さらに悪いことになっている不適切なデータを壊滅させる、または静かに読む。あなたがあなたのレコード数に達していることを現在のチェックと一緒に読んで、あなたが不適切なデータの場合には何かをすることで、読書の任意のステップでファイルの終わりに達していないことを確認してください。 3)無期限に動作させないように、サイクル中の問題の可能性のある問題を確認してください。今あなたの状態for(count=0;TelephoneNames[count]!=name;count++) は、そのような名前がない場合、またはカウント100以上でプログラムを壊すだけで実行されます。カウントがその値を超えていないことを確認する必要があります。がんばろう!

+0

ありがとうございました!私はプログラムの誤りを見つけました。 – Andi

関連する問題