2016-01-07 130 views
-1

我是一名C++學生,致力於使用簡單密碼接收和編碼消息的項目。爲此,程序將每個單詞接受爲一個字符串,將該字符串轉換爲一個字符向量,然後在將每個字符存儲到一個文件中之前對其進行修改。在將編碼消息返回給主函數的行中,我收到此錯誤消息:<無法轉換std::vector<char, std::allocator<char> >' to char *'爲參數1' to char * cipher(char *,int)'。儘管有這個錯誤,程序仍然會運行,但是,只要輸入一個單詞並結束程序,程序就會停止。這是我正在使用的代碼:將std :: vector轉換爲char *

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

我很樂意提供關於如何使這項工作的任何建議。謝謝!

+1

嘗試使用char數組,動態分配,因爲您不知道它在編譯時的大小,而不是'vector '您傳遞給'cypher'(稱爲破壞)的第一個參數。這讓我覺得它看起來真的很像作業 – arainone

+0

'#include '???你使用什麼編譯器? –

+0

在'cypher'中你可以使用'char index'而不需要設置任何值。這將導致未定義的行爲。 – NathanOliver

回答

4

在C++中,矢量實際上並不是一個字符數組,所以你不能將它傳遞給期望char數組的函數。你在這裏有幾個選擇。

首先,我建議更新您的代碼,以便密碼函數接受作爲參數的矢量或std :: string。這些對象比原始數組更安全,因爲他們知道它們的大小,並且如果需要它們的增長可以重新分配。

其次,你可以改變呼叫到加密的指針傳遞到向量的基本數組,像這樣:

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

我認爲這是一個不太雅緻,更容易出錯的解決方案,但你歡迎您使用它,如果你願意。

+0

您能否澄清如何使陣列成爲實際的矢量?對不起,我對C++幾乎是全新的,並且從2002年開始編寫的教科書幾乎沒有任何解釋。你可以給我的任何幫助都會很棒。謝謝! – Timpanus

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帖子:What are the fundamental differences between C and C++?

嘗試用現代C++ IDE,如代碼塊時,Eclipse,Netbeans的,...; 這將幫助您在開始時防止出現一些故障。

相關問題