1 Replies - 712 Views - Last Post: 15 January 2016 - 01:07 AM Rate Topic: -----

#1 nvc5221   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-December 15

Pointer to Structure Array Help

Posted 14 January 2016 - 08:11 PM

Hello, I'm new to new to the use of pointers, and having trouble understanding how to implement pointers to a structure array. Here's the question:

In a linked list of integers using pointers to structures instead of array indices. The integers must be read from an input file determined by prompting the user. Allocate an array of 50 C++ structures defined as follows:
struct entry {
int value; // the integer read in
struct entry *nextPtr; // pointer to the next structure
};

You must also declare a struct entry *firstPtr which points to the first entry in the linked list. You must declare a struct *lastPtr which points to the last entry in the linked list. You must NOT allow the lastPtr to point beyond the end of the array.

After reading in the integers in the file print out (using pointers, not array indices) the integers read in the order they were read with no more than 10 entries per line.


This is the code i've came up with so far:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

struct entry *getEntry() {
	static int nextEntry = 0; static struct entry array[50];
	if (nextEntry >= 50) return NULL; 
	array[nextEntry].value = 0;
	nextEntry++; return &array[nextEntry - 1];
}

int main()
{
	int value;
	ifstream inFile;
	string input; // holds string for filename

	cout << "Enter file name: "; //opens the file user enters
	cin >> input;
	inFile.open(input);

	while (inFile.good())
	{
		inFile >> value[array];
	}
	
	inFile.close();
	ofstream outFile("outFile.txt");//opens output txt

}


If someone could help explain an easier way or fix the issues with my cod

Is This A Good Question/Topic? 0
  • +

Replies To: Pointer to Structure Array Help

#2 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Pointer to Structure Array Help

Posted 15 January 2016 - 01:07 AM

Hi, you haven't declared the structure entry.

Each element of the array is a struct, so each pointer just points to a struct.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1