CANNOT IDENTIFY ERRORS

I am having difficulty to identify the errors with this coding here. Can anyone help please?

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
int pollution_level[6];
int average;
int counter =0;

while (counter<=6)
{
cout<<"Enter pollution level for day "<<counter<<" range (1-100): ";
cin>>pollution_level[counter];
average = average + pollution_level[counter];
}
cout<<endl;
cout<< "The average pollution level over those 6 days was :"<<average/6<<endl;

return 0;
}
Missing a counter++ in ur loop after average equal average + pollution
How do I do that please? I have been told there are 3 errors in this program.
Well, you need to go through the code line by line as if you were the computer executing the code.

Before you start, write down the test data and the expected results.

Write down the values of the important variables. In this case as a minimum that would be counter and average.

Step through the code line by line, write down the values each time they change, and follow the loop through each iteration. You might also compile and run the program, (possibly using a debugger).

Watch out for deviations from the required behaviour or expected results.

well error two is the line with aver += pollution_level[counter] how can you add a value to something that has no value? you're probably getting like 4563423823123
Last edited on
I have tried to complete this program by fixing errors. Not possible. Can anyone correct the errors for me please? I am completely new to C++
Have u tried setting average requal to 0 when u initialize? What good for you would it be for us to do for you instead of help you
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


int main()
{
int pollution_level[6];
int average = 0;
int counter =0;


string temp = "";
while (counter < 6)
{
cout<<"Enter pollution level for day "<<counter<<" range (1-100): ";
getline(cin, temp);
pollution_level[counter] = atoi(temp.c_str());
average += pollution_level[counter];
counter ++;
}
cout<<endl;
cout<< "The average pollution level over those 6 days was :"<<average/6<<endl;

return 0;
}
What's the point in inputting a string then convert to an int instead of.just inputting an int for something simple like this unless you are trying to weed out bad characters but you just did string >int
I am just stuck with this and now I have been given more instructions to get this going.

Modify the code to catch faulty values – if the user enters a number outside the range 1 to 100, tell him off and repeat the cycle of prompt and reading in. Your code must be able to do this any number of times (allowing for really stupid user) – you will need to use a 'while' loop for this ie
while (user is stupid)
Tell him off
Prompt for new input
Get input
Check value of input
and thanks @Pilla means alot mate
Topic archived. No new replies allowed.