0

I'm new to coding. I wrote the below code in C++ and I am not allow to use array.

You will create a console C++ program that uses a nested loop to enter each archer's individual end scores and then displays the total score for each archer.

I am stuck at how to calculate the total end score:

#include <iomanip>

using namespace std;

int main()
{
    int Rounds = 4;
    int Archers = 3;
    int endScore ;

    int average;

    for (int a = 1; a <= Archers ; a++)
    {
        cout <<  endl << "Number " << a << " score" << endl;
        int tEndScore = 0 ;

        for(int i=1; i <=Rounds ; i++)
        {
            cout << "Round " << i << " : " ;
            cin >>  endScore;
            while(cin.fail())           
            {
                cout << endl << "not enter an integer " << endl ;                             
                cout << "Please enter an integer ";
                cin >> endScore;

            }
            tEndScore += endScore;

        }


        cout << endl << "The total score for 4 ends of Archer Number " << a << " is " << tEndScore << endl;
        average =(double) tEndScore/Rounds;
        cout << setiosflags(ios::fixed) << setprecision(2) << endl << "The average score of 4 ends of Archer Number " << a << " is " << average << endl;

    }
}

This is the result after running. It will only use the last value I entered as tEndScore:

image

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Mia Ng
  • 3
  • 2
  • tEndScore and totalEndScore is exactly the same, I haven't fixed the name when posted. My question is after entering 4 value for end score ( 4 rounds), how can I add them together, calculate for the total value because they don't have the specific name for each end score – Mia Ng Jan 29 '20 at 06:16
  • `tEndScore =+ endScore;` is IMHO valid code, identical to: `tEndScore = +endScore;` although you probably intended to write `tEndScore += endScore;`. Concerning `totalEndScore`, you should have got compiler complaints, didn't you? – Scheff's Cat Jan 29 '20 at 06:16
  • A hint: proper indentation helps to manage the program structure easier... – Scheff's Cat Jan 29 '20 at 06:18
  • tEndScore and totalEndScore is exactly the same, I haven't fixed the name when posted. My question is after entering 4 value for end score ( 4 rounds), how can I add them together, calculate for the total value because they don't have the specific name for each end score – Mia Ng Jan 29 '20 at 06:18
  • Please, see the comments about `=+` vs. `+=`. There is no operator `=+` but the compiler reads this as two operators `=` `+`. Btw. you should initialize `tEndScore`: `int tEndScore = 0;`. Otherwise, `tEndScore` is uninitialized which results in [U.B.](https://stackoverflow.com/a/4105123/1505939). – Scheff's Cat Jan 29 '20 at 06:20

1 Answers1

1

You need to shift tEndScore =+ endScore; this line inside the second for loop as

for(int i=1; i <=Rounds ; i++)
{
    ...
    ...
    tEndScore += endScore;
}

And it will be a good practice (And mandatory for your code...) to initialize the tEndScore for each player as

for (int a = 1; a <= Archers ; a++)
{
    tEndScore = 0;
    endScore = 0;
    average = 0;
    ...
    ...
}

You need to replace totalEndScore to tEndScore and totalRounds to Rounds.

halfer
  • 19,824
  • 17
  • 99
  • 186
RC0993
  • 898
  • 1
  • 13
  • 44