Emil Skovgaard
Emil Skovgaard

Reputation: 31

Discarding checking if list is empty

As you managed to fix my code last time, i wanted to ask for your help again.

As i already have a predefined list of five elements, this code seems rather nonsens, as there is no purpose of checking if the list is empty. I can't seem to figure out who to bypass the if-else, and just keep the "insert" function instead of also checking if the list is empty...

#include <iostream>
#include <cmath>

using namespace std;

struct node
{
    string nameOfFood;
    int eatCalories;
    int number;
    node *next;

};

bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories);
void insert(node *&head, node *&last, string name, int eatCalories);
void showList(node *current);


bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}

char menu()
{
    char choice;

    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;
    }

}

Let me know if you need more of the code?

Hoping for your help!

Upvotes: 1

Views: 552

Answers (3)

Emil Skovgaard
Emil Skovgaard

Reputation: 31

I think i understand with the check for empty list - therefore i will take my code to another question.

I got the InsertAsFirstElement function, but at the moment, i think i forgot what the function does, because nowhere in the code, there will be "insert by first element", so can someone tell what that function does?

Sorry for the inconvenience!

Upvotes: 0

Emil Skovgaard
Emil Skovgaard

Reputation: 31

But my list will never be empty, as i have five predefined elements. I will post the rest of the code, and maybe you will understand what i mean? Or else, it's me who got it wrong.

#include <iostream>
#include <cmath>

using namespace std;

struct node
{
    string nameOfFood;
    int eatCalories;
    int number;
    node *next;

};

bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories);
void insert(node *&head, node *&last, string name, int eatCalories);
void showList(node *current);


bool isEmpty(node *head)
{
    if(head == NULL)
        return true;
    else
        return false;
}

char menu()
{
    char choice;
    
    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;
        
        while(current != NULL)
        {
            ++number;
            cout << number << " " << current->nameOfFood << " " << current->eatCalories << " calories" << endl;
            
            current = current->next;
            
        }
    }
}
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;
}


int main(int argc, const char * argv[]) {
    
    double caloriesToBurn;
    double weight;
    double height;
    double BMI;
    
    int answerWhile = 1;
    int answerToExercise = 0;
    float minutesExerciseInHour;
    float caloriesBurned;
    
    node *head = NULL;
    node *last = NULL;
    node *current;
    char choice;
    int eatCalories;
    string nameOfFood;
    
    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;
                    
                    switch(answerToExercise)
                    {
                        case 1:
                            caloriesBurned = weight*7.5*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "calories\n";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left today\n";
                            break;
                            
                        case 2:
                            caloriesBurned = weight*7*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "calories\n";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left today\n";
                            break;
                            
                        case 3:
                            caloriesBurned = weight*6*minutesExerciseInHour;
                            cout << "You have burned: " << caloriesBurned << "calories\n";
                            caloriesToBurn = caloriesToBurn + caloriesBurned;
                            cout << "You have: " << caloriesToBurn << " left today\n";
                            break;
                            
                        default: cout << "I think something went wrong? Try again..";
                    }
                }
                
                while (answerWhile != 1);
                
            default: cout << "System exit\n";
        }
    }while(choice != '6');
    
    
    return 0;
}

Upvotes: 0

Shoe
Shoe

Reputation: 76240

That check is necessary, because if your list is empty then you have to perform a certain number of operations that are only performed in that case.

There's really no purpose in implementing your own linked list. Classes that are way more flexible than yours have already been defined by the standard, see std::forward_list (singly linked list) and std::list (doubly linked list).

It is recommended that you default to std::vector or std::array when choosing a container. In this case, if you simply have a list of 5 elements, just use std::array and a custom type:

struct food
{
    string nameOfFood;
    int eatCalories;
    int number;
};

and then:

std::array<food, 5> food_list { ... };

Upvotes: 2

Related Questions