简体   繁体   English

C ++ c_str()不返回完整的字符串

[英]C++ c_str() doesn't return complete string

I'm doing a C++ assignment that requires taking user input of an expression (eg: 2 * (6-1) + 2 ) and outputting the result. 我正在执行C ++分配,需要用户输入表达式(例如:2 *(6-1)+ 2)并输出结果。 Everything works correctly unless a space is encountered in the user input. 除非在用户输入中遇到空格,否则一切都会正常工作。

It is a requirement to pass the user input to the following method; 要求将用户输入传递给以下方法;

double Calculate(char* expr);

I'm aware the issue is caused by c_str() where the space characters act as a terminating null byte, though I'm not sure how to overcome this problem. 我知道问题是由c_str()引起的,其中空格字符充当终止的空字节,尽管我不确定如何克服此问题。

Ideally I'd like to preserve the space characters but I'd settle for simply removing them, as a space serves no purpose in the expression. 理想情况下,我想保留空格字符,但我只想删除它们,因为空格在表达式中毫无用处。 I get the same result when using string::data instead of c_str. 当使用string :: data而不是c_str时,我得到相同的结果。

int main(int argc, char **argv)
{
    string inputExpr;
    Calc myCalc;

    while(true) {
        cin >> inputExpr;
        if(inputExpr == "q") break;

        cout << "You wrote:" << (char*)inputExpr.c_str() << endl; // debug
        printf("Result: %.3f \n\n", myCalc.Calculate( (char*)temp.c_str() ) );
    }
    return 0;
}

c_str works just fine. c_str工作正常。 Your problem is cin >> inputExpr . 您的问题是cin >> inputExpr The >> operator only reads until the next space, so you do not read your equation fully. >>运算符只读取直到下一个空格,因此您不会完全读取方程式。

What you want to use is std::getline : 您要使用的是std :: getline

std::getline (std::cin,inputExpression);

which will read until it reaches a newline character. 它将读取直到到达换行符。 See the function description if you need a specific delimiter. 如果需要特定的定界符,请参见功能说明。

Problem is not with inputExpr.c_str() and c_str as such, c_str() returns pointer to a character array that contains a null-terminated sequence. 问题不在于inputExpr.c_str()和c_str这样,c_str()返回指向包含以空终止的序列的字符数组的指针。 While reading through cin, you get space or tab etc separating as multiple strings. 在阅读cin时,您会得到空格或制表符等分隔为多个字符串的信息。 Check with the content of the string that way to solve the intended operation 检查与字符串内容相符的方法以解决预期的操作

First, I think your Calculate() method should take as input a const char* string, since expr should be an input (read-only) parameter: 首先,我认为您的Calculate()方法应将const char*字符串作为输入,因为expr应该是输入 (只读)参数:

double Calculate(const char* expr);

Note that if you use const char* , you can simply call std::string::c_str() without any ugly cast to remove const-ness. 请注意,如果使用const char* ,则可以简单地调用std::string::c_str()而无需进行任何难看的强制转换即可删除const-ness。

And, since this is C++ and not C, using std::string would be nice: 而且,由于这是C ++而不是C,因此使用std::string会很好:

double Calculate(const std::string& expr);

On the particular issue of reading also whitespaces, this is not a problem of terminating NUL byte: a space is not a NUL . 在还读取空白的特定问题上,这不是终止NUL字节的问题:空间不是 NUL
You should just change the way you read the string, using std::getline() instead of simple std::cin >> overload: 您应该使用std::getline()而不是简单的std::cin >>重载来更改读取字符串的方式:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string line;
    getline(cin, line);
    cout << "'" << line << "'" << endl;
}

If you compile and run this code, and enter something like Hello World , you get the whole string as output (including the space separating the two words). 如果编译并运行此代码,然后输入类似于Hello World ,则将获得整个字符串作为输出(包括将两个单词分隔开的空格)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM