-3

TASK 2 remains un-executed. Task 1 works fine, I input the yield values of the cows; but then the code stops running. A warning says that Herdtotalweek may be uninitialized. But I don't know how to fix that. There are no other warnings or errors.

#include <iostream>
#include <string>
using namespace std;

int main() { //Task 1
  int Herdsize;
  int Day;
  float MilkYield1;
  float MilkYield2;
  int count;

  cout << "Please input herd size" << endl;
  cin >> Herdsize;
  while (Herdsize < 1 || Herdsize > 900) {
    cout << "Please re-input herdsize between 1 and 900" << endl;
    cin >> Herdsize;
  }

  int CowID[Herdsize + 1];
  float DailyYield[Herdsize * 7];
  float WeeklyYieldpercow[Herdsize * 14];

  for (count = 1; count < Herdsize + 1; count++) {
    cout << "Input 3 digit cow id ";
    cin >> CowID[count];
    while (CowID[count] < 1 || CowID[count] > 999) {
      cout << "Please re-input cow a 3 digit cow id " << endl;
      cin >> CowID[count];
    }

    for (Day = 1; Day < 8; Day++) {
      cout << "Please input first milk yield of cow,day";
      cout << Day;
      cout << endl;
      cin >> MilkYield1;
      cout << "Please input second milk yield day:";
      cout << Day;
      cout << ",  if there is a second yield if not enter 0";
      cout << endl;
      cin >> MilkYield2;
    }
    DailyYield[((count - 1) * 7) + Day] = MilkYield1 + MilkYield2;
    WeeklyYieldpercow[count] = WeeklyYieldpercow[count] +
      DailyYield[((count - 1) * 7) + Day];
  }


  // TASK 2
  int count2 = 1;
  float Herdtotalweek;
  float Averagevolume;
  for (count = 1; count2 < Herdsize + 1; count++) {
    Herdtotalweek = Herdtotalweek + WeeklyYieldpercow[count];
  }
  Averagevolume = Herdtotalweek / Herdsize;
  int Herdtotalweekwhole = int(Herdtotalweek + 0.5);
  int Averagevolumewhole = int(Averagevolume + 0.5);
  cout << "Total weekly volume=";
  cout << Herdtotalweekwhole;
  cout << "Average volume =";
  cout << Averagevolumewhole;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115

2 Answers2

1

instead of float Herdtotalweek; try using float Herdtotalweek = 0; ?

also, in your second for statement, instead of for (count=1;count2<Herdsize+1;count++) try for (count=1;count<Herdsize+1;count++) (you were using count2 instead of count, which was probably a copy/paste error)

Mladen B.
  • 2,784
  • 2
  • 23
  • 34
1

The for loop after task2 never completes. It is an infinite loop as you are not updating count2.

penguin2048
  • 1,303
  • 13
  • 25