如何在函数中传递字符串?

问题描述:

我主要有一个函数(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改变了第二个。 –

您的代码的主要问题是缩进。你的代码是不可读的,而且很难理解它。美化它。写好,可读和结构化的代码。您可以在以下链接阅读更多关于缩进的信息。

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和数组的长度作为最佳选择。 –