程序未输出正确的标准偏差.另外,验证问题 [英] Program not outputting the correct standard deviation. Also, problems with validation

查看:105
本文介绍了程序未输出正确的标准偏差.另外,验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序当前有两个问题.当我尝试运行程序时,我没有得到正确的标准偏差,而且,我不确定如果用户输入的不是double值,如何实现一种验证用户输入的方法.我将如何解决这个问题?谢谢.我的代码有点混乱,如果可能的话,我想提出一些建议,以使它更好.我将发布的程序无法运行,因此,如果要编译和运行该程序,请取消注释输入并注释掉验证.谢谢.我得到的标准偏差始终低于实际标准偏差.

My program currently has two problems. I am not getting the correct standard deviation when I try to run my program and also, I am unsure about how to implement a way to validate the user's input if they enter anything other than a double value. How would I go about fixing this? Thanks. My code is a bit messy, and I'd like suggestions on how to make it nicer if possible. The program I will post does not run, so if you want it to compile and run, uncomment out the inputs and comment the validation out. Thanks. The standard deviation I get is always below the actual standard deviation.

#include <iostream>
#include <vector>
#include <numeric>
#include <math.h>

double average(std::vector<double> & values) {  // The Average function. Obtainin by accumulation method.
double sum = std::accumulate(values.begin(), values.end(), 0.0);
double average = sum/values.size();
return average;
}





double square(int x) { // Square function. Just x*x. The simplist.
double squared = x*x;
return squared;
}






double stddeviation (std::vector<double> & values) { 
double lessmean;
double totalsquare;
double variance;
double standev;
std::vector<double> lessmeanvector; // Subtracting average from each number in the vector.
for (int i = 0; i<values.size(); i++) {
    lessmean = (values.at(i)-average(values));
    lessmeanvector.push_back(lessmean);
    }
//std::vector<double> squaredvector; // Squaring each number in the vector.
for (int i = 0; i<lessmeanvector.size(); i++) {
    totalsquare+= square(lessmeanvector.at(i));
    }   
variance = (totalsquare/(values.size())-1);
standev = pow(variance,0.5);
std::cout<<"The standard deviation is "<<standev<<".\n";
}







int main() {

int total; // Decleration of main variables.
double numbers;
std::cout<<"How many numbers would you like to enter in?\n"; 
int total = 0;
    while(!(std::cin >> total)){
        std::cin.clear();
        std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
        std::cout << "Invalid input. Try again: ";
    }
//std::cin>>total;
std::cout<<"Enter "<<total<<" numbers in now.\n";

std::vector<double>values;
for(int i = 0; i<total; ++i) {
    while(!(std::cin >> numbers)){
        std::cin.clear();
        std::cin.ignore(numeric_limits<streamsize>::max(), '\n');
        std::cout << "Invalid input.  Try again: ";
    }
    //std::cin>>numbers;
    values.push_back(numbers);
    }
std::cout<<"The average is "<<average(values)<<".\n";
stddeviation(values);

}

推荐答案

验证数字的一种方法是读取数字,然后读取以下空格.到那时,您应该已经阅读了所有字符.如果不是,那么结尾处会有一些额外的字符.但是您需要从可能用完的输入中执行此操作(std :: cin会一直持续运行,而无需用户干预).

One way to validate numbers is to read the number and then read the following whitespace. At that point you should have read all the characters. If not you have some extra characters at the end. But you need to do that from an input that can run out (std::cin goes on forever without user intervention).

所以我要做的是将整个行读为一个字符串,并使用std::istringstream来转换和验证输入,如下所示:

So what I do is read in the whole line as a string and use a std::istringstream to convert and validate the input like this:

double numbers;

for(std::string line; std::getline(std::cin, line);)
{
    if((std::istringstream(line) >> numbers >> std::ws).eof())
        break;

    std::cout << "Invalid input.  Try again: ";
}

这不是完美的,因为如果仅输入空格,它将成功.您需要为此添加额外的检查.为此,我通常使用字符串trim()函数删除周围的空白字符(空格和制表符等).

It's not perfect because if you enter only spaces it succeeds. You need to add an extra check for that. For that I usually employ a string trim() function to remove surrounding whitespace characters (spaces and tabs etc...).

// remove surrounding whitespace
std::string& trim(std::string& s, char const* ws = " \t")
{
    s.erase(0, s.find_first_not_of(ws));
    s.erase(s.find_last_not_of(ws) + 1);
    return s;
}

给予:

double numbers;

for(std::string line; std::getline(std::cin, line);)
{
    if(!trim(line).empty())
        if((std::istringstream(line) >> numbers).eof())
            break;

    std::cout << "Invalid input.  Try again: ";
}

现在您不需要跳过输入末尾的空格,因为您已经对其进行了修剪.

Now you don't need to skip the whitespace at the end of the input because you already trimmed it.

这篇关于程序未输出正确的标准偏差.另外,验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆