calorie counter help please

Pages: 12
Keep getting these errors:
calories1.cpp: In function ‘int main()’:
calories1.cpp:72: error: invalid lvalue in assignment
calories1.cpp:76: error: expected primary-expression before ‘else’
calories1.cpp:76: error: expected `;' before ‘else’
calories1.cpp:80: error: invalid lvalue in assignment
calories1.cpp:84: error: expected primary-expression before ‘else’
calories1.cpp:84: error: expected `;' before ‘else’
calories1.cpp:87: error: expected `}' at end of input
calories1.cpp:87: error: expected `}' at end of input
calories1.cpp:87: error: expected `}' at end of input
calories1.cpp:87: error: expected `}' at end of input

whats wrong with my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
 #include <iostream>



using namespace std;



void male_function ();

void female_function ();

void level1_function ();

void level2_function ();

int main ()

{



	int gender;



    cout << "Welcome to Calorie Calculator!!\n\n";

    cout << "This program will calculate your caloric Intake!" << endl;

    cout << "\n\n" << "What is your gender? (1 for female, and 2 for male): ";

    cin >> gender;

    

    while (gender == 1 || gender == 2)

          { 

                  cout << "\n\n" << "I'm sorry, you entered something other than 1 or 2." << endl;

                  cout << "\n\n" << "Please indicate if you are male or female." << endl;

                  cout << "by entering 1 for female or 2 for male: ";

                  cin >> gender;

                  

                  }

                  

	if (gender == 1)

{

           female_function ();

}

	else

{

           male_function ();

}



	//get weight

	cout << "how much do you weigh in pounds? ";

	int weight;

	cin >> weight;



	while (weight < 50 || weight > 500)

{

	cout << "\n\n";

	cout << "Please enter a weight more than 50lbs and below 500lbs" << endl;

	cin >> weight;

}

	//get activity level

	int Alevel;

	cin>> Alevel;



	while (Alevel == 1 || Alevel == 2)

{

	cout << "What is your activity level?\n\n";

	cout << "Select 1 for Low activity, Select 2 for Moderate Activity.";

	cin>> Alevel;



	if (Alevel == 1)

{

           level1_function ();

}

	else

{

           level2_function ();

}

	//get caloric total

{

	int calories;

	cin>> calories;

	if (gender == 1 || Alevel = 1) 

	calories = (10 * weight);

	{

	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;

	else (Alevel = 2) {

	calories = (12 * weight);

	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;

	}

	if (gender == 2 || Alevel = 1)

	calories = (13 * weight);

	{

	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;

	} else (Alevel = 2) {

	calories = (15 * weight);

	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;

}

"else" should not have parameters - else (Alevel = 2)

You want to be using else if - else if (Alevel = 2)

This part -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 while (gender == 1 || gender == 2)

          { 

                  cout << "\n\n" << "I'm sorry, you entered something other than 1 or 2." << endl;

                  cout << "\n\n" << "Please indicate if you are male or female." << endl;

                  cout << "by entering 1 for female or 2 for male: ";

                  cin >> gender;

                  

                  }


Makes no sense to me. What this code does is. If the user enters the number 1 or 2, then it will tell the user he did not enter 1 or 2?

What about here -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 	int Alevel;

	cin>> Alevel;



	while (Alevel == 1 || Alevel == 2)

{

	cout << "What is your activity level?\n\n";

	cout << "Select 1 for Low activity, Select 2 for Moderate Activity.";

	cin>> Alevel;


Wouldnt you want to ask them the question before they answer the question?
Last edited on
yeah i looked at those parts, deleted and edited some heres what i have and the errors i get:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>

using namespace std;

void male_function ();
void female_function ();
void level1_function ();
void level2_function ();
int main ()
{

	int gender;

    cout << "Welcome to Calorie Calculator!!\n\n";
    cout << "This program will calculate your caloric Intake!" << endl;
    cout << "\n\n" << "What is your gender? (1 for female, and 2 for male): ";
    cin >> gender;
    
    while (gender == 1 || gender == 2)
          { 
                  cin >> gender;
                  
                  }
                  
	if (gender == 1)
{
           female_function ();
}
	else
{
           male_function ();
}

	//get weight
	cout << "how much do you weigh in pounds? ";
	int weight;
	cin >> weight;

	while (weight < 50 || weight > 500)
{
	cout << "\n\n";
	cout << "Please enter a weight more than 50lbs and below 500lbs" << endl;
	cin >> weight;
}
	//get activity level
	int Alevel;
	cin>> Alevel;

	cout << "What is your activity level?\n\n";
	cout << "Select 1 for Low activity, Select 2 for Moderate Activity.";

	while (Alevel == 1 || Alevel == 2)
{
	cin>> Alevel;

	if (Alevel == 1)
{
           level1_function ();
}
	else
{
           level2_function ();
}
	//get caloric total
{
	int calories;
	cin>> calories;
	if (gender == 1 && Alevel = 1) 
	calories = (10 * weight);
	{
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	else if (Alevel = 2) {
	calories = (12 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	if (gender == 2 && Alevel = 1)
	calories = (13 * weight);
	{
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	} else if (Alevel = 2) {
	calories = (15 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}


	return 0;
}


Errors:
1
2
3
4
5
6
7
8
9
10
calories1.cpp: In function ‘int main()’:
calories1.cpp:70: error: invalid lvalue in assignment
calories1.cpp:74: error: expected primary-expression before ‘else’
calories1.cpp:74: error: expected `;' before ‘else’
calories1.cpp:78: error: invalid lvalue in assignment
calories1.cpp:82: error: expected primary-expression before ‘else’
calories1.cpp:82: error: expected `;' before ‘else’
calories1.cpp:89: error: expected `}' at end of input
calories1.cpp:89: error: expected `}' at end of input
calories1.cpp:89: error: expected `}' at end of input 
Last edited on
1
2
3
4
if (gender == 1 && Alevel = 1) 
	calories = (10 * weight);
	{
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;


The calories = (10 * weight); part should be inside the curly brackets. they are currently outside.

You get error at line 89, but you didint post your code at line 89, cant help you with that unless you do.
89 is actually just 87, i only copied and pasted what was needed to show you guys the rest is notes up top.

just minues two to any error line (ie: 70 is actually 68)
Last edited on
1
2
3
if (gender == 1 && Alevel = 1) 
	calories = (10 * weight);
	{


At line - 68, you are missing a closing bracket. Also like I said earlier, the calories = (10 * weight); Should be put inside the curly brackets just above the COUT statement.

should be
1
2
3
4
5
6
7
8
9
 
if (gender == 1 && Alevel = 1) 
	
{ 
calories = (10 * weight);
cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
}

Last edited on
fixed everything this is what im getting:

calories1.cpp:70: error: invalid lvalue in assignment
calories1.cpp:79: error: invalid lvalue in assignment
calories1.cpp:90: error: expected `}' at end of input
calories1.cpp:90: error: expected `}' at end of input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
//
#include <iostream>

using namespace std;

void male_function ();
void female_function ();
void level1_function ();
void level2_function ();
int main ()
{

	int gender;

    cout << "Welcome to Calorie Calculator!!\n\n";
    cout << "This program will calculate your caloric Intake!" << endl;
    cout << "\n\n" << "What is your gender? (1 for female, and 2 for male): ";
    cin >> gender;
    
    while (gender == 1 || gender == 2)
          { 
                  cin >> gender;
                  
                  }
                  
	if (gender == 1)
{
           female_function ();
}
	else
{
           male_function ();
}

	//get weight
	cout << "how much do you weigh in pounds? ";
	int weight;
	cin >> weight;

	while (weight < 50 || weight > 500)
{
	cout << "\n\n";
	cout << "Please enter a weight more than 50lbs and below 500lbs" << endl;
	cin >> weight;
}
	//get activity level
	int Alevel;
	cin>> Alevel;

	cout << "What is your activity level?\n\n";
	cout << "Select 1 for Low activity, Select 2 for Moderate Activity.";

	while (Alevel == 1 || Alevel == 2)
{
	cin>> Alevel;

	if (Alevel == 1)
{
           level1_function ();
}
	else
{
           level2_function ();
}
	//get caloric total
{
	int calories;
	cin>> calories;
	if (gender == 1 && Alevel = 1)
	{
	calories = (10 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel = 2) {
  	calories = (12 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	if (gender == 2 && Alevel = 1) {
	calories = (13 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel = 2) {
	calories = (15 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}


	return 0;
}

1
2
3
4
if (gender == 2 && Alevel = 1) {
	calories = (13 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}



You should change this to else if. No need to switch back to "if".

Also the bracket at the end of this code should face the other way. -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 while (Alevel == 1 || Alevel == 2)
{
	cin>> Alevel;

	if (Alevel == 1)
{
           level1_function ();
}
	else
{
           level2_function ();
}
	//get caloric total
{


You have to close the while loop.

Also, huge problem here -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (gender == 1 && Alevel = 1)
	{
	calories = (10 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel = 2) {
  	calories = (12 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	if (gender == 2 && Alevel = 1) {
	calories = (13 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel = 2) {
	calories = (15 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}


It should never be Alevel = 1 or Alevel = 2 .

It has to be double =. So like you do with gender == 2 It has to be the same case everywhere. Full code -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (gender == 1 && Alevel == 1)
	{
	calories = (10 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel == 2) {
  	calories = (12 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	if (gender == 2 && Alevel == 1) {
	calories = (13 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel == 2) {
	calories = (15 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}

Last edited on
errors:
1
2
3
ries1.cpp: In function ‘int main()’:
calories1.cpp:83: error: expected primary-expression before ‘else’
calories1.cpp:83: error: expected `;' before ‘else’ 





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// 
#include <iostream>

using namespace std;

void male_function ();
void female_function ();
void level1_function ();
void level2_function ();
int main ()
{

	int gender;

    cout << "Welcome to Calorie Calculator!!\n\n";
    cout << "This program will calculate your caloric Intake!" << endl;
    cout << "\n\n" << "What is your gender? (1 for female, and 2 for male): ";
    cin >> gender;
    
    while (gender == 1 || gender == 2)
          { 
                  cin >> gender;
                  
                  }
                  
	if (gender == 1)
{
           female_function ();
}
	else
{
           male_function ();
}

	//get weight
	cout << "how much do you weigh in pounds? ";
	int weight;
	cin >> weight;

	while (weight < 50 || weight > 500)
{
	cout << "\n\n";
	cout << "Please enter a weight more than 50lbs and below 500lbs" << endl;
	cin >> weight;
}
	//get activity level
	int Alevel;
	cin>> Alevel;

	cout << "What is your activity level?\n\n";
	cout << "Select 1 for Low activity, Select 2 for Moderate Activity.";

	while (Alevel == 1 || Alevel == 2)
{
	cin>> Alevel;

	if (Alevel == 1)
{
           level1_function ();
}
	else
{
           level2_function ();
}
	//get caloric total
}
	int calories;
	cin>> calories;
	if (gender == 1 && Alevel == 1)
	{
	calories = (10 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel = 2) {
  	calories = (12 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else (gender == 2 && Alevel == 1); {
	calories = (13 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel = 2) {
	calories = (15 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (gender == 1 && Alevel == 1)
	{
	calories = (10 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel = 2) {
  	calories = (12 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else (gender == 2 && Alevel == 1); {
	calories = (13 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel = 2) {
	calories = (15 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}


else (gender == 2 && Alevel == 1);

That should be an if else statement. not just else. And there is not suppose to be a semicolon at the. Like this - else if (gender == 2 && Alevel == 1))

And like I said earlier, its suppose to be double assignment operators, not just 1.

else if (Alevel = 2)

Should be - else if (Alevel == 2)

Also, you have 2 if statements saying else if (Alevel = 2). Cant do that.
alright fixed both of those, same error.
Could you post your entire current code please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// 
#include <iostream>

using namespace std;

void male_function ();
void female_function ();
void level1_function ();
void level2_function ();
int main ()
{

	int gender;

    cout << "Welcome to Calorie Calculator!!\n\n";
    cout << "This program will calculate your caloric Intake!" << endl;
    cout << "\n\n" << "What is your gender? (1 for female, and 2 for male): ";
    cin >> gender;
    
    while (gender == 1 || gender == 2)
          { 
                  cin >> gender;
                  
                  }
                  
	if (gender == 1)
{
           female_function ();
}
	else
{
           male_function ();
}

	//get weight
	cout << "how much do you weigh in pounds? ";
	int weight;
	cin >> weight;

	while (weight < 50 || weight > 500)
{
	cout << "\n\n";
	cout << "Please enter a weight more than 50lbs and below 500lbs" << endl;
	cin >> weight;
}
	//get activity level
	int Alevel;
	cin>> Alevel;

	cout << "What is your activity level?\n\n";
	cout << "Select 1 for Low activity, Select 2 for Moderate Activity.";

	while (Alevel == 1 || Alevel == 2)
{
	cin>> Alevel;

	if (Alevel == 1)
{
           level1_function ();
}
	else
{
           level2_function ();
}
	//get caloric total
}
	int calories;
	cin>> calories;
	if (gender == 1 && Alevel == 1)
	{
	calories = (10 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel == 2) {
  	calories = (12 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (gender == 2 && Alevel == 1); {
	calories = (13 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	else if (Alevel == 2) {
	calories = (15 * weight);
	cout<<"Your caloric intake is.. " <<calories<< "." <<endl;
	}
	return 0;
}
else if (gender == 2 && Alevel == 1); Just like every other else if statement, there is not supposed to be a semicollon after the parameters.

Remove it so it looks like this - else if (gender == 2 && Alevel == 1)

Your while loop in the beginning should look like this -

1
2
3
4
5
6
while (gender != 1 && gender != 2)
	{
		cout << "Please enter 1 or 2";
		cin >> gender;

	}


"Run this loop while gender is NOT equal to 1 and not equal to 2"

same with the other loop, should look like this -

1
2
3
4
5
while (Alevel != 1 && Alevel != 2)
	{
		cout << "Not valid, select 1 or 2";
		cin >> Alevel;
	}
Last edited on
alright didnt catch that before, thanks. for some reason its now showing a completely different error:
1
2
3
4
5
6
/tmp/ccwb1Vka.o: In function `main':
/home/centos/calories1.cpp:29: undefined reference to `female_function()'
/home/centos/calories1.cpp:33: undefined reference to `male_function()'
/home/centos/calories1.cpp:60: undefined reference to `level1_function()'
/home/centos/calories1.cpp:64: undefined reference to `level2_function()'
collect2: ld returned 1 exit status 
Could you please post the female, male, level1 and level2 functions?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   while (gender != 1 && gender != 2)
          { 
		  cout << "Please enter 1 or 2";
                  cin >> gender;
                  
                  }
                  
	if (gender == 1)
{
           female_function ();
}
	else
{
           male_function ();
}


1
2
3
4
5
6
7
8
9
10
11
12
13
	while (Alevel != 1 && Alevel != 2)
{
	cout <<"Not valid, select 1 or 2";
	cin>> Alevel;

	if (Alevel == 1)
{
           level1_function ();
}
	else
{
           level2_function ();
}
No I mean. What does these functions do? Where are they? Whats their job?
Are you using classes? or just functions in other files? When you say

1
2
3
4
	if (Alevel == 1)
{
           level1_function ();
}


if Alevel is == 1, what do you tell level1_function() to do, whats its job when Alevel is equal to 1?
if a level is 1 then it is low activity, but if its 2 then its moderate activity.

edit: nevermind got everything to work perfectly.. removed the functions and finished a few bits of the other code.. working perfectly just cleaning up some of the code. Thanks a lot!
Last edited on
Have you written the code for those functions anywhere?
Pages: 12