c++ - 如何将名称发送给不同的功能

标签 c++

大家好,我是一名 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/

相关文章:

c++ - 将 float 数组与 int 数组进行比较

c++ - 从莫顿有序点构建平行四叉树

c++ - 我是否需要在 CUDA 中跨多个 GPU 镜像输入缓冲区/纹理?

c++ - 链表删除

python - Linux下如何知道C/C++调用的程序路径?

python - PyQt5 自定义 QAction 类

c++ - 'element' 的初始化被 'case' 标签跳过

c++ - Netcdf C++|如何为单个变量写记录?

c++ - 为 freebsd 11 : error: unknown type name 'choke' 编译 gcc4.8.5 时出错

c++ - 为什么为我的对象类编译标准优先级队列失败?