简体   繁体   English

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

[英]C++ Array not taking correct input from file

Disclaimer: I am a beginner to programming, so what I say might sound really stupid 免责声明:我是编程的初学者,所以我说的听起来很愚蠢

I have to make a "Telephone Directory" for school. 我必须为学校制作一个“电话簿”。 The program isn't complete, but there are some things that I need to fix before moving on. 该程序尚未完成,但是在继续之前,我需要修复一些问题。 The array TelephoneNumbers either isn't storing the numbers from the file correctly, or isn't displaying them. TelephoneNumbers数组未正确存储文件中的数字,或未显示它们。 For the SeaerchRecords function, the first number in the file is displayed correctly, the second is displayed as "2147483647," and the rest of the numbers display as "0." 对于SeaerchRecords函数,正确显示文件中的第一个数字,第二个显示为“ 2147483647”,其余数字显示为“ 0”。 The modify function also doesn't change the number, and I confirmed this with the while in the function. Modify函数也不会更改数字,我在函数中使用while确认了这一点。 The string array works perfectly fine, however. 字符串数组工作得很好,但是。 May someone explain what I'm doing incorrectly? 有人可以解释我在做什么吗?

#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;
}

Since there is no any answer still and I don't see exactly the problem at this point I'll provide some suggestions how you can find a problem in your code. 由于仍然没有任何答案,并且我目前还无法完全看到问题,因此我将提供一些建议,帮助您在代码中找到问题。 In any programming situation when you can't find a bug, first task is to locate it as much precisely as you can and check all input data and assumptions. 在任何找不到bug的编程情况下,首要任务是尽可能精确地定位它并检查所有输入数据和假设。 Usually, debugger is used for such purposes, but you can just output text in console before creating final version of your program. 通常,调试器用于此类目的,但是您可以在创建程序的最终版本之前仅在控制台中输出文本。 To start with, you must check that you really received names and telephones from your file: 首先,您必须检查您是否确实收到文件中的姓名和电话:

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

Ok, when it is checked and you are defenitely sure there is no problem in your data we can move to SeaerchRecords function doing the same procedure. 好的,当选中它并且您确信自己的数据没有问题时,我们可以使用相同的步骤移至SeaerchRecords函数。 We must check what is happening while you are searching: 在搜索时,我们必须检查正在发生的事情:

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;
}

Doing so you will locate your bug rather quickly. 这样,您将可以很快找到错误。 The problem can be in input files format, in difference of "name" and stored names format etc. 问题可能在于输入文件格式,“名称”和存储名称格式等的差异。

I'll provide several additional suggestion how you can improve your code. 我将提供一些其他建议,帮助您改进代码。 1) Try to use const declarations for such commonly used things as number of records (const int NUMBER_OF_RECORDS = 100; insted of just putting '100' everywhere), it will reduce the amout of work and possible bugs. 1)尝试将const声明用于记录数(const int NUMBER_OF_RECORDS = 100;仅将'100'放在各处)这样的常用内容,这将减少工作量和可能的错误。 2) Try to check all possible problems that you program can encounter if someting is wrong with data. 2)尝试检查如果某些数据错误,您的程序可能会遇到的所有问题。 What will happen if you have less than 100 records in your files now? 如果现在文件中的记录少于100条,将会怎样? Program crush or silent reading of unappropriate data which is even worse. 程序崩溃或无声读取不适当的数据,甚至更糟。 Check that you haven't reach file end on any step of reading along with current check that you've reached you number of records and do something in case of unappropriate data. 在读取的任何步骤中,请检查是否未到达文件末尾;在当前检查中,请检查是否已达到记录数量;如果数据不正确,请执行某些操作。 3) Check the possible problems with conditions in your cycles not to run them infinite number of times. 3)检查循环条件中可能存在的问题,不要无限次运行它们。 Now your condition for(count=0;TelephoneNames[count]!=name;count++) will execute forever if there is no such name or just crush the program on count 100 or more. 现在,如果没有这样的名称,则您for(count=0;TelephoneNames[count]!=name;count++)将永远执行,或者仅在计数100或更多时粉碎程序。 You should check that count doesn't exceed that value. 您应该检查计数不超过该值。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM