在 VC++ 中始终有 sqrt() 中的 0 值
Always have 0 value from sqrt() in VC++
我对 sqrt() 有问题!我是一个初学者,所以请放纵,答案可能非常简单。所以这是我的代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int touche;
int i;
float x;
float racx;
const int NFOIS = 5;
cout << "Bonjours!" << endl;
cout << "Je vais vous calculer " << NFOIS << " racines carrees" << endl;
for (i = 0; i < NFOIS; i++)
{
cout << "Donner un nombre: " ;
cin >> x;
if (x = 0.0)
{
cout << "Le nombre" << x << "ne possede pas de racines carrees!" << endl ;
}
else
{
racx = sqrt(x);
cout << "Le nombre " << x << " a une racine carree de : " << racx << endl;
}
}
cout << "Programme termine veuillez entrer un charactere pour fermer le programme." << endl;
cin >> touche;
return 0;
}
我的问题是当我输入一个数字时(它被存储了,我用cout检查了它,然后在这里发布它"以防万一")是sqrt(x);它只告诉程序x和racx是0,无论我键入什么。我真的不知道它会是什么。我还尝试更改"math.h"的 #include,并且仍然是相同的,所以我认为我的代码在某处是错误的。
我使用 Visual c ++ 2013 如果你想知道在 Windows 7 终极 64 位。
PS:正如你所注意到的,我的第一语言是法语,但不要担心,即使我不擅长写:P,我也非常了解英语。
无论如何,感谢您的关注和帮助!将不胜感激!
在这一行中:
if (x = 0.0)
您不是将 x 与 0 进行比较,而是将值 0 分配给 x,这在"if"语句中产生 false,我假设您想使用相等运算符 ==。
这是一个相当普遍的问题(混淆=与==,有些语言(不是c ++)甚至有===),所以不用担心,看来你努力寻找原因,问题有回答它所需的一切,干得好,学习愉快。
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- .cpp和.h文件中的模板专用化声明
- 反向给定链表中的K节点
- 正在查找文档以获得PS4平台的C++中的设备信息
- enum是C++中的宏变量还是整数变量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 将字符串存储在c++中的稳定内存中
- 文本文件中的单词链表
- 递归函数计算序列中的平方和(并输出过程)
- 如何从C++中的依赖类型中获得它所依赖的类型
- C++中的"inline"关键字
- 尽可能快地比较形式(a + sqrt(b))中的两个值
- 我们如何找到 N sqrt(m) 并检查答案是否是 C++ 中的整数
- 为什么在我的电脑上没有g++中的-O2,sqrt会变得更快
- 如何防止 sqrt 中的溢出
- sqrt() 使用 <math.h) 在二次公式中的意外结果>
- 这是 sqrt 函数中的错误吗?
- 如何使用Dev C++检查数学库函数sqrt()中的代码
- 在 VC++ 中始终有 sqrt() 中的 0 值
- 内联汇编器中的sqrtsd可以比sqrt()更快吗?