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.
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.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
#include <iostream>
#include <iomanip>
usingnamespace 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;
elseif (flutype == 'B') cin >> casesB;
elseif (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;
elseif (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