0

hi I am developing this code which is used to generate a password by taking letters, caps, numbers and symbols from the chars but this error appears to me,even removing the chars as constants the problem persists.

[Error] invalid operands of types 'const char [26]' and 'const char [26]' to binary 'operator+'

Here is the code (it's not finished yet, I want to understand this error first)

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand(time(NULL));
    
    const char letter_a[]={'a','b','c','d','e','f','g','h','i','j','k','l','n','m','o','p','q','r','s','t','u','v','w','x','y','z'};
    const char caps_letter_a[]={'A' ,'B' ,'C' ,'D' ,'E' ,'F' ,'G' ,'H' ,'I','J','K','L','N','M','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    const char symbol_a[]={'?','!','#','@','*','-','_'};
    const int number_a[]={1,2,3,4,5,6,7,8,9,0};
    int selection;
    bool operation=true;
    string letter,caps,symbol,number;
    
    cout<<"Enter password length ";
    cin>>selection;
    char *password= new char[selection];
        bool letter_b=false;
        bool caps_letter_b=false;
        bool symbol_b=false;
        bool number_b=false;
        
        cout<<"do you want to insert letters? type yes o no "<<endl;
        cin>>letter;
        cout<<"do you want to insert caps letters? type yes o no "<<endl;
        cin>>caps;
        cout<<"do you want to insert number? type yes o no "<<endl;
        cin>>number;
        cout<<"do you want to insert symbol? type yes o no "<<endl;
        cin>>symbol;
        while(operation==true)
    {
        if(letter=="yes")
        {
            //cout<<"sono nell'if lettere"<<endl;
            if(caps=="yes")
            {
                //cout<<"sono nell'if caps";
                for(int x=0; x<selection ; x++)
                {
                    static char r= letter_a + caps_letter_a + rand()%52+1;
                    password[x]= r;
                }
                operation=false;
                if(number=="yes")
                {
                    if(symbol=="yes")
                    {
                        
                    }
                }
            }
        }
    }
cout<<password;
return 0;
}
Enk17_
  • 35
  • 4
  • 3
    can you explain what this line is supposed to mean `static char r= letter_a + caps_letter_a + rand()%52+1;` ? – 463035818_is_not_an_ai Feb 15 '22 at 14:04
  • 1
    Unfortunately the language does not allow you to concatenate plain arrays that way... Use a different language or a different container. – Serge Ballesta Feb 15 '22 at 14:11
  • @463035818_is_not_a_number I wish r would be the variable that generates the characters taking into account the arrays placed (I have seen some tutorials that did so) 52 is the sum total of the characters. – Enk17_ Feb 15 '22 at 14:12
  • Don't look at tutorials, get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Feb 15 '22 at 15:17

1 Answers1

2

You get the error, because you cannot add character arrays like that. You can add std::strings:

std::string letter_a={'a','b','c','d','e','f','g','h','i','j','k','l','n','m','o','p','q','r','s','t','u','v','w','x','y','z'};
std::string caps_letter_a={'A' ,'B' ,'C' ,'D' ,'E' ,'F' ,'G' ,'H' ,'I','J','K','L','N','M','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

or shorter:

std::string letter_a{"abcdefghijklnmopqrstuvwxyz"};
std::string caps_letter_a{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};

and then (no reason to make r static):

char r = (letter_a + caps_letter_a )[rand()%52+1];

letter_a + caps_letter_a will be the string resulting from concatenating the two. + rand()%52+1 would just add that to the string, which isnt right. You can use it to index into the new string, but then the modulo is wrong. Better, declare the concatenated string upfront:

std::string all_letters = letter_a + caps_letter_a;

and then

char r = all_letters[ rand()% all_letters.size() ];
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185