c++ - Converting std::vector to char* -


i c++ student working on project receive , encode message simple cipher. so, program accepts each word string, converts string vector of characters, , modifies each character before storing in file. in line returns encoded message main function, error message: < cannot convert std::vector<char, std::allocator<char> >' tochar*' argument 1' tochar* cipher(char*, int)' . despite error, program run, however, stop after word has been entered , end program. code using:

// cipher.cpp // program accepts message , encodes  // written grindle  #include<iostream.h> #include<string.h> #include<iomanip.h> #include<math.h> #include<fstream.h> #include<vector.h>  using namespace std;  string get_message(); // function prototypes char* cipher(char broken[], int length);  int main() {    int index, length; // declare index vectors     string message; // declare string     ofstream outfile; // declare filestream     outfile.open("message.dat", ios::in); // create file , open     message = get_message(); // use function retrieve data     length = message.length(); // find length of message     // declare vector breaking strings down    vector <char> broken(message.begin(), message.end());      vector <char> encoded(50); // declare vector encoded message     encoded[index] = cipher(broken, length); // initialize encoded new         message     for(index = 0; index <= length - 1; index++)// loop displaying values    {       cout << encoded[index] << endl;    }     if(outfile) // if there no error file    {       for(index = 0; index <= length - 1; index++) // loop writing encoded       {                                            // message file          outfile << encoded[index] << endl;          cout << "data written file." << endl;       }    }    else    {       cout << "error opening file." << endl;     }              outfile.close(); // close file     system("pause");    return 0; }  string get_message() {    string entry; // declare string     cout << "please enter word entered." << endl; // request entry    cin >> entry; // user enters word     system ("pause");     return entry; }  char* cipher(char broken[], int length) {    char index; // declare index     if( broken[index] < 123 && broken[index] > 96 ) // if characters lowercase,     {                                               // make them uppercase       broken = broken - 32;    }     for(index = 0; index <= length - 1; index ++)    {       broken[index] = broken[index] * (2/3);        broken[index] = broken[index] - 12;       broken[index] = broken[index] ^ 2;    }     cout << "message encoded." << endl;     system ("pause");     return(broken); }  

i open suggestions on how make work. thank you!

in c++, vector isn't array of chars, can't pass function expecting char array. have few options here.

first, i'd recommend updating code cipher function takes in parameter either vector or std::string. these objects safer raw arrays because know sizes , can reallocations if need them grow.

second, change call cipher pass in pointer vector's underlying array, this:

cipher(broken.data(), broken.size()); 

i think less elegant , more error-prone solution, you're welcome use if you'd like.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -