Help Needed!!

I am trying to create a code for a class and I am stuck....

Basically I have to use nested for loops to calculate the total flu cases for each week, determine the level of activity for the week
and output to a bar chart

I have the loops working to prompt for types of cases per week, and to total that amount and save to a variable. What I can't figure out is how to keep that total intact when the loop runs again, ie not overwrite the total I already have.

I may not be explaining it well, I don't fully understand it myself and I am very new to the coding world.

Please help,


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
 #include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int weekNumber = 0; // week number
    char flutype; // type of flu reported
    int casesA = 0; //number of type A cases
    int casesB = 0; // number of type B cases
    int casesC = 0; //number of type C cases
    int countWeek = 0; // number of cases per week
    int weeks = 0; //total number of weeks




    cout << "How many weeks will go in the report? ";
    cin >> weeks;

    for (int weekNumber = 0; weekNumber < weeks; weekNumber++){

        for (flutype = 'A'; flutype <= 'C'; flutype++){
          cout << "Week " << (weekNumber + 1)
          <<" Enter the number of cases of type "
          << flutype << ": ";
            if (flutype == 'A'){
                cin >> casesA;}
            else if (flutype == 'B'){
               cin >> casesB;}
            else if (flutype == 'C'){
               cin >> casesC;}
        }
        countWeek = (casesA + casesB + casesC);
                if (countWeek < 500)
                    cout << "Low";
                else if (countWeek < 2000)
                    cout << "Moderate";
                else
                    cout << "Widespread";



    }
cout << endl;

cout << countWeek;
  //  cout <<"Week"<< setw(10) << "Total Cases" << setw(16) << "Activity"
         << "      Chart" << endl;
   // cout << weekNumber << countWeek <<

    //cout << "Total number of flu cases: " << weekNumber<< (casesA + casesB + casesC) << endl;

return 0;

    }
Use a separate count integer for each week and then sum total that count for your count week.

Here's an example. I also added line breaks after Low/Moderate/Widespread, because it was causing formatting errors, and line 50 should be commented out like line 49.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{

  ///VARIABLES
  int weekNumber = 0; // week number
  char flutype; // type of flu reported
  int casesA = 0; //number of type A cases
  int casesB = 0; // number of type B cases
  int casesC = 0; //number of type C cases
  int countWeek = 0; // number of cases per week
  int count = 0; //Count for each week.
  int weeks = 0; //total number of weeks

  cout << "How many weeks will go in the report? ";
  cin >> weeks;

  for (int weekNumber = 0; weekNumber < weeks; weekNumber++){

    for (flutype = 'A'; flutype <= 'C'; flutype++){
      cout << "Week " << (weekNumber + 1)
      <<" Enter the number of cases of type " << flutype << ": ";
        if (flutype == 'A') cin >> casesA;
        else if (flutype == 'B') cin >> casesB;
        else if (flutype == 'C') cin >> casesC; 
    } //End nested for loop

    count = (casesA + casesB + casesC); //This count will get overwritten each loop.
    if (count < 500) cout << "Low" << endl;
    else if (count < 2000) cout << "Moderate" << endl;
    else cout << "Widespread" << endl;
    countWeek += count; //This count will sum-total (+=) all the weeks, each loop.
  } //End main for loop

cout << "\nTotal cases: " << countWeek; //Output just the sum total.

//cout <<"Week"<< setw(10) << "Total Cases" << setw(16) << "Activity" << "      Chart" << endl;
//cout << weekNumber << countWeek << 
//cout << "Total number of flu cases: " << weekNumber<< (casesA + casesB + casesC) << endl;

  return 0;
}


Example of my test:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 ./main
How many weeks will go in the report? 3
Week 1 Enter the number of cases of type A: 1
Week 1 Enter the number of cases of type B: 1
Week 1 Enter the number of cases of type C: 1
Low
Week 2 Enter the number of cases of type A: 5000
Week 2 Enter the number of cases of type B: 1
Week 2 Enter the number of cases of type C: 1
Widespread
Week 3 Enter the number of cases of type A: 1999
Week 3 Enter the number of cases of type B: 0
Week 3 Enter the number of cases of type C: 0
Moderate

Total cases: 7004
Last edited on
Thank you, that worked perfectly.

One more question, how would I get it to output an * for every 250 cases?

Again sorry for all the simple questions.
Got it!!

Thanks again for all of your help!
Topic archived. No new replies allowed.