将std :: vector转换为char *

时间:2016-01-07 18:30:14

标签: c++ arrays string vector

我是一名c ++学生,致力于一个项目,用简单的密码接收和编码消息。为此,程序将每个单词作为字符串接受,将字符串转换为字符向量,然后在将每个字符存储到文件中之前修改它们。在将编码消息返回到main函数的行中,我收到以下错误消息:&lt;无法转换std::vector<char, std::allocator<char> >' to char *'作为参数1' to char * cipher(char *,int)'。尽管出现此错误,程序仍会运行,但是,只要输入一个单词并结束程序,它就会停止。这是我正在使用的代码:

// 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);
} 

我愿意接受有关如何使这项工作的任何建议。谢谢!

2 个答案:

答案 0 :(得分:4)

在C ++中,向量实际上并不是一个字符数组,因此您无法将其传递给期望char数组的函数。你有几个选择。

首先,我建议更新您的代码,以便密码函数作为vector或std :: string的参数。这些对象比原始数组更安全,因为它们知道它们的大小,并且如果需要它们可以进行重新分配。

其次,您可以将对cipher的调用更改为传递指向vector的基础数组的指针,如下所示:

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

我认为这是一个不太优雅且更容易出错的解决方案,但如果您愿意,欢迎您使用它。

答案 1 :(得分:0)

您的代码看起来更像C ...

Maybe start learning the basic data types and data structures first.

Then make sure you understand the usage of the vector template

这似乎也很好:Differences between C and C++ 它还指出了一些关于风格和你可以做什么的事情,但不应该这样做。

当然这个Programmers.StackExChange Post:What are the fundamental differences between C and C++?

尝试使用现代C ++ IDE,如CodeBlocks,Eclipse,Netbeans,......; 这有助于您在开始时防止出现一些故障。