# How to make the result of x and not x1, c ++

1

I am developing some basic exercises and I get stuck in this, the program asks me to make a result of a variable x1 and that is equal to nX but if it is 1 that only shows x. Example

a = 1 * x a = x

``````#include<iostream>

using namespace std;

int main () {
int x1,a;
char X;
cout<< "ENTERO POR CHAR";
cout<< "DIGITE EL VALOR DE X1: "; cin >> x1;
a = x1 * X;
cout << a;
//Si se digita un 1 en x1 que salga en pantalla una x.
//Si se digita un 2 en x1 que salga en pantalla un 2x.

return 0;
}
``````

``````cout<< a << "x";
``````

But if a is 1 it would not print x if 1x.

asked by Diego David 06.06.2017 в 02:26
source

1

You can look at it like this: You should only print the stored value in `x1` if it is greater than 1 or, said with code:

``````if( x1 > 1 )
std::cout << x1;
std::cout << 'x';
``````

The `x` I understand that it should always be printed, then it does not make sense to include it in `if` .

The complete program:

``````int main () {
int x1;
cout<< "ENTERO POR CHAR";
cout<< "DIGITE EL VALOR DE X1: ";
cin >> x1;

if( x1 > 1 )
cout << x1;
cout << 'x';

return 0;
}
``````

• If you are asked to print a literal, which is a constant value, it is not necessary to store it in a variable.
• Try to initialize all the variables when declaring them.
• A variable of numeric type can only store numbers, the operation `a=x1*X` will not allow you to evaluate the equation but it will store in `a` the result of multiplying `x1` by `X` (in your case `X` is not initialized).
source
1

``````cout<<a<<"x";
``````

OK, just that you have to use conditions in such a way that if "a" is equal to 1 print "x" and if it is different print nx

``````cin>>a;
if(a==1)
cout<<"x";
else
cout<<a<<"x";
``````

Please note that when performing this operation

``````char X;
a=x1*X;
``````

you are multiplying the value associated with X (the variable) and not the value of the character 'x'. Since you do not initialize the value of variable X, depending on the compiler, its value may vary. In my case, X was 0, so a = x1 * 0 and that made the result 0