convert variable string into char array c++

时间:2015-07-31 19:54:42

标签: c++ arrays string char

I found so incredibly many question posts of this sort - i'm speaking of "convert string to char array" - but none of those solutions actually work for me, trying to convert cin >> text into some char array textArray[1024] which I could then convert into a list cause I think it's easier to work with.

The Problem is: Spaces. Every time when there's a space in there, it just skips the following actions and punches me with my own error messeges.

It's for some encryptor (code down below).

If there's any easier way of doing this then let me know.

#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 == "");
}

1 个答案:

答案 0 :(得分:1)

您的主要问题不在于将字符串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;
}