Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ while循环到错误检查c++;?_C++_While Loop - Fatal编程技术网

C++ while循环到错误检查c++;?

C++ while循环到错误检查c++;?,c++,while-loop,C++,While Loop,有人能看一下这段代码,帮我找出问题所在吗 #include <iostream> #include <vector> using namespace std; int numExercise; cout << "Enter num exercises: "; cin >> numExercise; vector <float> weight; float sumWeight = 0; while(sumWeight != 1) {

有人能看一下这段代码,帮我找出问题所在吗

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

int numExercise;
cout << "Enter num exercises: ";
cin >> numExercise;

vector <float> weight;
float sumWeight = 0;
while(sumWeight != 1)
{   
    // Loop to assign a weighted value to each exercise.
    for ( int i = 0; i < numExercise; i++ )
    {
        float weightEntered;

        cout << "\n Assignment " << i + 1 << " : ";
        cin >> weightEntered;

        //Divide percentage by 100 to get decimals.     
        weightEntered /= 100;

        //Send the data back to the vector.
        weight.push_back( weightEntered );
    }


    // Loop to error check if the total weights isn't 100.

    for ( int i = 0; i < numExercise; i++ )
    {
        sumWeight += weight[i]; 
    }

    cout << sumWeight << endl;
    sumWeight = 0;
    //if ( sumWeight != 1 )

    cout << "\n\t\tError, total weights should be 100" << endl;
}
#包括
#包括
使用名称空间std;
国际核运动;
颈部运动;
向量权重;
浮子重量=0;
while(总重量!=1)
{   
//循环为每个练习指定一个加权值。
for(int i=0;icout您正在使用vector存储权重

对于三个赋值,你输入了30,30,30,现在向量变成了{30,30,30},你对这个向量求和,从0到2。 下一次,你们输入了20,20,40,现在向量变成了{30,30,30,20,20,40},你们再次对这个向量求和,从0到2


您可以使用插入而不是推回

来解决问题。请查看此代码并帮助我找出问题所在?请学习使用调试器。从长远来看,这将比有人为您调试代码更有帮助。解决此类问题的正确工具是调试器。您可以使用调试器在询问堆栈溢出问题之前,您应该逐行检查代码。有关更多帮助,请阅读。至少,您应该[编辑]您的问题将包括一个重现您的问题的示例,以及您在调试器中所做的观察。如果用户输入分数值,例如10.3、56.7等,则此代码注定会失败。原因是您无法保证这些数字会完全等于1.0(或100)当加起来的时候,即使你认为他们会。原因是浮点不精确。