Hello forum. I am new to programming, I understand most of the concepts but sometimes get stuck at some easy problems.
Here I need to ask user to manually type in the id number of the team, enter win lose and draw data. It then calculates total games value and checks if its less or equal to 16. Then my program should calculate the winning average which is wins/games and output the averages as 0.9876 ( 4 decimals ) form. It just outputs it as a 0 and not the desired form. I tired using it as float, long and double but it is always gives me the same result. Please help. Thanks.
//
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int wins, losses, draws;
int remaining, games;
float w_average;
int team [10];
for (int n_teams=1; n_teams<=10; n_teams++)
{
cout << "Enter the team number: ";
cin >> team[n_teams];
cout << "Enter the number of Wins: ";
cin >> wins;
cout << "Enter the number of Losses: ";
cin >> losses;
cout << "Enter the number of Draws: ";
cin >> draws;
cout << "The number of Wins: " << wins << endl;
cout << "The number of Losses: " << losses << endl;
cout << "The number of Draws: " << draws << endl;
cout << "The number of Games: " << games << endl;
cout << setprecision (5) << "The winning average is: " << w_average << endl;
if (remaining ==0)
cout << "Season is Finished!" << endl;
else if (remaining <0)
{
cout << "Enter Valid Data!" << endl;
cout << "Enter the number of Wins: ";
cin >> wins;
cout << "Enter the number of Losses: ";
cin >> losses;
cout << "Enter the number of Draws: ";
cin >> draws;
cout << "The number of Wins: " << wins << endl;
cout << "The number of Losses: " << losses << endl;
cout << "The number of Draws: " << draws << endl;
}
else
cout << "The number of Games Remaining: " << remaining << endl;
Thanks for quick reply...
it works for most of the time unless there are numbers like 0.2500 then the program sets the value as 0.25 not the 4 decimals
here its the input and output:
Enter the team number: 1234
Enter the number of Wins: 4
Enter the number of Losses: 7
Enter the number of Draws: 5
The number of Wins: 4
The number of Losses: 7
The number of Draws: 5
The number of Games: 16
The winning average is: 0.25
Season is Finished!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int wins, losses, draws, teamID,remaining, games, n_teams, final_score;
double w_average;
int team [10];
for (n_teams=1; n_teams<=10; n_teams++) //for loop to stop when total number of teams reaches 8
{
cout << "Enter the team number: "; // let user enter the appropriate values for:
cin >> team[n_teams];
cout << "Enter the number of Wins: ";
cin >> wins; // wins
cout << "Enter the number of Losses: ";
cin >> losses; // losses
cout << "Enter the number of Draws: ";
cin >> draws; // draws
cout << "\n" << endl;
{
cout << "Enter Valid Data!" << endl;
cout << "Enter the number of Wins: ";
cin >> wins;
cout << "Enter the number of Losses: ";
cin >> losses;
cout << "Enter the number of Draws: ";
cin >> draws;
cout << "The Team ID: " << teamID << endl;
cout << wins << " Wins " << losses << " Losses " << draws << " ties " << endl;
cout << "The final team score is: " << final_score << endl;
}
cout << "The Team ID: " << teamID << endl;
cout << wins << " Wins " << losses << " Losses " << draws << " ties " << endl;
cout << "The number of Games played : " << games << endl;
if (remaining ==0)
cout << "Season is Finished!" << endl;
else if (remaining !=0)
cout << "The number of Games Remaining: " << remaining << endl;
else cout << endl;
cout << fixed << setprecision (4) << "The winning average is: " << w_average << endl;
if (draws > wins) //if statements for draws versus wins
cout << "The number of games tied is greater than the number of games won" << endl;
else if (draws == wins)
cout << "The number of games tied is equal to the number of games won" << endl;
else if (draws < wins)
cout << "The number of games tied is not greater than the number of games won" << endl;
if (losses > wins) //if statements for losses versus wins
cout << "The number of games tied is greater than the number of games lost"<< endl;
else if (losses == wins)
cout << "The number of games lost is equal to the number of games won" << endl;
else if (losses < wins)
cout << "The number of games tied is not greater than the number of games lost" << endl;
cout << "The final team score is: " << final_score << "\n" << endl; //calculate the finals team score
}
cout << "Thanks you!"<< endl;
return 0;
}
now the bold section is where i am having problems now...
if i set the values of wins, losses and draws to be more then 16 in sum, the program should say that the data incorrect enter new data and calculate until the sum is 16, but for some reason it does it once and skips to the end.
would you be able to point out what needs to be done. maybe a while loop
THANKS>
> if i set the values of wins, losses and draws to be more then 16 in sum,
> the program should say that the data incorrect enter new data and calculate until the sum is 16,
> but for some reason it does it once and skips to the end.
You can't do something repeatedly with an if statement; an if statement will execute at most once. You need a looping construct of some kind do something repeatedly.
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
// *** postpone defining variables till just before their first use
//int wins, losses, draws, teamID,remaining, games, n_teams, final_score;
// double w_average;
// int team [10]; // you don't need this array, do you?
// *** with the array, this was wrong; array subscrpts go from zero to (N-1)
// for (n_teams=1; n_teams<=10; n_teams++)
for( int n_teams=0; n_teams<10 ; ++n_teams )
{
int teamID, wins, losses, draws ;
cout << "Enter the team number: "; // let user enter the appropriate values for:
//cin >> team[n_teams];
cin >> teamID ;
cout << "Enter the number of Wins: ";
cin >> wins; // wins
cout << "Enter the number of Losses: ";
cin >> losses; // losses
cout << "Enter the number of Draws: ";
cin >> draws; // draws
cout << "\n" << endl;
int games = wins + losses + draws ;
int remaining = 16-games;
while( remaining < 0 )
{
cout << "Enter Valid Data!" << endl;
cout << "Enter the number of Wins: ";
cin >> wins;
cout << "Enter the number of Losses: ";
cin >> losses;
cout << "Enter the number of Draws: ";
cin >> draws;
games = wins + losses + draws ;
remaining = 16-games;
}
// *** move these here, compute after all the input is over
double w_average = double (wins)/games;
int final_score = (2*wins)+draws-losses;
cout << "The Team ID: " << teamID << endl;
cout << wins << " Wins " << losses << " Losses " << draws << " ties " << endl;
cout << "The number of Games played : " << games << endl;
if (remaining ==0)
cout << "Season is Finished!" << endl;
elseif (remaining !=0)
cout << "The number of Games Remaining: " << remaining << endl;
else cout << endl;
cout << fixed << setprecision (4) << "The winning average is: " << w_average << endl;
if (draws > wins) //if statements for draws versus wins
cout << "The number of games tied is greater than the number of games won" << endl;
elseif (draws == wins)
cout << "The number of games tied is equal to the number of games won" << endl;
elseif (draws < wins)
cout << "The number of games tied is not greater than the number of games won" << endl;
if (losses > wins) //if statements for losses versus wins
cout << "The number of games tied is greater than the number of games lost"<< endl;
elseif (losses == wins)
cout << "The number of games lost is equal to the number of games won" << endl;
elseif (losses < wins)
cout << "The number of games tied is not greater than the number of games lost" << endl;
cout << "The final team score is: " << final_score << "\n" << endl; //calculate the finals team score
}
cout << "Thanks you!"<< endl;
//return 0; // this can be omitted; there isan implicit return 0 at the end of main
}
Thank you so much i really appreciate that i will take note of everything you have added or changed. and hopefully in the future I would be as kind and helpful as you are. Thanks.