C++ - 不跳出循环

标签 c++

我正在创建一个程序来计算标准偏差和平均值。当用户输入“#”时,程序应显示四舍五入到小数点后两位的结果。我的代码获取值,但是当我输入“#”时,程序不会退出 4 外观并在 else if 语句中显示该 block 。它只会使 IDE 崩溃。代码如下。

using namespace std;

int main(){
vector<double> myValues;
double val = 0.00;
int n = 1;
double sum = 0.00;
double average = 0.00;
double sumStandDif = 0.00;
double standDev = 0.00;

for(int i = 0 ; i < n ; i++){
    cout << "Enter value " << n << ":";
    cin >> val;
    myValues.push_back(val);

    if (to_string(val) !=  "#"){
        sum = sum + myValues[i];
        average = sum/myValues.size();
        n += 1;

    }else if(to_string(val) == "#"){
        i = n + 1;
        for(int j = 0 ; j < myValues.size() - 1 ; j++){
            sumStandDif +=  (myValues[j] - average)*(myValues[j] - average);
        }
        standDev = sqrt((1/(myValues.size() -1))*sumStandDif);
        cout << "The average is " << average;
        cout << "The standard deviation is " <<  standDev;
        break;
    }
}
return 0;
}

更新:解决方案

 using namespace std;   //allow access to all symbols in std namespace

int main(){                     //main method
vector<double> myValues;    //initializes/contructs an empty vector, with no elements
string val;                 //declare string val (input value)
int n = 1;                  //declare and initialize and assign int n a value of 1
double sum = 0.00;          //declare and initialize sum
double average = 0.00;      //declare and initialize average
double sumStandDif = 0.00;  //declare and initialize sumStandDif
double standDev = 0.00;     //declare and initialize standDev

for(int i = 0 ; i < n ; i++){               //declare and initialize for loop
    cout << "Enter value " << n << ": ";    //output stream, asks to enter a value
    cin >> val;                             //input stream, stores input value in string val
    if (val !=  "#"){                       //checks if the string val is #, if not then go into if block
        myValues.push_back(stod(val));      //Converts the value of string val to double and store it in the vector myVales
        sum = sum + myValues[i];            //Calculates the running sum of the values, while iterating over the for loop
        average = sum/myValues.size();      //Calculates the average of the values in myValues
        n += 1;                             //increment n by 1 to continue iterating over the for loop

    }else {                                                                             //if string val is equal to '#' proceed into else block
        for(int j = 0 ; j < myValues.size() ; j++){                                 //calculates the summation of (myValues[j]-average)^2 by iterating over for loop and not including '#'
            sumStandDif +=  (myValues[j] - average)*(myValues[j] - average);
        }
        standDev = sqrt((1.00/myValues.size())*sumStandDif);                    //Calculates the standard deviation once it calculates the summation
        cout << endl;                                                                   //new line
        cout << fixed << "The average is " << setprecision(2) << average;               //Output stream prints the average while rounding it to two decimals
        cout << endl;                                                                   //new line
        cout << fixed <<"The standard deviation is " << setprecision(2) << standDev;    //Output stream prints the standard deviation while rounding it to two decimals
        cout << endl;                                                                   //new line
        break;                                                                          //break/exit out of for-loop
    }
}
return 0;                   //terminate program
}

最佳答案

由于 val 不用于计算,因此可以安全地将其声明为字符串,这样就不需要 to_string 了。 还有,

}else if(to_string(val) == "#"){

可以像这样简单 }其他{

关于C++ - 不跳出循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688895/

相关文章:

c++ - UUID生成器问题

C++ 析构函数无法正常工作,编译错误,只是试图用 destr 删除一个成员变量

c++ - lambda 转换为 bool 而不是推导函数指针类型

c++ - 为什么 clang++ 报告与 "value stored to ' .. .' during its initialization is never read"的结构化绑定(bind)?

c++ - OpenCV在内存中加载视频

c++ - 与 g++ 链接时 undefined symbol ?我该如何解决这个问题?

c++ - 在 C++ 中使用 SDL 的简单声波发生器

c++ - 比较从递归树分支返回的 vector

c++ - 如何在 Matlab/C++ 中合并两个具有不同增益的图像

c++ - 通过 stdio 的 HTTP POST/GET