File Input into Heap using inFile

  c++, file-io, heap, input, structure

Hello I am currently having an issue with no print out where my loop stalls or is not functioning properly. I’m working with 3 files. Main.cpp, heap.h, and heapdata.txt.

This is all I have in main currently:

#include <iostream>
#include <cctype> // for tolower
#include <fstream>
#include <string>
#include "heap.h"
using namespace std;

int main () {

fileGet(0);

}

This is all I have in heap.h currently:

#ifndef HEAP_H
#define HEAP_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct member {
    string name;
    int id;
};

void fileGet(member heap[])
{
    char YN;
    string firstName;
    string lastName;
    ifstream inFile;

    inFile.open("heapdata.txt",ios::in);

    // While loop to read the file
    while (inFile >> heap->id >> firstName >> lastName >> YN) {
        heap->name = firstName + " " + lastName;

    }
    cout << "test";

  inFile.close();
}

void reheapUp(member heap[], int size)
{
    int i = size-1; // current index
    int parent = (i-1)/2;
    while (i != 0 && heap[i].id > heap[parent].id)
    {
        int temp = heap[i].id;
        heap[i].id = heap[parent].id;
        heap[parent].id = temp;
        
        i = parent;
        parent = (i-1)/2;

    }
}

void reheapDown(int heap[], int size)
{
    int i = 0;
    int lc, rc, bigChild;
    lc = 2*i + 1;
    rc = 2*i + 1;
    while(lc < size) {
        if (rc < size) {
            if(heap[rc] > heap[lc])
                bigChild =rc;
            else
                bigChild = lc;
        }
        else
            bigChild = lc;

        if(heap[i] < heap[bigChild]) {
            int temp = heap[i];
            heap[i] = heap[bigChild];
            heap[bigChild] =temp;
        }
        else
            return;

        i = bigChild;
        lc = 2*i + 1;
        rc = 2*i + 2;      
    }
}

/*
void insertNode(int heap[], int& size, int key)
{
    heap[size] =key;
    size++;
    reheapUp(heap.id,size);
}
*/

int deleteNode(int heap[], int& size)
{
    int output = heap[0];
    heap[0] = heap[size-1];
    size--;
    reheapDown(heap, size);
    return output;
}

void printHeap(int heap[], int size)
{
    for(int i = 0; i < size; i++) {
        cout << heap[i] << " ";
    }
    cout << endl;
}

#endif

This is the text inside of heapdata.txt:

41 Jona N
334 Vizility Y
169 Leaf Y
478 SoOn Y
962 Hqrdest Y
705 Sinatraa N
281 Corey N
961 Yawar N
995 Quinn Y
827 MSS Y
391 SVG Y
902 Saberlight N
292 CemaTheSlayer Y
421 Brokenblade Y
718 Spica N
447 Bjergsen Y
771 Zven Y
869 Smoothie Y
667 Wealthyman N
35 Snutz Y

This is the area of code I’m currently having issue (heap.h):

#define HEAP_H
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct member {
    string name;
    int id;
};

void fileGet(member heap[])
{
    char YN;
    string firstName;
    string lastName;
    ifstream inFile;

    inFile.open("heapdata.txt",ios::in);

    // While loop to read the file
    while (inFile >> heap->id >> firstName >> lastName >> YN) {
        heap->name = firstName + " " + lastName;

    }
    cout << "test";

  inFile.close();
}

Code compiles but does not run test cout. Trying to put the first data entry into heap id, the second into firstName, the third into lastName, and the fourth into YN.

Source: Windows Questions C++

LEAVE A COMMENT