Reputation: 53
This is my first endeavor into for loops and I'm having some problems. I'm trying to write a program that will ask for how many points two teams scored per quarter and then display total points and the winning team.
#include <iostream>
using namespace std;
int main( )
{
int scoreA = 0;
int scoreB = 0;
cout << "This program calculates the average score of 10 tests." << endl;
for (int counter = 0; counter < 4; counter = counter + 1)
{
cout << "Enter Team A's quarterly points: ";
cin >> scoreA;
cout << "Enter Team B's quarterly points: ";
cin >> scoreB;
scoreA = scoreA + scoreA;
scoreB = scoreB + scoreB;
}
cout << "Team A's Score: " << scoreA << endl;
cout << "Team B's Score: " << scoreB << endl;
if (scoreA > scoreB)
{
cout << "Team A wins";
}
else
{
cout << "Team B wins";
}
system("pause");
return 0;
}
Upvotes: 1
Views: 91
Reputation: 58251
The variable in which you stores sum of scores and variable in which you inputs from user should be different. Do like (read comments):
int sumB=0 , sumB=0; // added this
for (int counter = 0; counter < 4; counter = counter + 1){
cout << "Enter Team A's quarterly points: ";
cin >> scoreA;
cout << "Enter Team B's quarterly points: ";
cin >> scoreB;
sumA = sumA + scoreA;
sumB = sumB + scoreB;
// ^ ^
}
In your code you are doing like for example scoreA = scoreA + scoreA;
and cin >> scoreA
both statements over writes each other effects during the loop, and same happens with scoreB
.
Hence accordingly change next lines in your code too, like:
cout << "Team A's Score: " << sumA << endl;
cout << "Team B's Score: " << sumB << endl;
if (sumA > sumB){
// your code
}
else{
// your code
}
Additionally, because you are new to c++ and SO, I would like to suggest a link: The Definitive C++ Book Guide and List
Upvotes: 1
Reputation: 368
You're not saying specifically what isn't working with the loop, but I see in your for loop the following:
cout << "Enter Team A's quarterly points: ";
cin >> scoreA;
cout << "Enter Team B's quarterly points: ";
cin >> scoreB;
scoreA = scoreA + scoreA;
scoreB = scoreB + scoreB;
So you are overwriting the scores in scoreA and scoreB on every iteration (cin >> scoreA
), and then doubling them (scoreA = scoreA + scoreA
).
Upvotes: 1