Sep 10, 2016 at 10:37pm UTC
Everything up to line 22 is working. After that I keep getting the error "warning: comparison with string literal results in unspecified behaviour" on linese 22, 26, and 30. Any help is appreciated.
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
#include <iostream>
using namespace std;
int main()
{
int running = 10;
int basketball = 8;
int sleep = 1;
int weight = 0;
char activity[10];
double caloriesPerMinute;
{
cout << "Enter weight here:" ;
cin >> weight;
cout << "You entered:" << " " << weight << endl;
cout << "Enter activity here:" ;
cin >> activity;
cout << "You entered:" << " " << activity << endl;
}
if (activity == "basketball" ){
caloriesPerMinute = 0.0175 * basketball * weight;
cout << "You burned" << " " << caloriesPerMinute;
}
else if ( activity == "running" ){
caloriesPerMinute = 0.0175 * running * weight;
cout << "You burned" << " " << caloriesPerMinute;
}
else if ( activity == "sleep" ){
caloriesPerMinute = 0.0175 * sleep * weight;
cout << "You burned" << " " << caloriesPerMinute;
}
return 0;
}
Last edited on Sep 10, 2016 at 10:38pm UTC
Sep 10, 2016 at 10:53pm UTC
If you want to compare strings, it is better to use the strcmp() and not the equal to (==) operator.
So something like this:
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
#include <iostream>
using namespace std;
int main()
{
int running = 10;
int basketball = 8;
int sleep = 1;
int weight = 0;
char activity[10];
double caloriesPerMinute;
{
cout << "Enter weight here:" ;
cin >> weight;
cout << "You entered:" << " " << weight << endl;
cout << "Enter activity here:" ;
cin >> activity;
cout << "You entered:" << " " << activity << endl;
}
if (strcmp(activity, "basketball" ))
{
caloriesPerMinute = 0.0175 * basketball * weight;
cout << "You burned" << " " << caloriesPerMinute;
}
else if ( strcmp(activity, "running" ))
{
caloriesPerMinute = 0.0175 * running * weight;
cout << "You burned" << " " << caloriesPerMinute;
}
else if ( strcmp(activity, "sleep" ))
{
caloriesPerMinute = 0.0175 * sleep * weight;
cout << "You burned" << " " << caloriesPerMinute;
}
return 0;
}
strcmp is a function that compares two strings, it stands for string comparison.
Last edited on Sep 10, 2016 at 10:54pm UTC
Sep 10, 2016 at 10:53pm UTC
You can't use the comparison operators to compare C-strings, you will need to either start using C++ strings or use strcpy().
Sep 10, 2016 at 11:05pm UTC
strcmp doesn't seem to work. After trying that it has been telling me that strcmp hasn't been declared.
Sep 10, 2016 at 11:07pm UTC
It works fine on my xcode, perhaps you should #include string?
Sep 10, 2016 at 11:19pm UTC
I just used #include <string.h> and that solved my issue. Thanks for the help.
Sep 10, 2016 at 11:25pm UTC
However, my if-else statement is no longer working. No matter what I type in for the activity it runs the basketball formula.
Sep 10, 2016 at 11:30pm UTC
Hmm, seems like they're all testing true.
Sep 10, 2016 at 11:34pm UTC
So basically, if two strings are the same, strcmp will return 0, if they're not, the function will return 1, and since 1 is interpreted as true, several statements were being executed. So I just added these == 0 to tell the compiler specifically what I want.
Last edited on Sep 10, 2016 at 11:35pm UTC
Sep 10, 2016 at 11:37pm UTC
That works. Why is it necessary to include "== 0"?
Sep 10, 2016 at 11:41pm UTC
Because apparently the strcmp() string function returns 0 if two strings are the same.
Sep 10, 2016 at 11:52pm UTC
Thanks. You've been a lifesaver.