2017-10-18 20 views
-2

我主要有一個函數(INT,字符串):如何在函數中傳遞字符串?

string word("HELLO"); 
int x = 0; 
char choice; 
swap(x, word); 

我想,沒有成功,進入下面的函數:

void swap(int, string) { 

int x = 0; 
string word = "HELLO"; 

cout << "Would you like to change a letter? Please enter the letter 
position. " << endl; 
cin >> x; 

if (x == 1) { 
    cout << "What do you want to change it to?" << endl; 
    cin >> word[0]; 

我不斷收到此錯誤:

錯誤C2664 '無效化std :: swap(STD :: exception_ptr &,性病:: exception_ptr &)擲()':無法從 '詮釋' 轉換參數1 '的std :: exception_ptr &'

什麼給了?

+3

從'swap'重命名功能或使用命名空間std'你有 – Tas

+1

你的代碼看起來醜陋和太亂除去明顯的'。你的函數'swap'並不真正交換任何東西只會替換某個字符。重命名它。你在'std :: cin >> word [0];'後面做了什麼? –

+0

這是另一個if/else語句。所以如果有人按1,它會改變第一個字母。 2改變了第二個。 –

回答

0

您的代碼的主要問題是縮進。你的代碼是不可讀的,而且很難理解它。美化它。寫好,可讀和結構化的代碼。您可以在以下鏈接閱讀更多關於縮進的信息。

https://en.wikipedia.org/wiki/Indentation_style

接下來是函數聲明。在定義它之前,你不要聲明你的函數。功能聲明應位於功能的頂部,並且功能的定義應低於main功能。 您可以找到有關函數聲明在以下鏈接的詳細信息:

http://en.cppreference.com/w/cpp/language/function

既然你不使用char array打印出string,是沒用的,要經過string一個循環。包括<string>庫,並開始朝着string類型工作。通過傳遞std::cout內部的string變量就足以打印出string

最後,由於您試圖操作main函數之外的string變量,因此需要您傳遞參考參數。 (void myFunction(std::string& parameter);。這樣,存在於主內部或其他函數內部的原始變量將被改變。沒有參考,&,您嘗試修改的值將不會更改。

以下鏈接演示了使用引用。

http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

請閱讀我的評論以下爲什麼應用一些更改。我對change函數作了狡猾的修改。您現在有資格使用任何尺寸的 string類型。

#include <iostream> 
#include <string> //When you are working on strings, use the string library. 

using namespace std; 

//Function declaration is very important. Have the declarations above main. 
void change(string&); 

int main() { 
    string word("HELLO"); 
    char choice; 

    cout << "The word is : " << endl; 

    cout << word << endl; 

    //No need for the for loop to print out the string as 
    // we are working on a string and not a char array. 
    // for (int i = 0; i < word.length(); i++) { 
    //  cout << word[i]; 
    // } 


    change(word); 

    cout << "The new word is" << endl << word << endl; 

    cout << "Would you like to enter another change ? Enter Y or N ? " << endl; 

    cin >> choice; 

    if (choice == 'y' || choice == 'Y') { 
     change(word); 
     cout << word << endl; 
    } 
    else { 
     cout << "Good Bye" << endl; 
    } 

    system("pause"); 

    return 0; 

} 


//When your datatype is to be modified outside the function, use the reference 
//parameter type '&'. 
//Without the reference type, your modified version of the type will only be modified 
//inside that function. 
//The original one will not be altered. 

void change(string& word) { 
    /* 
    * size_t is simply unsigned int, to work towards manipulation and accessing 
    * of string types, use unsigned int or std::size_t 
    */ 
    size_t x = 0; 

    cout << "Would you like to change a letter? Please enter the letter position. " << endl; 
    cin >> x; 

    //Check to see if the inputted value is within the string length range. 
    if(x > 0 && x <= word.length()) 
     cout << "What do you want to change it to?" << endl; 
    else{ 
     cout << "The entered position is outside the string size range\n"; 
     return; //Quit from the function if the condition is not met. 
    } 

    /* 
    * Instead of using if/else if statements, 
    * Just make a normal loop. Much simpler. 
    */ 

    for(size_t i = 0; i < word.length(); i++){ 
     if((x-1) == i) 
      cin >> word[i]; 
    } 
} 
+0

答案應該是(主要)自包含的,指的是評論不符合條件,他們可能會被刪除,恕不另行通知。 –

+0

@PserserBy感謝您的警告! –

+0

這很好,我的下一個問題是優化。我原本有word.length()而不是硬計數器,但它拋出了一個錯誤。不知道我做錯了什麼,但我理解利用size_t和數組的長度作爲最佳選擇。 –

相關問題