Ledorub - 1 year ago 93
C++ Question

Why var x display incorrectly?

``````    #include <iostream> // подключаем библиотеку ввода-вывода
#include <cmath> // подключаем библиотеку математических функций

using namespace std;

int main()
{
float a, x, y = 0; // объявление переменных
cout << "Enter a variable: ";
cin >> a; // запрос на ввод a
cout << "Enter x variable: ";
cin >> x; // запрос на ввод x
cout << "a = " << a << ", x = " << x; // вывод a и x
y = (pow(x, 3) + sqrt(1 + pow(x, 3))) / (a + exp(x)); // формула расчета
cout << y;
}

Enter a variable: 6
Enter x variable: 7
a = 6, x = 70.327894
``````

I can't understand why x == 70, when it must be 7. Also I don't know why
`cout << y;`
doesn't work. If I delete
`y = ...`
and
`cout << y`
, x display correctly.
I think, that it's my fault, because I'm new in C++ and don't know syntax well.

You're simply printing everything on the same line, you can either:

Print it after your two first result:

``````cout << "a = " << a << ", x = " << x; // вывод a и x
y = (pow(x, 3) + sqrt(1 + pow(x, 3))) / (a + exp(x)); // формула расчета
cout << ", y = " << y;
``````

Output:

``````Enter a variable: 6
Enter x variable: 7
a = 6, x = 7, y = 0.327894
``````

Or print `y` on another line

``````cout << "a = " << a << ", x = " << x << "\n"; // returns to another line
y = (pow(x, 3) + sqrt(1 + pow(x, 3))) / (a + exp(x)); // формула расчета
cout << "y = " << y;
``````

Output:

``````Enter a variable: 6
Enter x variable: 7
a = 6, x = 7
y = 0.327894
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download