C++ assignment help

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
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
You can't use the comparison operators to compare C-strings, you will need to either start using C++ strings or use strcpy().

strcmp doesn't seem to work. After trying that it has been telling me that strcmp hasn't been declared.
It works fine on my xcode, perhaps you should #include string?
I just used #include <string.h> and that solved my issue. Thanks for the help.
Any time.
However, my if-else statement is no longer working. No matter what I type in for the activity it runs the basketball formula.
Hmm, seems like they're all testing true.
Okay I fixed it, try 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
39
40
41
42
43
#include <iostream>

using namespace std;

int main()
{
    bool isCompared = false;
    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") == 0)
    {
        cout << "basketball\n";
        caloriesPerMinute = 0.0175 * basketball * weight;
        cout << "You burned" << " " << caloriesPerMinute;
    }
    else if ( strcmp(activity, "running") == 0)
    {
        cout << "Running\n";
        caloriesPerMinute = 0.0175 * running * weight;
        cout << "You burned" << " " << caloriesPerMinute;
    }
    else if ( strcmp(activity, "sleep") == 0)
    {
        cout << "sleep\n";
        caloriesPerMinute = 0.0175 * sleep * weight;
        cout << "You burned" << " " << caloriesPerMinute;
    }

    return 0;
}
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
That works. Why is it necessary to include "== 0"?
Because apparently the strcmp() string function returns 0 if two strings are the same.
Thanks. You've been a lifesaver.
Topic archived. No new replies allowed.