Home > Net >  error: invalid conversion from 'const char*' to 'unsigned char' [-fpermissive]
error: invalid conversion from 'const char*' to 'unsigned char' [-fpermissive]

Time:11-22

I plan to use C to make a random password generator (replacing python for speed), so I typed characters. When I input all characters, an error occurs:

warning: character constant too long for its type
 const unsigned char chars[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_= [{]}\\|;:'"   '",<.>/?'};
                                                                                                       ^~~~~~~~~

main.cpp:3:123: error: invalid conversion from 'const char*' to 'unsigned char' [-fpermissive]
 const unsigned char chars[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_= [{]}\\|;:'"   '",<.>/?'};
#include <iostream>

const unsigned char chars[] = {"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_= [{]}\\|;:'"   '",<.>/?'};
//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_= [{]}\|;:'",<.>/?
int main(){
    std::cout << chars;
    return 0;
}

CodePudding user response:

Did you try using std::string they are easier to manipulate and they are more similar to python

CodePudding user response:

You are using '",<.>/?' in the second part. You can normally use ' ' with chars. Use instead following with escape char \

#include <iostream>

const std::string chars= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_= [{]}\\|;:'\",<.>/?";

//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_= [{]}\|;:'",<.>/?
int main(){
    std::cout << chars;
    return 0;
}

CodePudding user response:

this is the code (been fixed):

#include <iostream>

using namespace std;

//abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_= [{]}\|;:'",<.>/?
class generate{
    public:
        void generate_random_password(){
            const char* word = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()-_= [{]}\\|;:',<.>/?\"";
            cout << word;
        }
};
int main(){
    generate test;
    test.generate_random_password();
    int test2;
    std::cin >> test2;
    return 0;
}
  •  Tags:  
  • c
  • Related