C++ 我不知道';我不知道为什么我的程序没有进入循环 #包括 #包括 #包括 使用名称空间std; int main(){ int排名=0; 整数总体=0; 浮动导线强度=0; 浮点计数器=0; 浮点计数器=0; 浮点计数器=0; 浮点四计数器=0; 浮点fiveCounter=0; 浮点六计数器=0; 浮点数=0; 浮点数=0; 浮点九计数器=0; 浮动总计数器=0; int i=0; 字符串countryName; 河流充填; infle.open(“test.txt”); 而(!infle.eof()){ 内嵌>>排名; 填充>>人口; getline(infle,countryName); 而(人口>=10){ 人口=(人口/10); } if(总体=10)时,您强制该值小于10

C++ 我不知道';我不知道为什么我的程序没有进入循环 #包括 #包括 #包括 使用名称空间std; int main(){ int排名=0; 整数总体=0; 浮动导线强度=0; 浮点计数器=0; 浮点计数器=0; 浮点计数器=0; 浮点四计数器=0; 浮点fiveCounter=0; 浮点六计数器=0; 浮点数=0; 浮点数=0; 浮点九计数器=0; 浮动总计数器=0; int i=0; 字符串countryName; 河流充填; infle.open(“test.txt”); 而(!infle.eof()){ 内嵌>>排名; 填充>>人口; getline(infle,countryName); 而(人口>=10){ 人口=(人口/10); } if(总体=10)时,您强制该值小于10,c++,loops,C++,Loops,是的,返回是原因 也让我来帮你写代码 1.-检查总体是否小于10真的有意义吗?如果您想避免不合逻辑的错误,最好与负值进行比较,并在用户在文本文件中指出这一点时警告用户。使用while(总体>=10)时,您强制该值小于10 2.-为什么要做这么复杂的比较?那么: #include <iostream> #include <fstream> #include <string> using namespace std; int main(){ int ra

是的,
返回是原因

也让我来帮你写代码

1.-检查
总体
是否小于10真的有意义吗?如果您想避免不合逻辑的错误,最好与负值进行比较,并在用户在文本文件中指出这一点时警告用户。使用
while(总体>=10)
时,您强制该值小于10

2.-为什么要做这么复杂的比较?那么:

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


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

而不是使用所有这些ifs并一次又一次地比较
population

3.-你为什么要使用
return
呢?你破坏了整个程序。而且
infle.close()
也不会这样做。怎么样:

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


}
while (population >= 10) {
  population = (population/10);
} 
4.-如果要测试某些值,为什么不
中断
while(!infle.eof())
循环并让
cout
显示变量


5.-如果你真的是指整数,不要使用
float
。当你在计算数量时,使用
int
或者更好的
unsigned int
我用cout检查了它,你有
return oneCounter;cout,因为在oneCounter中的return周围填充值0I发生了变化,它将进入循环,但当我离开if循环并尝试将它变成一个百分比,当CutBube从文件名中得到它的值时,你在循环的第一次迭代中从函数返回,在你得到代码之前,引号NUMDS/<代码>计算。我建议你重新阅读你喜欢的C++教科书的部分。描述了<代码>返回<代码>语句。IM仍然开始学习C++,我不真正的int计数器[10 ];计数器[Populy1+++;]。我把返回和它的工作做得更好。很好地知道,我确信你会变得更好如果你实践。我实际上得到它工作(正如我需要的分配)。我要做的是得到一个for循环,并处理成吨的if。谢谢你的帮助,我将计数器转换成一个百分比,这就是我选择float的原因。(人口中领先数字的百分比)
inFile.close();