如何从main获取可变值并将其发送到函数

How to get variable values from main and send them to a function?

本文关键字:函数 main 获取      更新时间:2023-10-16

我在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";
    }
}

在使用getName()的代码行上,它输出了类似"您的> 空白的名称?"而不是正确的clasStype。我想知道如何将变量值从main发送到一个函数,以便在此处正确输出文本。

它不起作用的原因是因为您的getName函数不知道clasStype变量中存储的内容。阅读函数可变范围如何工作以理解整个机制可能是有益的。

如果您想保留程序的当前实施。重写您的getName函数以接受字符串类作为参数

string getName(string classType)
{
  string charName;
  cout << "What is your " << classType << "'s name?" << endl;
  cin >> charName;
  return charName;
}

,在您的主机中,您将函数称为以下内容:

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

您可能还需要添加到文件顶部的字符串库,因为没有它也可能导致您的代码根本不起作用。以及确保正确存储从您的getName()函数返回的名称如下:

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;
}