Wrong Division Result
Why cant i get a result from division, it always comes out to 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include<iostream>
#include<iomanip>
#include<vector>
using namespace std;
int main()
{
vector<int> population;
vector<int> birth;
double birthRate;
int Cities;
int index;
cout<<"How many cities do you have";
cin>>Cities;
for (index=0; index<Cities; index++)
{
int tempPop;
double tempBirth;
cout<<"Population for city " << (index+1) << ": ";
cin >> tempPop;
population.push_back(tempPop);
cout<<"Births for city " << (index+1) << ": ";
cin >> tempBirth;
birth.push_back(tempBirth);
}
for (index=0;index<Cities; index++)
{
birthRate = birth[index] / population[index];
cout << "Birthrate for city #" << (index+1) <<": "<< birthRate<< endl;
}
return 0;
}
|
You cannot get fractions from integer division, so atleast one of the values you are dividing has to be a double or float. Try casting
thanks!
Topic archived. No new replies allowed.