C ++ - 不会破坏循环

时间:2015-11-13 08:53:26

标签: c++

我正在创建一个计算标准偏差和平均值的程序。当用户输入'#'时,程序应显示舍入到两个小数点的结果。我的代码获取值,但是当我输入'#'时,程序不会退出4外观并在else if语句中显示该块。它只是崩溃了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;
}

UPDATE:溶液

 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
}

3 个答案:

答案 0 :(得分:1)

val是一个浮点值,因此用户永远不能使用#为其分配cin。首先将val存储在字符串中。

答案 1 :(得分:1)

由于 val 未在计算中使用,因此可以安全地声明为字符串,然后不需要 to_string 。 另外,

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

可以一样简单  }else{

答案 2 :(得分:1)

您可以像这样更改代码:

 string val;//declare val as string
then, after cin>>val check 
if(val!="#"){
   //convert string to double
   // insert double value into vector
   // do other work
}
else if(val=="#"){
// like your code.
}