用于检测负数;
#include <string>
#include <iostream>
#include <cmath>
using namespace std;
string command;
getline(cin,command);
if (command[0] == '-')
encoder = -1;
用于检测点;
else if (command[x] == '.')
{
if (detector = 0)
{
floater = pow(10, -1 * x);
detector = 1;
}
else
{
cout << "You have typed the wrong word or format.\n";
return 0;
}
}
我在Windows 8.1 K(韩国)环境中使用ENU visual studio 2013。 当我输入&#39; - &#39;在第一个空间或&#39;。&#39;在任何空间进入&#34;命令&#34;(字符串变量),
它无法检测到 - 和。
并且总是忽略并传递两个句子。
我忘记了什么?请帮帮我。
答案 0 :(得分:1)
您发布的代码不是真正的代码,但遗漏setlocale
来电可能反映了实际代码中的类似遗漏。
要完成一个操作,请在setlocale( LC_ALL, "" )
开头添加main
来电。
您还应该注意 trim (从中删除不需要的空格)输入字符串,并检查之后是否还有任何字符。
另外,
中的作业if (detector = 0)
很可能是一个错字。
自由使用const
可以帮助避免这种情况,以及(在我看来误导,因为它会降低可读性)在比较中首先编写常量的做法。
此外,使用整数作为布尔值,而不是bool
类型,正如您显然所做的那样,是不行的做法。