1

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 == "");
}
6
  • 2
    If your code does not work, please tell where you think the problem might be, what you expect the code to do, and what the observed behavior is. If the code works as intended but you think it can be improved, post it at codereview.stackexchange.com. Commented Jul 31, 2015 at 20:03
  • You're misusing sizeof, it evaluates to the size in memory of either a given expression or an instance of a given type. sizeof(text) is sizeof(std::string) which is completely unrelated to the actual string's size what you want is text.size() (it works for textArray though, but you're potentially accessing memory outside of text.c_str() and it isn't really elegant) Commented Jul 31, 2015 at 20:04
  • Also, this textArray[sizeof(text) - 1] = 0; is useless text.c_str() is already null terminated (and that should be '\0' rather than 0). (set aside the error pointed above concerning text's size) Commented Jul 31, 2015 at 20:07
  • Well, I'm sorry, there seem to be some mistakes in there. aText was the old name of textArray. Seems if i didn't update it yet. Also I added what's wrong. I'm sorry - this is my first post and i'm quite new concerning programming. Commented Jul 31, 2015 at 20:16
  • 1
    Contrary to apparent public opinion, you do not want text.size() where you currently have sizeof(text). The code you're using is attempting to set a hard nullchar terminator on the last char in textArray (and rightly so, as the strncpy will not terminate the string if text.c_str() brings a string larger than textArray can hold). Using textArray[text.size()] doesn't solve the problem. if the string was too long to begin with that still invokes undefined behavior. Rather, it should be textArray[sizeof textArray - 1]. Commented Jul 31, 2015 at 20:46

1 Answer 1

1

Your main issue is not in converting the string text to a character array but it is that you are not capturing the entire line from stdin.

The line cin >> text; will read from stdin until the first whitespace character has been met. That is why you are having issues with spaces. You are only reading characters into text up to the first whitespace character. Instead you need to use getline(). Replacing cin >> text; with getline(cin, text); will read in an entire line from stdin including any whitespace characters.

I've included a complete example to read in a line of text from stdin and convert it to a list of characters for you below. It completely skips the need to convert the string into a character array before converting it into a list.

#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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Well, thank you for your answer, it really helped me a lot! But I got one further problem... There is some mistake in my new code, that makes the programm take the encrypted text as the file name and let the file empty. do {
If it helped please accept it as the correct answer and ask a new question about your new problem. I can't help if I don't know what the problem is or the code you've used.
Well, I dind't mean to type the "do {"... I just ask with getline(cin, fileName) for the filename and want to write into the new file with std::ofstream write write.open("files\ \b" + fileName + ".txt") write << code write.close(). Would be very kind if you could answer this question as well. (If you need more information, e.g. the complete code or something, then just ask for it.
I don't understand what your problem is. You will need to provide the code in a new question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.