When this program runs it is supposed to display the total amount of games and the percentage of games won from the total amount of games played. The functions have to be under the main function.
Problem: The program runs and compiles but the percentage is only of type int and does not display the decimal percentage. Any help would be appreciated!
#include <iostream>
#include <iomanip>
#include <string>
usingnamespace std;
int games_won(); // call number of wins
int games_lost(); // call number of losses
int total_games(int,int); // Addition function of total games
float percentGames(int,int); // percentage of games won.
int main()
{
int won;
int lost;
int gamesplayed;
float percent;
cout<< "Welcome to the baseball Calculator" << endl;
won = games_won();
lost = games_lost();
gamesplayed = total_games(won,lost);
percent = percentGames(won,lost);
cin.get();
cin.get();
}
int games_won()
{
int manyWins;
cout<<" How many games did the team win?" << endl;
cin >> manyWins ;
return(manyWins);
}
int games_lost()
{
int manyLost;
cout<< "How many games were lost?" << endl;
cin >> manyLost ;
return(manyLost);
}
int total_games(int manyLost, int manyWins)
{
int gamesCompleted;
gamesCompleted=manyLost + manyWins ;
cout << "Total Games Played: " << gamesCompleted << endl;
return (gamesCompleted);
}
float percentGames(int manyWins, int manyLost)
{
int sum;
float totalPercent;
sum = manyWins + manyLost;
totalPercent = sum % manyWins;
cout << "Percent of games won: "<< setprecision(2) << totalPercent << endl;
return(totalPercent);
totalPercent = sum % manyWins;
Do you know what the % operator does or even what it is called? It is called the modulo/modulus operator, and returns the remainder of division. I think you wanted this: totalPercent = sum * 100.0 / manyWins;