大家好,我是一名 scrub,但我喜欢 C++,但我不知道如何将名称发送给函数。我的意思的一个例子是这个 ps 我之前曾在这里寻求帮助,但从未寻求过帮助。所以可能发送不正确
#include <iostream>
using namespace std;
//here is where i would like to send the name
int use()
{
cout<<name<<endl;
return 0;
}
int main()
{
//this is the name i want to send
string name;
cout<<"please enter name"<<endl;
cin>>name;
use();
return 0;
}
我不希望它成为一个全局字符串,我需要它来发送和接收名称,因为它将被传递以供使用。 需要您的帮助
最佳答案
使用命令 use(name)
从 main 调用 use 但您还需要使用字符串参数声明 use,因为您向它传递了一个字符串参数,因此您的 use 函数应该如下所示:
int use(string myString)
{
cout<<myString<<endl;
return 0;
}
我也不知道你从这个函数返回 0 是否出于某种原因但它不像 main 你应该在最后返回 0 所以如果你不需要你可以这样做:
void use(string myString)
{
cout<<myString<<endl;
}
关于c++ - 如何将名称发送给不同的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34247118/