问题出现在main函数的Insert();
insert(head, "Carrot", 100);
#include <iostream>
#include <iostream>
using namespace std;
struct node
{
string name;
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. Tilføj madvarer til listen\n";
cout << "2. Vis listen af madvarer\n";
cout << "3. Opdater vægt\n";
cout << "4. Indtast madvarer\n";
cout << "5. Indtast dagens motion\n";
cout << "6. Exit program \n";
cin >> choice;
return choice;
}
void insertAsFirstElement(node *&head, node *&last, string name, int eatCalories)
{
node *temp = new node;
temp->name = name;
temp->eatCalories = eatCalories;
temp->next = NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, string name, int eatCalories)
{
if(isEmpty(head))
insertAsFirstElement(head, last, name, eatCalories);
else
{
node *temp = new node;
temp->name = name;
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->name << " " << current->eatCalories << " calories" << endl;
current = current->next;
}
}
}
node* subtractCalories(node *head)
{
int choice;
showList(head);
cout << "Vælg nummer fra listen\n";
cin >> choice;
int number = 1;
node *current = head;
while (current != NULL && choice != number)
{
++number;
current = current->next;
}
cout << "Du har valgt: " << current->name << endl;
return current;
}
int main(int argc, const char * argv[]) {
insert(head, "Carrot", 100);
insert(head, "Banana", 50);
insert(head, "Apple", 20);
insert(head, "Pineapple", 30);
insert(head, "Biscuit", 40);
string motion;
double vaegt;
double kalorie;
int answer = 1;
float minutesExerciseInHour;
int quotes = 0;
float caloriesBurned;
cout << "Velkommen til kalorieberegneren!\n";
cout << "Indtast dit kalorieindtag for i dag, så vi kan komme igang med at regne på det\n";
cin >> kalorie;
cout << "Hvad er din nuvaerende veagt?\n";
cin >> vaegt;
node *head = NULL;
node *last = NULL;
node *current;
char choice;
int eatCalories;
string name;
do{
choice = menu();
switch(choice)
{
case '1':
cout << "Indtast navn:";
cin >> name;
cout << "Indtast nummer:";
cin >> eatCalories;
insert(head, last, name, eatCalories);
break;
case '2': showList(head);
break;
case '3': cout << "Hvad er din vægt?\n";
cin >> vaegt;
cout << "Din vægt er blevet opdateret til: \n";
cout << vaegt << endl;
break;
case '4':
cout << endl << "You need to consume " << kalorie << " calories today!" << endl << endl;
current = subtractCalories(head);
kalorie = kalorie-current->eatCalories;
cout << endl << "You need to eat " << kalorie<< " calories more today!" << endl;
break;
case '5':
do
{
cout << "Hvor mange minutter motion? (angiv i timer)\n";
cin >> minutesExerciseInHour;
cout << "Hvilken type motion?" <<endl;
cout << "1. Løb" <<endl;
cout << "2. Svømning" <<endl;
cout << "3. Cykling" <<endl;
cin >> quotes;
switch(quotes)
{
case 1:
caloriesBurned = vaegt*7.5*minutesExerciseInHour;
cout << "Du har forbrændt: " << caloriesBurned << "kalorier\n";
kalorie = kalorie + caloriesBurned;
cout << "Du har: " << kalorie << " tilbage for i dag\n";
break;
case 2:
caloriesBurned = vaegt*7*minutesExerciseInHour;
cout << "Du har forbrændt: " << caloriesBurned << "kalorier\n";
kalorie = kalorie + caloriesBurned;
cout << "Du har: " << kalorie << " tilbage for i dag\n";
break;
case 3:
caloriesBurned = vaegt*6*minutesExerciseInHour;
cout << "Du har forbrændt: " << caloriesBurned << "kalorier\n";
kalorie = kalorie + caloriesBurned;
cout << "Du har: " << kalorie << " tilbage for i dag\n";
break;
default: cout << "What is that?";
}
}
while (answer != 1);
default: cout << "System exit\n";
}
}while(choice != '5');
return 0;
}
答案 0 :(得分:1)
您正在尝试在声明
之前使用标识符head
int main(int argc, const char * argv[]) {
insert(head, "Carrot", 100);
insert(head, "Banana", 50);
insert(head, "Apple", 20);
insert(head, "Pineapple", 30);
insert(head, "Biscuit", 40);
//...
node *head = NULL;
node *last = NULL;
node *current;
char choice;
int eatCalories;
string name;
因此,当在main的最开始调用函数insert
时,编译器还不知道head
是什么。
还要考虑用四个参数声明的函数
void insert(node *&head, node *&last, string name, int eatCalories);
但你在主要用三个参数调用它
insert(head, "Carrot", 100);
insert(head, "Banana", 50);
insert(head, "Apple", 20);
insert(head, "Pineapple", 30);
insert(head, "Biscuit", 40);
答案 1 :(得分:0)
感谢大家的帮助。解决方案由πάνταῥεῖ和Vlad来自莫斯科。我只给了三个参数,它需要四个。