totals not adding up

Hi there

I apologize if there is a solution to this. I have however browsed the forum and have not found something that fits.

When running this function the total is wrong. For example if I enter 6 Marks of 50 each it totals to 341 instead of 300. I am sure it is a simple mistake .


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
//Function to input marks
void getMarks (const string subjectNameP[], int markResultsP[], int num)
{
    int mark, i, total;

    i =0;

    do
    {
            cout    <<  "Subject: " << subjectNameP[i];
            cout    <<  endl;
            cout    <<  "Mark: ";
            cin     >>   mark;
            cout    <<  endl;

            if (mark >=0 && mark<=100)
                {
                markResultsP[i] = mark;
                total += markResultsP[i];
                i++;


                }
            else
            {
            cout    << "**PLEASE ENTER A VALID MARK** "<<endl<<endl;
            }


    }while ( i<num);

cout <<total<<endl;
Never mind.

I should have initialized total =0;

I am leaving this here in case someone else has a similar error.
Topic archived. No new replies allowed.