简体   繁体   中英

I don't know why my program is not entering the loop

#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;


}

here is test.txt file that I link to http://www.buildingthepride.com/faculty/jajerkins/cs155-01/population2014.txt . it seems that the program wont enter the if(population < 10){ if (population == 1) loop. I checked it using cout and the population is being reduced to a single digit

Yes, the return is the reason.

Also let me help you on your code.

1.- Does it really make sense to check if population is smaller than 10? If you want to avoid illogical errors better compare against negative values and warn the user if he indicated that in the text file. With the while (population >= 10) you are forcing the value become < than 10.

2.- Why to do all that complex comparison? How about:

int counter[10];
counter[population-1]++;

after

while (population >= 10) {
  population = (population/10);
} 

instead of using all these ifs and comparing population again and again.

3.- Why are you using return anyways? You are breaking the whole program. Also inFile.close() is not going to happen that way. How about:

for(int i=0 ; i<10 ; i++)
{
    cout << "Number " << i << ": " << counter[i] << std::endl;
}

after the line

inFile.close();

4.- If you want to test some values why not break the while(!inFile.eof()) loop and let cout show you the variables.

5.- Don't use float if you really mean integers. As you are counting quantities, use int or better yet unsigned int .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM