C 数组未从文件中获取正确的输入

C++ Array not taking correct input from file

本文关键字:输入 获取 数组 文件      更新时间:2023-10-16

免责声明:我是编程的初学者,所以我说的话听起来真的很愚蠢

我必须为学校做一个"电话目录"。该程序还不完整,但是在继续之前,我需要修复一些事情。Array TelephonEnumbers要么无法正确存储文件中的数字,要么没有显示它们。对于SeaerChreCords功能,正确显示文件中的第一个数字,第二个数字显示为" 2147483647",其余数字显示为" 0"。修改函数也不会更改数字,我在函数中使用WILE确认了这一点。但是,字符串阵列的工作原理很好。有人可以解释我在做什么吗?

#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 Mn";
    cout << "-Search Records|Enter Sn";
    cout << "-Delete Records|Enter Dn";
    //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;
}

由于没有任何答案,而且我目前还没有确切地看到问题,我将提供一些建议,如何在代码中找到问题。在任何编程情况下,当您找不到错误时,第一个任务是尽可能精确地找到它,并检查所有输入数据和假设。通常,调试器用于此类目的,但是您可以在创建程序的最终版本之前将文本输出。首先,您必须检查您真的从文件中收到了名称和电话:

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

好的,当您检查它时,您可以明确确定数据中没有问题,我们可以移至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 int number_of_records = 100;仅在任何地方放置100'(使用常见的事物,它将减少工作和可能的错误。2(尝试检查您编程的所有可能的问题,如果某个数据错误的数据是错误的。如果您现在的文件中的记录少于100个记录将会发生什么?程序会暗中或无声阅读不适当的数据,这甚至更糟。检查您是否在阅读的任何步骤中都没有到达文件结束,以及当前的检查您已访问了您的记录数量并在不适当的数据时做某事。3(检查周期中可能的条件可能的问题,以免将它们运行无限的次数。现在您的状况for(count=0;TelephoneNames[count]!=name;count++)如果没有这样的名称或仅在100或更多的情况下粉碎程序,将永远执行。您应该检查计数不会超过该值。祝你好运!