给出错误答案的C++平均程序 我刚刚做了一个C++程序,根据用户输入的数字计算平均值,但它总是给出错误的答案,虽然它简单又小,但我不能发现错误:< /P> #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; }

给出错误答案的C++平均程序 我刚刚做了一个C++程序,根据用户输入的数字计算平均值,但它总是给出错误的答案,虽然它简单又小,但我不能发现错误:< /P> #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; },c++,average,calculator,C++,Average,Calculator,编辑 我经常得到错误结果的一个例子:您正在将输入中的字符值添加到总和中。如果我输入'2',UTF-8或。然后在总数上加50。基本上你做错了 您需要做的是获取int值输入必须是int,或者如果您想继续检查q',您必须在将其添加到总和之前将作为char或string输入的文本值转换为int值。根据C++版本,如果您访问Boosi.,有几种方法可以做到这一点。 也许使用字符串作为输入将解决其他问题,因为一旦使用char修复代码,它将无法处理大于9的数字 以下是一个适用于me C++11的版本和一些细微

编辑


我经常得到错误结果的一个例子:

您正在将输入中的字符值添加到总和中。如果我输入'2',UTF-8或。然后在总数上加50。基本上你做错了

您需要做的是获取int值输入必须是int,或者如果您想继续检查q',您必须在将其添加到总和之前将作为char或string输入的文本值转换为int值。根据C++版本,如果您访问Boosi.

,有几种方法可以做到这一点。 也许使用字符串作为输入将解决其他问题,因为一旦使用char修复代码,它将无法处理大于9的数字

以下是一个适用于me 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::atoiinput.C_str而不是std::stoiinput。我还改进了变量的使用。你绝对不需要把一切都放在全球


但是这个例子是围绕您的原始代码设计的,这是有问题的,至少是为了检查错误。因此,正如评论中指出的那样,这并不理想,James Kanz的答案+1中还提供了一种更为惯用的输入检查方法。理想情况下,您应该更喜欢他的版本,这里只是向您展示案例中的一个直接示例。

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

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


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

您的代码存在许多问题:最明显的是 您正在使用输入的结果,而无需先进行检查 输入是否成功,以及您是否正在使用该字符 编码为整数值

最惯用的写作方式是:

int input;
while ( std::cin >> input ) {
    sum += input;
}
std::string line;
while ( std::getline( std::cin, line ) && notEndToken( line) ) {
    //  ...
}
这并不完美;如果用户输入一个字母,那么这将处理 它作为文件的结尾。一个可能更稳健的解决方案是:

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;
    }
}
使用与上述相同的循环体。函数notEndToken 可以像返回行==q;,一样简单;,但更有可能的是, 您需要跳过空格,允许大小写,等等,所以 将其放在单独的函数中更有意义。属于 当然,您仍然需要检查输入是否成功。 仅仅因为你期待一个特殊的代币并不意味着 你会得到一个。您必须在中正确处理文件结尾 每个案例。

研究后:

#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;
}

因为您使用的是char变量,所以您要平均每个输入字符的ASCII码,不平均他们输入的数字。你必须将输入从字符转换为整数:1秒钟我将发布我得到的edit@vincentp我需要它来查看用户输入的字母q int在这种情况下是否不起作用,sum必须是int,因为sum可能超过255,你认为我应该做什么?你需要sum+=chartPointInput@我知道,别忘了投票,选择我的答案;我不知道为什么有人投了反对票。我能用sum+=intinput@ilou你不能只是施放值,它只会保持相同的结果。最好将输入设置为std::string include,然后使用std::atoi函数。这样,您的程序也可以处理大于9的数字。@ilou我使用字符串添加了一个完整的示例。另一个注意事项:如果你把你需要的变量放在使用它们的地方,你的代码可以被快速简化。这个代码完全是错误的。它使用输入结果,而不检查输入是否成功。如果要使用std::stoi,则需要捕获异常。如果用户输入a,你真的不希望程序崩溃。不是我,我还不能投票,但很明显,所有答案都投了反对票