Need help with adding up percentage of two variables

I cant seem to get the two variables to add up to 100% even if the 2 variables are 2 different numbers they seem to come out to the same percentage when 1 should be higher than the other. please help

#include<iomanip>
#include<iostream>
using namespace std;

int main()
{
int Fe; // User enters input
int Ma; // User enters input

float average; // the calculated value

// prompt the user for input
cout << "Enter the number of females in class: ";
cin >> Fe;

cout << "Enter the number of males in class: ";
cin >> Ma;

// calculate the average
average = float(Fe + Ma) / 2;

// Display the output
cout << endl << endl << setprecision(2) << fixed;
cout << "The Precentage of females: "<< average << "%" << endl;
cout << "The precentage of males: " << average << "%" << endl << endl;



system("pause");
return 0;

}
Last edited on
you only calculate one average, surely you will need 2? they're not averages though are they :)

1
2
3
 
float avgFe = static_cast<float>(Fe) / (Fe+Ma) * 100.0f;
float avgMa = static_cast<float>(Ma) / (Fe+Ma) * 100.0f;
Last edited on
You're not calculating the percentage, you're calculating the average of the two values, which is entirely unrelated to the percentage.

percentage_that_subset_makes_up = size_of_the_subset * 100 / total

So if I have 2 apples, 5 oranges, and 3 bananas, I have 2 * 100 / 10 = 20% apples.
so in order for me to add the 2 averages up i would need to add what you provided onto the calculated value, or would i get rid of the previous value before


#include<iomanip>
#include<iostream>
using namespace std;

int main()
{
int Fe; // User enters input
int Ma; // User enters input

float average; // the calculated value

// prompt the user for input
cout << "Enter the number of females in class: ";
cin >> Fe;

cout << "Enter the number of males in class: ";
cin >> Ma;

// calculate the average
average = float(Fe + Ma) / 2;
float avgFe = static_cast<float>(Fe) / (Fe+Ma) * 100.0f;
float avgMa = static_cast<float>(Ma) / (Fe+Ma) * 100.0f;


// Display the output
cout << endl << endl << setprecision(2) << fixed;
cout << "The Precentage of females: "<< average << "%" << endl;
cout << "The precentage of males: " << average << "%" << endl << endl;



system("pause");
return 0;

}
Get rid of the average calculation. It has nothing to do with the percentages. It's a meaningless value.
i got rid of the average calculation but the 2 values are still the same
Well, before you were printing the variable 'average' for both lines. What are you printing now, if you're no longer calculating the average?
Are you printing the same thing for both lines? If so, why would you expect to the printed values to be different?

1
2
3
4
int a = 42;
int b = 24;
std::cout << "This is 42: " << a << std::endl;
std::cout << "Why isn't this 24?? " << a << std::endl;
Oh okay thanks i got it now appreciate the help guys
Topic archived. No new replies allowed.