C++ Function f(x)

I have a task to write a program in C ++ with the following condition: To write a program
0 ,at x <=0
f(x) = x , at 0 < x <=1
x^4 ,at x > 1


This is my code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
system("chcp 1251");
int x;
cout << "Âúâåäè x: "; cin >> x;
if(x <= 0)
{
x=4;
cout <<"x: " << x <<endl;
}
else
{
x=x;
cout <<"x: " << x <<endl;
}
if (x > 1)
{
x=x^4;
cout <<"1x: " << x << endl;
}
}


But second if don't work. I need help
First, posting code with code tags and logical indentation makes it more readable. See https://www.cplusplus.com/articles/jEywvCM9/

Your task and code:
f(x) = 0 when x <= 0
f(x) = x  when 0 < x <=1
f(x) = x^4 when x > 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <stdlib.h> // C++ programs should include <cstdlib>
using namespace std;

int main()
{
  system("chcp 1251");
  int x;
  cout << "Âúâåäè x: "; cin >> x;

  if (x <= 0)
  {
    x = 4; // is this "0 when x <= 0"?
    cout <<"x: " << x <<endl; // we don't want x, we want f(x)
  }
  else // this does "when input > 0"
  {
    x = x; // this does nothing
    cout <<"x: " << x <<endl;
  }


  if (x > 1) // this is a separate IF
  { // this happens when user input was <= 0 or > 1
    x = x^4; // the ^ is not exponent in C++.  Write x*x*x*x or use pow()
    cout <<"1x: " << x << endl;
  }
}

You should compute value of f(x) from input. You are using x for both result and input. It is better to have separate variables for them.

You should use if .. else if .. else .., not if .. else .. and if ..

You repeat the cout <<"x: " << x <<endl; three times (although third differs).
Store result of ifs into variable and have the output once, after all ifs.
In all of 5 posts OP has barely made the effort to ask a coherent question. He’s not trying because he doesn’t care and thinks he can get people online to do his very simple homeworks for him.

He’s on my ignore list.
ok, but I will say it:
^ is not power. ^ is logical xor. It sure looks like the original question is for a polynomial, not bitwise logic. Just sayin.
Despite the title, he/she isn't writing a function. Which was part of the issue with his/her other thread.
Topic archived. No new replies allowed.