Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从main获取变量值并将其发送到函数?_C++_Function - Fatal编程技术网

C++ 如何从main获取变量值并将其发送到函数?

C++ 如何从main获取变量值并将其发送到函数?,c++,function,C++,Function,在IDE中运行此代码时遇到问题。您可以很早就看到,我尝试使用函数。这样做的原因是为了以后通过输出文本来节省内存,但是函数中的变量会出现问题。classType变量未初始化,如何防止出现这种情况?我已经在main中定义了它们,但是当我试图用main中的变量输出文本时,它就不能正常工作了 #include<iostream> using namespace std; string getName() {    string charName;    int classType;  

在IDE中运行此代码时遇到问题。您可以很早就看到,我尝试使用函数。这样做的原因是为了以后通过输出文本来节省内存,但是函数中的变量会出现问题。classType变量未初始化,如何防止出现这种情况?我已经在main中定义了它们,但是当我试图用main中的变量输出文本时,它就不能正常工作了

#include<iostream>
using namespace std;
string getName()
{
    string charName;
    int classType;
    cout << "What is your " << classType << "'s name?" << endl;
    cin >> charName;
    return charName;
}
int main()
{
    int classType; //Later we will ask the user what class they're playing.  
    string charName; 
    /*We will use a function to ask a question.  
    We use a function to save memory instead of copy-pasting the text*/
    cout <<"Welcome to \"Orcs and Ogres\"" << endl;
    cout << "What class do you want to play?  " << endl;
    cout << "\tType 1 for Warrior class]" << endl;
    cout << "\tType 2 for Archer class ]" << endl;
    cout << "\tType 3 for Mage class   ]" << endl;
    cin >> classType;
    if(classType == 1)
    {
        cout << endl << "You are a warrior" << endl;
        string classType;
        classType = "warrior";
        getName();
    }
    else if(classType == 2)
    {
        cout << endl << "You are an archer" << endl;
        string classType;
        classType = "archer";
        getName();
    }
    else if(classType == 3)
    {
        cout << endl << "You are a mage" << endl;
        string classType;
        classType = "mage";
        getName();
    }
    else
    {
        cout << endl << "UserError:  Number too high or too low";
    }
}
#包括
使用名称空间std;
字符串getName()
{
字符串字符名;
int类类型;

cout它不起作用的原因是getName函数不知道classType变量中存储了什么。阅读函数变量作用域的工作原理,了解整个机制的工作原理可能会有所帮助

如果希望保留程序的当前实现,请重新编写getName函数以接受string类作为参数

string getName(string classType)
{
  string charName;
  cout << "What is your " << classType << "'s name?" << endl;
  cin >> charName;
  return charName;
}
您可能还希望添加字符串库以将其包含在文件顶部,因为没有字符串库也可能导致代码无法正常工作。此外,请确保正确存储从getName()函数返回的名称,如下所示:

getName("Warrior"); // to ask warrior for a warriors' name
getName("Mage"); // to ask for a mage's name.
string name = getName("Warrior");

另外,正如其他人所说,也许多读一点关于函数接收和返回值如何对您有益的内容。

就这么简单。试试这段更新的代码

#include<iostream>
using namespace std;
string getName(string classType)
{
    string charName;
    cout << "What is your " << classType << "'s name?" << endl;
    cin >> charName;
    cout<<"your "<<classType<< "'s name is "<<charName<<endl;
    return charName;
}
int main()
{
    int Type; 
    string charName; 
    cout <<"Welcome to \"Orcs and Ogres\"" << endl;
    cout << "What class do you want to play?  " << endl;
    cout << "\tType 1 for Warrior class]" << endl;
    cout << "\tType 2 for Archer class ]" << endl;
    cout << "\tType 3 for Mage class   ]" << endl;
    cin >> Type;
    if(Type == 1)
    {
        cout << endl << "You are a warrior" << endl;
        string classType;
        classType = "warrior";
        getName("warrior");
    }
    else if(Type == 2)
    {
        cout << endl << "You are an archer" << endl;
        string classType;
        classType = "archer";
        getName("archer");
    }
   else if(Type == 3)
   {
        cout << endl << "You are a mage" << endl;
        string classType;
        classType = "mage";
        getName("mage");
    }
    else
    {
        cout << endl << "UserError:  Number too high or too low";
    }
    return 0;
}
#包括
使用名称空间std;
字符串getName(字符串类类型)
{
字符串字符名;

不能将链接发布到代码,而是发布代码。您当时甚至没有阅读它。Stackoverflow告诉我,我的代码再次格式不正确,因此,如果您能告诉我我的格式错误,以供将来参考,这也很有帮助。我有一个可共享的链接,您可以在此处查看代码:“
getName()::classType
main()::classType
不是同一个对象。在前者中,您正在打印一个未初始化的变量。您希望发生什么?没有什么好结果,因为它是未定义的行为,因此会导致整个程序格式错误。如果您想在函数之间传递一个变量…那么当然,只需传递它。(或者如果你想把它变成一个全局变量,a:不要,B:你做错了。)对于你的编辑:“类类型变量未初始化,我如何防止这种情况?“你可以通过了解如何在函数之间传递变量的基本原理来防止它。不知道这意味着你需要阅读一本书或教程;这不是一个好问题。