I have an error on totalWeight, but I'm not sure what it is.

Hi, I'm working on an assignment and I have been able to complete most of it. The only problem I am having is the total weight, which should be 26.56, but it outputs 25.00. I see that it is considering the numbers as integers, but I've defined totalWeight as a double. So I know I'm missing something, I just don't know what. The inputs are 4.6, 9.4, 3.56, and 9.0.
And the output I am currently receiving is:
Enter the weight of Steak 1: 4.6
Enter the weight of Steak 2: 9.4
Enter the weight of Steak 3: 3.56
Enter the weight of Steak 4: 9.0
Mr. Slate's steak weight is 3.56
Pebble's steak weight is 9.40
The total weight of all steaks is 25.00
Here's the 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
  #include <iostream>

using namespace std;

double maxWeight(double steaks[], int numSteaks)
{
    int i;
    double max;
    max = steaks[0];
    for (i = 0; i < numSteaks; i++)
    {
        if (max < steaks[i])
            max = steaks[i];
    }
    return max;
}

double minWeight(double steaks[], int numSteaks)
{
    int i;
    double min;
    min = steaks[0];
    for (i = 0; i < numSteaks; i++)
    {
        if (min > steaks[i])
            min = steaks[i];
    }
    return min;
}

double totalWeight(double steaks[], int numSteaks)
{
    int i, sum = 0;
    for (i = 0; i < numSteaks; i++)
    {
        sum += steaks[i];
    }
    return sum;
}

int main()
{
    double steaks[4];
    int numSteaks = 4, i;

    for (i= 0; i < numSteaks; i++)
    {
        cout << "Enter the weight of Steak " << i + 1 << ": ";
        cin >> steaks[i];
    }   

    cout << "Mr. Slate's steak weight is ";
    printf("%.2lf", minWeight(steaks, numSteaks));
    cout << endl;

    cout << "Pebble's steak weight is ";
    printf("%.2lf", maxWeight(steaks, numSteaks));
    cout << endl;

    cout << "The total weight of all steaks is ";
    printf("%.2lf", totalWeight(steaks, numSteaks));
    cout << endl;

    return 0;
}
Last edited on
Hello Amnesiax,

The only thing that catches my attention is the "printf" statements. Although they work they can be replaced with C++ code and therefor should be avoided.

I will know more when I get the program loaded and tested.

Andy
Hello Amnesiax,

In your function:
1
2
3
4
5
6
7
8
9
10
11
double totalWeight(double steaks[], int numSteaks)
{
    int i, sum = 0;

    for (i = 0; i < numSteaks; i++)
    {
        sum += steaks[i];
    }

    return sum;
}

First unless you need "i" outside the for loop, which you do not, it is better to define it in the for loop. There it becomes a local variable that is destroyed when the loop ends.

Second, and the compiler should have warned you about this, in line 7 you are trying to stuff a "double" into an "int". There will be data loss specifically the decimal portion.

Then you are trying to return an "int" as a "double" which will leave the decimal portion at (0) zero. Not what you want.

Try this:
1
2
3
4
5
6
7
8
9
10
11
double totalWeight(double steaks[], int numSteaks)
{
    double sum = 0;

    for (int i = 0; i < numSteaks; i++)
    {
        sum += steaks[i];
    }

    return sum;
}


Andy
Okay, thank you.
Note that

1
2
3
4
5
6
7
8
9
10
11
12
double maxWeight(double steaks[], int numSteaks)
{
    int i;
    double max;
    max = steaks[0];
    for (i = 0; i < numSteaks; i++)
    {
        if (max < steaks[i])
            max = steaks[i];
    }
    return max;
}


can be 'simplified as'

1
2
3
4
5
6
7
8
9
10
11
double maxWeight(double steaks[], int numSteaks)
{
    double max = steaks[0];

    for (int i = 1; i < numSteaks; i++)
    {
        if (max < steaks[i])
            max = steaks[i];
    }
    return max;
}


and the same for minweight().

i starts at 1 as max is already the value of steaks[0]. A variable can (and should be) initialsied when it is defined.
Topic archived. No new replies allowed.