c ++平均程序给出了错误的答案

时间:2015-01-19 11:36:04

标签: c++ average calculator

我刚刚制作了一个c ++程序,根据用户输入的数字来计算平均值,但它总是给出错误的答案,虽然它很简单而且很小我无法发现错误:

#include <iostream>

using namespace std;

void average(void);
char input = 0;
int number = 0;
int mode = 0;
int sum = 0;

int main()
{
    cout << "This is a program to calculate the average(mean) of numbers you write. \nPlease enter all the numbers you want to calculate. \nWrite \'q\' after entering numbers to calculate! ^_^" << endl;
    average();
    return 0;
}

void average(void){

    while(input != 'q'){
        cin >> input;
        sum += input;
        number++;

    }

    mode = (sum / number);
    cout << "\aThe mode= " << mode;
}

修改


我总是得到错误结果的一个例子:http://i.imgur.com/YxW8Zdp.png

4 个答案:

答案 0 :(得分:1)

您的代码存在许多问题:最明显的是 您在没有先检查的情况下使用输入结果 输入是否成功,以及您是否正在使用该字符 编码为整数值。

最惯用的写作方式如下:

int input;
while ( std::cin >> input ) {
    sum += input;
}

这并不完美;如果使用输入一个字母,那么对待 它作为文件的结尾。一个可能更强大的解决方案是:

std::string line;
while ( std::getline( std::cin, line ) ) {
    std::istringstream parse( line );
    int input;
    if ( parse >> input >> std::ws && parse.get() == EOF ) {
        sum += input;
    } else {
        std::cerr << "not a number: " << line << std::endl;
    }
}

这假定(要求)每行一个数字,这很简单 格式化,易于验证和重新同步 错误。如果行结尾没有意义,则重新同步 变得更加复杂。

这两个解决方案都输入到文件末尾,即 大多数自然解决方案(而不是寻找q)。如果你 坚持以特定标记结束,输入是行 面向,你可以做类似的事情:

std::string line;
while ( std::getline( std::cin, line ) && notEndToken( line) ) {
    //  ...
}

具有与上述相同的循环体。函数notEndToken 可能就像return line == "q";一样简单,但更有可能, 你想要跳过空格,允许大写和小写等等 将它放入一个单独的函数更有意义。 (的 当然,您仍然需要检查输入是否成功。 仅仅因为你期待一个特殊的令牌并不意味着这一点 你会得到一个。您必须正确处理文件结尾 每一个案例。)

答案 1 :(得分:0)

您正在将输入中的字符值添加到总和中。如果我键入&#39; 2&#39;,UTF-8或ASCII value of the char will be 50。然后你会在总和上加50。基本上你做错了。

你需要做的是获取int值(输入必须是int)或者如果你想继续检查&#34; q&#39;,你必须转换文本值(输入为char或string)在将其添加到总和之前转换为int值。有几种方法可以执行此操作,具体取决于您的C ++版本以及您是否可以访问Boost。

使用字符串作为输入可能会解决其他问题,因为您的代码(一旦使用char修复)将无法使用大于9的数字。


这是一个适合我的版本(C ++ 11和略有改进):

#include <iostream>
#include <string>

using namespace std;

void average();

int main()
{
    cout << "This is a program to calculate the average(mean) of numbers you write. \nPlease enter all the numbers you want to calculate. \nWrite \'q\' after entering numbers to calculate! ^_^" << endl;
    average();
    return 0;
}

void average() {
    std::string input;
    int number = 0;
    int sum = 0;

    cin >> input;
    while (input != "q") {
        sum += std::stoi(input);
        number++;
        cin >> input;
    }

    const int mode = (sum / number);
    cout << "\aThe mode= " << mode;
}

请注意,我稍微更改了while循环的执行顺序,因为当您输入“q”时,它在最后一个循环中运行不正确。此外,如果条目不是数字,这个版本将抛出异常(我猜这是好的)。最后请注意,如果您使用的是C ++ 03,则可以使用std::atoi(input.c_str())代替std::stoi(input)。我还改进了一些变量用法。你绝对不需要把一切都变得全球化。

但是这个示例是围绕您的原始代码设计的,这开始时会出现问题,至少是为了检查错误。所以这不是理想的,正如评论中所指出的那样,詹姆斯·坎兹的回答(+1)中也提供了更为惯用的输入检查方式。理想情况下,您应该更喜欢他的版本,这只是为了向您展示您的案例的直接示例。

答案 2 :(得分:0)

研究后:

#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>

using namespace std;

void average(void);
string input;
int number = 0;
int mode = 0;
int sum = 0;
int StringToNumber ( const string& );

int main()
{
    cout << "This is a program to calculate the average(mean) of numbers you write. \nPlease enter all the numbers you want to calculate. \nWrite \'q\' after entering numbers to calculate! ^_^" << endl;
    average();
    return 0;
}

int StringToNumber ( const string& Text )
{
    stringstream ss(Text);
    int result;
    return ss >> result ? result : 0;
}

void average(void){

    cin >> input;
    while (input != "q") {
        sum += StringToNumber(input);
        number++;
        cin >> input;
    }

    mode = (sum / number);
    cout << "\aThe mode= " << mode;
}

答案 3 :(得分:-1)

代码存在问题,您已将input作为char。 这里发生的是,cin正在读取变量中的字符值。

例如,当您输入123时,它只会读取1并将其视为字符,并将其49中的{ASCII值}存储在input中变量。这意味着,您要将49添加到总和中,而不是123。这就是你得到错误答案的原因。

您应该将char input = 0;更改为int input = 0;并将终止条件更改为其他内容。

相关问题