C++ assignment help

Sep 10, 2016 at 10:37pm
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
Sep 10, 2016 at 10:53pm
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
Sep 10, 2016 at 10:53pm
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
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
It works fine on my xcode, perhaps you should #include string?
Sep 10, 2016 at 11:19pm
I just used #include <string.h> and that solved my issue. Thanks for the help.
Sep 10, 2016 at 11:24pm
Any time.
Sep 10, 2016 at 11:25pm
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
Hmm, seems like they're all testing true.
Sep 10, 2016 at 11:33pm
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;
}
Sep 10, 2016 at 11:34pm
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
Sep 10, 2016 at 11:37pm
That works. Why is it necessary to include "== 0"?
Sep 10, 2016 at 11:41pm
Because apparently the strcmp() string function returns 0 if two strings are the same.
Sep 10, 2016 at 11:45pm
Sep 10, 2016 at 11:52pm
Thanks. You've been a lifesaver.
Topic archived. No new replies allowed.