Have worked with this project sometime, and im kinda getting 'blind' on the code. As os know, the problem is, i can't remember what the purpose of the function InsertAsFirstElement is, because there is nowhere something is being inserted as first element?
Hoping someone can give me the answer!
#include <iostream>
#include <cmath>
using namespace std;
struct node
{
string nameOfFood;
int eatCalories;
int number;
node *next;
cout << "Menu\n";
cout << "1. Add food, beverage etc.\n";
cout << "2. Show the list of food(s), beverage(s) etc.\n";
cout << "3. Update your current weight\n";
cout << "4. What have you been eaten?\n";
cout << "5. What exercise have you done?\n";
cout << "6. Exit program \n";
cin >> choice;
return choice;
}
void insertAsFirstElement(node *&head, node *&last, string nameOfFood, int eatCalories)
{
node *temp = new node;
temp->nameOfFood = nameOfFood;
temp->eatCalories = eatCalories;
temp->next = NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, string nameOfFood, int eatCalories)
{
if(isEmpty(head))
insertAsFirstElement(head, last, nameOfFood, eatCalories);
else
{
node *temp = new node;
temp->nameOfFood = nameOfFood;
temp->eatCalories = eatCalories;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void showList(node *current)
{
if(isEmpty(current))
cout << "The list of foods is empty\n";
else
{
cout << "The list of foods contain: \n";
int number = 0;
}
}
}
node* subtractCalories(node *head)
{
int choice;
showList(head);
cout << "Pick the food, beverage etc. from the list by number\n";
cin >> choice;
int number = 1;
node *current = head;
while (current != NULL && choice != number)
{
++number;
current = current->next;
}
cout << "You have choosen: " << current->nameOfFood << endl;
return current;
}
insert(head, last, "Tuna (in oil)", 190);
insert(head, last, "Milkchocolate (Marabou)", 540);
insert(head, last, "Milk (skimmed)", 33 );
insert(head, last, "Ice Tea (white peach)", 1);
insert(head, last, "Peanuts (roasted and salted)", 624);
cout << "*** Welcome to the calorie calculator! ***\n";
cout << "To get started, i need some numbers from you\n";
cout << "Please enter the number of calories you need to burn today:\n";
cin >> caloriesToBurn;
cout << "What are your current weight? (please enter in kilograms)\n";
cin >> weight;
cout << "And lastly i need to know your height (please enter in centimeters)\n";
cin >> height;
BMI = (weight / pow(height/100,2));
cout << "Your BMI is: " << BMI << endl;
if (BMI <= 18.5)
cout << "You are in the range: under weight" << endl;
else if ((BMI > 18.5) && (BMI < 25))
cout << "You are in the range: normal weight" << endl;
else
cout << "You are in the range: over weight" << endl;
do{
choice = menu();
switch(choice)
{
case '1':
cout << "Enter the name of the food, beverage etc.:";
cin >> nameOfFood;
cout << "How many calories did it contain? (measured per 100 grams):";
cin >> eatCalories;
insert(head, last, nameOfFood, eatCalories);
break;
case '2': showList(head);
break;
case '3': cout << "What you wanna update your weight to?\n";
cin >> weight;
cout << "Your weight have been update to: \n";
cout << weight << endl;
if (BMI <= 18.5)
cout << "You are in the range: under weight" << endl;
else if ((BMI > 18.5) && (BMI < 25))
cout << "You are in the range: normal weight" << endl;
else
cout << "You are in the range: over weight" << endl;
break;
case '4':
cout << endl << "You need to consume " << caloriesToBurn << " calories today!" << endl << endl;
current = subtractCalories(head);
caloriesToBurn = caloriesToBurn-current->eatCalories;
cout << endl << "You need to eat " << caloriesToBurn << " calories more today!" << endl;
break;
case '5':
do
{
cout << "How many minuttes have you ben exercising (please enter in hours)\n";
cin >> minutesExerciseInHour;
cout << "What type of exercise have you been doing?" <<endl;
cout << "1. Running" <<endl;
cout << "2. Swimming" <<endl;
cout << "3. Cycling" <<endl;
cin >> answerToExercise;
I hope you guys can help me out a little more, try to make me understand the code to the fullest. As of now, the questions a still concerned the InserAsFirstElement and the insert function:
1: In the InsertAsFirstElement, why does both the head and last equals temp? They both can't be the 'new value' , then both head and last will be the same value, and that is not the case when running the program.
2: In the insert function, at the else-part, why is last = temp here? I get that the next element i NULL, because there is no more elements, and that the last value equals the 'new' temp, but why the last last = temp; part?