简体   繁体   English

我对 C++ 有疑问,因为它拒绝显示答案而且我不知道问题出在代码中

[英]I have a problem with C++ because it refuses to display the answer and I don't know where the problem is in code

 #include <iostream>
#include <math.h>
#include <iomanip>
#include <tchar.h> using namespace std;

int _tmain(int argc, _TCHAR* argv[]) {
    setlocale(LC_CTYPE, "Russian");
    double x;
    for (x = 2; x < 6.3; x + 0.4);
    {
        double Rez, Rez1;
        Rez = pow(x, 4) + 2 * pow(x, 2) + 3;
        Rez1 = cos(3 * x) + exp(-2 * x);
        cout << Rez << Rez1;
    }
    system("Pause");
    return 0;

it supposed to count equations with each value of X in the range from 2 to 6.3, with a step of 0.4 aka 2, 2.4, 2.8, etc.它应该计算每个 X 值在 2 到 6.3 范围内的方程,步长为 0.4,即 2、2.4、2.8 等。

The problem was with the final semicolon as someone in the comments suggested.正如评论中的某人所建议的那样,问题出在最后的分号上。 Just due to that semicolon, x's value was incremented above 6.4 in the 1st iteration and only 1 iteration was taking place.正是由于那个分号,x 的值在第一次迭代中增加到 6.4 以上,并且只发生了 1 次迭代。 Also the variable x needs to be incremented in an appropriate way: Here is the working code:变量 x 也需要以适当的方式递增:这是工作代码:

#include <iostream>
#include <math.h>
#include <iomanip>
#include <tchar.h> 
using namespace std;

int _tmain(int argc, _TCHAR* argv[]) 
{
    setlocale(LC_CTYPE, "Russian");
     double x=2;
    for ( x = 2 ; x < 6.3;  x = x + 0.4 )
    {
        double Rez, Rez1;
        Rez = pow( x, 4) + 2 * pow( x, 2) + 3;
        Rez1 = cos(3 * x) + exp(-2 * x);
        cout <<"Result 1:" <<Rez <<"  Result 2:"<< Rez1<< endl;
    }

    system("Pause");
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM