简体   繁体   中英

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.

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. Also the variable x needs to be incremented in an appropriate way: Here is the working 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=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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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