My assignment asks me to Implement a Linked List class with FoodItem as the objects to be inserted in the class, and implement a PrintList function member that prints the name of each food item in the list. Implement a SelectPrint function member that only prints the food item names with a calorie count less than 300.

I'm stuck at the Linked List class, my professor wasn't very clear on how to do it, and I'm so confused. Here is my FoodItem code thus far, it's my Fooditem.h, fooditem.cpp, and main file.


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

enum relationtype{LESS,EQUAL,GREATER};
class fooditem {

string name;
char type;
double calories;

fooditem(string,char, double);
void setname(string);
void settype(char);
void setcalories(double);
string getname();
char gettype();
double getcalories();
relationtype compareto(fooditem) const;
class notfound
{};// class not found

class negative
{}; // negative number



#include <iostream>
#include <string>
using namespace std;
#include "fooditem.h"

name = "water ";
type = 'v';
calories = 0;

fooditem::fooditem(string j, char k, double l)
name = j;
type = k;
calories = l;

string fooditem::getname()
return name;

char fooditem::gettype()
return type;

double fooditem::getcalories()
return calories;

void fooditem::setname(string j)
void fooditem::settype(char k)
void fooditem::setcalories(double l)
/*relationtype fooditem::compareto(fooditem w) const
if (calories==w.calories) return EQUAL;
if (calories < w.calories)return LESS;
if (calories > w.calories) return GREATER;


#include <iostream>
#include <fstream>
#include "fooditem.h"
using namespace std;
int main(void)
fstream jay;
string name="";
char type=' ';
double calorie=0;

for ( int i=0;i<7;i++)
throw fooditem::negative();
throw fooditem::notfound();

catch (fooditem::notfound)

cout << "Error: A data file is not file.\n";
catch (fooditem::negative)
cout << "Error: A negative value was entered.\n";
cout << "End of the program.\n";

fooditem apple(name,type,calorie );
cout<<"name of food "<<apple.getname()<<endl;
cout<< "food type "<<apple.gettype()<<endl;
cout<<"food calories "<<apple.getcalories()<<endl;

fooditem food;
cout<<"name of food "<<food.getname()<<endl;
cout<< "food type "<<food.gettype()<<endl;
cout<<"food calories "<<food.getcalories()<<endl;

return 0;

I suggest you first write a linked list for ints. This will condense your code into at least a quarter of its current size. Also, remove the file input stuff and hard code some values. Once you have this working, you can "upgrade" your linked list class to support this other data structure.

Also, thanks for trying to use code tags, but you have to enclose the code in them, not just the filename.

This article has been dead for over six months. Start a new discussion instead.