This is what i have for code this far, i need help trying to keep score here. Any help would be well appreciated thanks

int sum = 0, teamScore, numPoints, score1 = 0, score2 = 0;

while (teamScore != -999)
{
cout << "Did team 1 or team 2 score? (Enter 1, 2 or -999 to quit): ";
cin >> teamScore;
cout << "How many points did they score? (1, 2, or 3): ";
cin >> numPoints;

if (teamScore ==1 && teamScore != -999)
{
cout << "Team 1" << score1 = sum + numPoints;
cout << "Team 2" << score2 = sum + numPoints;
}
else if (teamScore ==2 && teamScore != -999)
{
cout << "Team 1" << score1 = sum + numPoints;
cout << "Team 2" << score2 = sum + numPoints;
}

Dani AI

Generated

Notes for : the current code has three core problems. First, teamScore is tested in the while condition before it ever gets a value (undefined behavior). Second, sum is initialized to 0 but never changed, so score1 = sum + numPoints does not accumulate points. Third, both branches assign to both score1 and score2, so both teams always get the same update. is correct that checking teamScore != -999 inside the if is redundant if the sentinel is handled right. Also avoid assignments inside a cout expression — it’s legal but hides intent and makes debugging hard.

A minimal, robust pattern is: initialize variables, read the team, break on the sentinel, read and validate the points, then update the correct score with += and print the running total. For example:

#include <iostream>

int main() {
    int team = 0, points = 0;
    int score1 = 0, score2 = 0;

    while (true) {
        std::cout << "Team (1, 2, or -999 to quit): ";
        if (!(std::cin >> team) || team == -999) break;

        std::cout << "Points (1, 2, or 3): ";
        if (!(std::cin >> points)) break;

        if (points < 1 || points > 3) { std::cout << "Invalid points\n"; continue; }
        if (team == 1) score1 += points;
        else if (team == 2) score2 += points;
        else { std::cout << "Invalid team\n"; continue; }

        std::cout << "Score: Team1 " << score1 << " - Team2 " << score2 << '\n';
    }

    std::cout << "Final: Team1 " << score1 << " - Team2 " << score2 << '\n';
}

Quick tips: use a named constant for the sentinel, check std::cin for bad input and clear it if needed, and keep assignment and output as separate statements for clarity and fewer bugs.

if (teamScore ==1 && teamScore != -999) If team = 1 it can't be -999. Simplify both if statements

cout << "Team 1" << score1 = sum + numPoints;
cout << "Team 2" << score2 = sum + numPoints;

Did they both score the points? Once the output is done, what's the value of sum ? Is sum important?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.