繁体   English   中英

将变量字符串转换为char数组c ++

[英]convert variable string into char array c++

我发现了如此多的此类问题帖子-我说的是“将字符串转换为char数组”-但这些解决方案均不适合我,试图将cin >> text转换为某些char数组textArray[1024]然后,我可以转换为list因为我认为使用起来更容易。

问题是:空格。 每当那里有一个空格时,它都会跳过以下操作,并以我自己的错误信息打我。

它用于某些加密器(下面的代码)。

如果有任何更简便的方法,请告诉我。

#include <iostream>
#include <string>
#include <fstream>
#include <list>
#include "encryptor.h"

using namespace std;

void encrypt()
{
    string text;
    char textArray[1024];
    list<char> listText;
    list<char>::iterator it;
    int textSize;
    string code;
    bool fail = false;
    string segment;
    string fileName;

    cout << "Now enter your text. (max 1024 chars)" << endl;
    cin >> text;

    textSize = text.size();

    //string to char[]
    //none of these work
    strncpy(textArray, text.c_str(), sizeof(textArray));
    textArray[sizeof(text) - 1] = 0;

    strcpy_s(textArray, text.c_str());

    for (int i = 0; i < text.length(); i++)
    {
        textArray[i] = text[i];
    }

    aText[text.length()] = '\0';

    text.copy(textArray, text.length()+1);



    //char[] to list
    for(int i = 0; i < textSize; i++)
    {
        char *c = new char(textArray[i]);
        listText.push_back(*c);
    }

    //Going through list
    //for every char there's a special segment added to the string
    for(it = listText.begin(); it != listText.end(); it++)
    {
        if(fail == true) break;

        switch (*it)
        {
        case 'a':
        case 'A':
            {
                segment = "XQ7";
            } break;
        {/*---*/}               //I just let everything from b - z and 0 - 9 out for this post
        case ' ':
            {
                segment = "Z 7";
            } break;
        case '.':
            {
                segment = "Z 8";
            } break;
        case ',':
            {
                segment = "Z 4";
            } break;
        default:
            {
                cout << "There's a special char this program doesn't understand. It is "
                cout << *it << endl;
                cout << "Do it again" << endl;
                fail = true;
            } break;
        }

        code = code + segment;
    }

    do
    {
        cout << "\n\nname of the file: ";
        cin >> fileName;

        if(fileName != "")
        {
            ofstream write;
            write.open(fileName + ".txt");
            write << code;
            write.close();
        } else {
            cout << "Name shouldn't be empty!" << endl;
        }
    } while(fileName == "");
}

您的主要问题不是将字符串text转换为字符数组,而是您没有从stdin捕获整行。

cin >> text; 将从stdin读取, 直到遇到第一个空格字符为止 这就是为什么您遇到空格问题的原因。 您仅将字符读入text直到第一个空白字符。 相反,您需要使用getline() 替换cin >> text; getline(cin, text); 将从stdin读取整行包括任何空格字符

我提供了一个完整的示例,可以从stdin读取一行文本,并将其转换为以下字符列表。 在将字符串转换为列表之前,它完全不需要将字符串转换为字符数组。

#include <iostream>
#include <list>
#include <string>

using namespace std;

int main() {
    string s;
    list<char> text;

    getline(cin, s);

    for (string::iterator it = s.begin(); it != s.end(); ++it) {
        text.push_back(*it);
    }

    // Verification
    cout << "You entered " << text.size() << " characters\nThey were:\n";
    for (list<char>::iterator it = text.begin(); it != text.end(); ++it) {
        cout << *it;
    }
    cout << endl;
}

暂无
暂无

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

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