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.

FoodItem.h

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

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

private:
string name;
char type;
double calories;


public:
fooditem();
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

};
#endif

FoodItem.cpp

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

fooditem::fooditem()
{
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)
{
name=j;
}
void fooditem::settype(char k)
{
type=k;
}
void fooditem::setcalories(double l)
{
calories=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;
}
*/
//***************************************

Main.cpp

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

for ( int i=0;i<7;i++)
{
jay>>name;
jay>>type;
jay>>calorie;
if(calorie<0)
throw fooditem::negative();
}
}
else
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;
//cout<<apple.compareto(food)<<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.

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.