Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Making my first list

$
0
0
I am having some trouble learning the syntax for a basic list. This is what I have so far... my function return values may not be correct. They are simply place holders until I learn what I need exactly.

The challenge, cliff note version.

  • Read in data from a file
  • Send said data to a class
  • In said class store said data in a list
  • List should contain pointers to the objects within the class, not the objects themselves
  • Print the list, using an iterator.


main.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "Animal.h"
using namespace std;

void fileread();

int main()
{
	fileread();
    return 0;
}

void fileread()
{
	string type;
	int age, energy;
	float weight;
	ifstream data;

	data.open ("input.txt", ios::in);						//Opens input file, located in root

	if (data.is_open())
	{
		data >> type;										//Prime read
		while (!data.eof())
		{
			if (type == "Animal")
			{
				Animal Animal1;	//Setup as pointers? How to make new for every call (aka static Animal1 dec)?			
				data >> age;
				data >> energy;
				data >> weight;
				Animal1.makelist(age, energy, weight);
			}
			data >> type;									//Sub read
		}
	}
}//end fileread


Animal.cpp
#include "Animal.h"
#include <iostream>


Animal::Animal(void)
{
}


Animal::~Animal(void)
{
}

int Animal::makelist(int age, int energy, float weight)
{
	int newage;
	newage = age * 365;

	//list goes here

	return 1;
}

void Animal::print(void)
{
	//loop begin
		//cout << "I am a " << newage << " year old, " << weight << "kilogram Animal with " << energy << " calories.";
		//cout << endl;
	//loop end
}


Animal.h
class Animal
{
public:
	Animal(void);
	~Animal(void);
	int makelist(int, int, float);
	void print(void); // need to add parameters
};



Current Issue:

I am not sure, after sending the data to the "makelist" function, how to code the list. After this, I am not clear on how to setup an iterator in the "print" function. I think the best route would be to get it all setup with a list that stores the objects themselves... to learn the list syntax. After that graduate to making the list full of pointers pointing to the objects.

Any help would be greatly appreciated.

I forgot to mention, the STL is allowed for usage.

Would I build the list simply by adding the following to main.cpp?

#include <list>

list<Animal>



I doubt that is correct... but its what I am gathering from some quick searches. The tutorial here is helping, but is a bit hard to follow for some reason.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>