Reputation: 139
I am a c++ student working on a project to receive and encode a message with a simple cipher. To do so, the program accepts each word as a string, converts the string into a vector of characters, and then modifies each character before storing it in a file. In the line that returns the encoded message to the main function, I get this error message: < cannot convert std::vector<char, std::allocator<char> >' to
char*' for argument 1' to
char* cipher(char*, int)' . Despite this error, the program will run, however, it will simply stop after a word has been entered and end the program. This is the code I am using:
// Cipher.cpp
// This program accepts a message and encodes it
// Written by Will 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 for vectors
string message; // declare string
ofstream outfile; // declare filestream
outfile.open("MESSAGE.DAT", ios::in); // create file and open it
message = get_message(); // use function to retrieve data
length = message.length(); // find length of message
// declare vector for breaking the strings down
vector <char> broken(message.begin(), message.end());
vector <char> encoded(50); // declare vector for encoded message
encoded[index] = cipher(broken, length); // initialize encoded to new message
for(index = 0; index <= length - 1; index++)// loop for displaying values
{
cout << encoded[index] << endl;
}
if(outfile) // if there is no error with the file
{
for(index = 0; index <= length - 1; index++) // loop for writing encoded
{ // message to file
outfile << encoded[index] << endl;
cout << "Data written to 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 the word to be 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 are 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 am open to any suggestions on how to make this work. Thank you!
Upvotes: 0
Views: 2905
Reputation: 742
Your code looks more like C...
Maybe start learning the basic data types and data structures first.
Then make sure you understand the usage of the vector template
This seems good as well: Differences between C and C++ It also points out some things about style and what you could do, but shouldn't do.
And of course this Programmers.StackExChange Post: What are the fundamental differences between C and C++?
Try to use a modern C++ IDE such as CodeBlocks, Eclipse, Netbeans, ...; This will help you to prevent some faults at the beginning.
Upvotes: 0
Reputation: 373112
In C++, a vector isn't actually an array of chars, so you can't pass it into a function expecting a char array. You have a few options here.
First, I'd recommend updating your code so that the cipher function takes in as a parameter either a vector or a std::string. These objects are safer than raw arrays because they know their sizes and can do reallocations if you need them to grow.
Second, you could change the call to cipher to pass in a pointer to the vector's underlying array, like this:
cipher(broken.data(), broken.size());
I think this is a less elegant and more error-prone solution, but you're welcome to use it if you'd like.
Upvotes: 4