0

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. 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." The modify function also doesn't change the number, and I confirmed this with the while in the function. 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;
}
RazaUsman_k
  • 612
  • 5
  • 18
Andi
  • 9

1 Answers1

0

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. 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. 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. 2) Try to check all possible problems that you program can encounter if someting is wrong with data. What will happen if you have less than 100 records in your files now? 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. 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. You should check that count doesn't exceed that value. Good luck!

Arsenii Fomin
  • 2,681
  • 1
  • 16
  • 30