C++字符串,该字符串不能包含加号
C++ strings, the string cant contain the plus sign
我正在尝试制作一个更高级的计算器,您可以在其中输入一段代数。我有这个问题,包含你输入的字符串的'+'测试结果不呈阳性,即使它呈阳性。
这是测试字符串的代码
for(int i = 0; i < line.length(); i++)
{
if(pos == 0 && line[i] >= '0' && line[i] <= '9' || line[i] == '.')
{
p1[p1p] = line[i]; // setting the number to the current character
p1p++; // position of the first number
}
if(line[i] == '+')
{
pos++;
operation = 1;
cout << "add" << endl;
}
}
除非数字的最后一个字符和+符号之间没有空格,否则它永远不会输出+
例如,"100+10"的"+"检测结果为阳性但"100+10"不会。
谢谢-Hugh
如果我的猜测是正确的,则使用std::cin
输入数据。这就是为什么它不读取第一空白之后的字符。
请改用getline()
函数。
您可以使用operator>>
,它自然读取(可选)空格分隔的值。
int val;
while(std::cin >> val)
{
// We have a value (an integer)
char c;
if (std::cin >> c)
{
// we have read the next non space character
switch(c)
{
case '+': std::cout << "Adding: " << val << "n";break;
}
}
}
相关文章:
- 编译包含字符串的代码时遇到问题
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 一个函数,用于查找字符串1包含字符串2 c++的次数
- 当字符串包含 %2C 时,在 c++ 中不完整的 std::string
- C++ 检查 UTF8 字符串包含指定的字符
- 将文件作为字符串包含在内
- 为什么string.empty()在字符串包含一个值时评估为true
- boost::p rogram_option::store 在选项字符串包含混合语言字符时引发异常
- 字符串流加一个额外的零
- 在 c++ 中将字符串(包含字符和浮点数)转换为双精度
- 当数据包含空格时,如何将制表符分隔文件的内容加载到C++字符串的 2D 向量中
- 使用ifstream将文件加载到字符串将加载另一个文件的不完整副本
- 将字符串(包含数字)转换为整数并返回该整数
- 如果输入字符串包含"%",则vsntprintf_s崩溃
- 当一个字符串包含另一个字符串的一部分时比较字符串
- 如果字符串包含多个 ,如何在每25个换行符( )上分割字符串
- 输出字符串包含奇怪的字符
- 当字符串包含多个NUL字符时,如何获取c_str(用于发送)
- 乘法的字符串[包含整数],输出也存储在字符串中,如何
- c++ #只包含加载一个命名空间