如何从main获取可变值并将其发送到函数
How to get variable values from main and send them to a function?
我在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;
}
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 代码在main()中运行,但在函数中出现错误
- 函数是否可以访问传递给main()的参数
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- 为什么类和 main() 函数中也有动态内存分配
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 如何在 C 中使用空的 main() 方法运行函数?
- 我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 未解析的外部符号__imp_glewInit函数 main 中引用
- 函数main中的参数argv(int argc,char *argv [])
- 可以(通过编译器)使用多少个线程来初始化全局对象(函数main之前)
- 为什么我的编码出现错误无法在函数 main 中将 int 转换为 int *
- C/C++中函数main()的地址
- 无法在函数 main() 中创建抽象类的指针
- 无法在函数main中编译,需要Lvalue
- 如何在 c++ 中获取主函数"main()"的返回值?