0
struct Month {
  string month;
  float avg;
};

float calMonth1() {
  float sum;
  int i;
  Month month1;
  //month 1
  cout << "Weekly Dengue Case for Month of: ";
  cin >> month1.month;

  int n = 4;
  int * week = new int[n];

  for (i = 0; i < n; ++i) {
    cout << "Week " << i + 1 << ": ";
    cin >> week[i];
    sum += week[i];
  }

  month1.avg = sum / i;
  int firstWeek = week[0];

  cout << "Average for " << month1.month << ": " << month1.avg;
  return month1.avg;
}

float calMonth2() {
  int i;
  float sum;
  Month month2;
  //month 2
  cout << "Weekly Dengue Case for Month of: ";
  cin >> month2.month;

  int n = 4;
  int * week = new int[n];

  for (i = 0; i < n; ++i) {
    cout << "Week " << i + 1 << ": ";
    cin >> week[i];
    sum += week[i];
  }

  month2.avg = sum / i;

  cout << "Average for " << month2.month << ": " << month2.avg;
  return month2.avg;
}

int main() {
  int firstWeek;
  calMonth1();
  calMonth2();

  cout << "\nInformation for first week of January, total of " << firstWeek << " cases:\n";

  return 0;
}

This is my code. What i want to do is to use firstWeek in the calMonth1 function into my main function. How can i possibly do that? I want to use only the first value of the array in calMonth1.

Example output:

Week 1: 10 Week 2: 20 Week 3: 30 Week 4: 40

Information for first week of January, total of 10 cases:

I would only want the value for the first week to display it in the above message. Thank you

Wander3r
  • 1,801
  • 17
  • 27
unicorny
  • 1
  • 2
  • What is month2? – 273K May 27 '21 at 05:13
  • sorry. i forgotten to include it inside but its basically the same as calMonth1. I already edited the original post and included it in it. – unicorny May 27 '21 at 05:14
  • 3
    You need to assign the value that the function returns to a variable, and print the value of that variable. I'm surprised your book, tutorial or class haven't taught you how to do that. – Some programmer dude May 27 '21 at 05:17
  • you can return value from function then use it or you can make variable global. – N0ll_Boy May 27 '21 at 05:18
  • On an unrelated note, you have *memory leaks* in your functions... You do `new[]` but never `delete[]`. You don't even need the `new[]` as the size of the "arrays" are known at compile-time and you can use actual arrays instead. – Some programmer dude May 27 '21 at 05:18
  • Or is the confusion about you having two ***different*** variables named `firstWeek` in two different functions, and you assume that those would be the same? Well your book, tutorial or class should have told you that isn't true, and taught you about scopes and life-times as well. – Some programmer dude May 27 '21 at 05:25
  • Yes...I am still new actually and am self learning and im trying to improve. Anyways, thank you guys for your help – unicorny May 27 '21 at 05:32
  • 2
    Avoid global variables. They can greatly complicate understanding and debugging code because a global variable can be changed as a side-effect of any function completely unadvertised. – user4581301 May 27 '21 at 05:33
  • Okay, noted. It will be great if you can show me how to do this? "assign the value that the function returns to a variable, and print the value of that variable." – unicorny May 27 '21 at 05:38
  • 3
    `float value = calMonth1();`. Now you can use `value` for your nefarious purposes in `main`. This is basic stuff covered in the first chapters of any credible C++ programming text. This is why Some Programmer Dude is puzzled. You literally can't miss it if you're learning with any sort of competent guide. If you don't have [a good book on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), get one. It'll save you a lot of time over trying to learn completely on your own. – user4581301 May 27 '21 at 05:43
  • Okay i got it. Thank you thank you thank you so much! Will keep improving my C++. Thank you so much – unicorny May 27 '21 at 06:02

0 Answers0