#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
int ranking = 0;
int population = 0;
float leadingNumPercent = 0;
float oneCounter = 0;
float twoCounter = 0;
float threeCounter = 0;
float fourCounter = 0;
float fiveCounter = 0;
float sixCounter = 0;
float sevenCounter =0;
float eightCounter =0;
float nineCounter =0;
float overAllCounter =0;
int i =0;
string countryName;
ifstream inFile;
inFile.open("test.txt");
while(!inFile.eof()){
inFile >> ranking;
inFile >> population;
getline(inFile, countryName);
while (population >= 10) {
population = (population/10);
}
if (population < 10){
if (population == 1){
oneCounter++;
return oneCounter;
}
if (population == 2){
twoCounter++;
return twoCounter;
}
if (population==3){
threeCounter ++;
return threeCounter;
}
if (population==4){
fourCounter ++;
return fourCounter;
}
if (population==5){
fiveCounter ++;
return fiveCounter;
}
if (population==6){
sixCounter ++;
return sixCounter;
}
if (population==7){
sevenCounter ++;
return sevenCounter;
}
if (population==8){
eightCounter ++;
return eightCounter;
}
if (population==9){
nineCounter ++;
return nineCounter;
}
}
leadingNumPercent = (oneCounter/238)*100;
cout << leadingNumPercent;
}
inFile.close();
return 0;
}
這裏是test.txt文件,我鏈接到http://www.buildingthepride.com/faculty/jajerkins/cs155-01/population2014.txt。它似乎不會進入if(人口< 10){if(population == 1)循環。我使用cout進行了檢查,人口正在減少到一位數我不知道爲什麼我的程序沒有進入循環
'我用cout檢查過了'你有'return oneCounter; cout << oneCounter;'return之後的語句當然不會運行。 –
,因爲人口有價值0 –
我改變了一個計數器的回報,它正在進入循環,但是當我離開if循環並嘗試使它成爲一個百分比時,它出現空白,當我cout –