Why is my program failing to close? These names and phone numbers are what I am trying to load into the array and display.

Yaztremski 592-48-1003
Tilden 355-71-8210
Charles 290-65-8817
Balboa 458-12-9834
Watterson 234-98-0023
Jefferson 303-41-5692
Carmichael 392-60-0932
Martin 418-77-0974
Davila 339-89-4475
Jacobs 228-42-0547
Dobbs 219-52-4058
Nastromo 628-79-2516
Rivers 611-69-7045
Williams 498-12-3362
Georgopolis 204-65-8006
Espinosa 457-68-8103
Knight 566-10-3369
Morgan 285-69-2746
Lawrence 300-20-5638
Silverius 605-12-0882 



#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <conio.h>
using namespace std;

enum columns{Lname=0, SSN=1};

const int MAXELEMENTS = 20;

//Function prototypes
void Pause();
void Pause(char *,int);
int SearchSerial(string [][2],int,string, int);

void Sort(string [][2],int,char direction='A');
void Sort(string *[][2],int,char direction='A');

int main()
{
    //Declare an array of string VALUES
    string LnameSSN[MAXELEMENTS][2];
    //Declare an array of POINTERS to string values
    string * p2LnameSSN[MAXELEMENTS][2]= {NULL};

    int index;
    fstream dataIn;

    string filePath ="H:\\Lab2.csv";

    //Open the data file
    dataIn.open(filePath.c_str(),ios::in);
    if(dataIn.fail())
    {
         cout << "Unable to open the data file." << endl;
        ("pause");
         return 999;
    }

    index = 0;  //initialize the subscript var

    while(!dataIn.eof())
    {
        //Load the values array
        getline(dataIn,LnameSSN[index][Lname],',');
        getline(dataIn,LnameSSN[index][SSN],'\n');

        //Load the pointers array
        p2LnameSSN[index][Lname] = &LnameSSN[index][Lname];
        p2LnameSSN[index][SSN] = &LnameSSN[index][SSN];
        index++;
    }

    dataIn.close();
    if(dataIn.fail())
    {
        cout << "Failed to close the data file." << endl;
        ("pause");
        return 888;
    }
}

Recommended Answers

All 4 Replies

can you copy your code, paste it on here. then highlight it and click the (CODE) button??? that puts it in code like visual studios has it.

Looping while(!data.eof()) is incorrect. It causes one extra (unwanted) read, which in your case leads to a classic buffer overflow.

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <conio.h>
using namespace std;

enum columns{Lname=0, SSN=1};

const int MAXELEMENTS = 20;

//Function prototypes
void Pause();
void Pause(char *,int);
int SearchSerial(string [][2],int,string, int);

void Sort(string [][2],int,char direction='A');
void Sort(string *[][2],int,char direction='A');

int main()
{
//Declare an array of string VALUES
string LnameSSN[MAXELEMENTS][2];
//Declare an array of POINTERS to string values
string * p2LnameSSN[MAXELEMENTS][2]= {NULL};

int index;
fstream dataIn;

string filePath ="H:\\Lab2.csv";

//Open the data file
dataIn.open(filePath.c_str(),ios::in);
if(dataIn.fail())
{
cout << "Unable to open the data file." << endl;
("pause");
return 999;
}

index = 0; //initialize the subscript var

while(!dataIn.eof())
{
//Load the values array
getline(dataIn,LnameSSN[index][Lname],',');
getline(dataIn,LnameSSN[index][SSN],'\n');

//Load the pointers array
p2LnameSSN[index][Lname] = &LnameSSN[index][Lname];
p2LnameSSN[index][SSN] = &LnameSSN[index][SSN];
index++;
}

dataIn.close();
if(dataIn.fail())
{
cout << "Failed to close the data file." << endl;
("pause");
return 888;
}
}

Looping while(!data.eof()) is incorrect. It causes one extra (unwanted) read, which in your case leads to a classic buffer overflow.

So what needs to be used instead?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.