簡體   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