我正在创建一个程序来计算标准偏差和平均值。当用户输入“#”时,程序应显示四舍五入到小数点后两位的结果。我的代码获取值,但是当我输入“#”时,程序不会退出 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/