使用CIN时,字符串变量不会被覆盖
String variable not being overwritten when using cin
enum state{
state_Mmenu, // State 0
state_settingmenu, // State 1
state_gamemenu, // State 2
state_savegamemenu, // State 3
state_loadgamemenu, // State 4;
state_Exit, // State 5;
state_other
};
state stateMmenu();
state statesettingmenu(string, string);
state stategamemenu();
state statesavegamemenu(string, string, string, ofstream&);
state stateloadgamemenu(string, string, string, ifstream&);
state stateExit();
int main()
{
state curr=state_other;
string filename= "PlayerOne.jak";
string fname="Player";
string lname="One";
ifstream inFS;
ofstream outFS;
while(curr != state_Exit)
{
switch(curr)
{
case state_Mmenu:
curr = stateMmenu();
break;
case state_settingmenu:
curr = statesettingmenu(fname, lname);
break;
case state_gamemenu:
curr = stategamemenu();
break;
case state_savegamemenu:
curr = statesavegamemenu(fname, lname, filename, outFS);
break;
case state_loadgamemenu:
curr = stateloadgamemenu(fname, lname, filename, inFS);
break;
default:
cout << "Don't Tip the Vending Machine"<<endl;
curr = state_Mmenu;
break;
}
}
return 0;
}
state statesavegamemenu(string fname, string lname, string filename,
ofstream outFS)
{
state next=state_savegamemenu;
int choice4=0;
cout<<" -Save Game Menu-----------------"<<endl;
cout<<"1. File Name - "<<filename<<endl;
cout<<"2. Save" <<endl;
cout<<"3. Game Menu" <<endl;
cout<<"----------------------------"<<endl;
do
{
cout << "Choice? "<< endl;
cin >> choice4;
cin.clear();
cin.ignore(10000,'n');
} while(!cin.good());
switch(choice4)
{
case 1:
{
cout<<"What would you like to save this file as? ";
cin>>filename;
cout<<endl;
next = state_savegamemenu;
}
break;
case 2:
{
outFS.open(filename.c_str());
if(!outFS.is_open())
{
next = state_savegamemenu;
break;
}
else
{
outFS<<fname<<endl;
outFS<<lname<<endl;
}
outFS.close();
next = state_savegamemenu;
}
break;
case 3:
next = state_gamemenu;
break;
default:
cout << "Bad Input!"<<endl;
next = state_savegamemenu;
break;
}
cout <<endl;
return next;
}
state stateloadgamemenu(string fname, string lname, string filename,
ifstream inFS)
{
state next=state_loadgamemenu;
int choice5=0;
cout<<" -Save Game Menu-----------------"<<endl;
cout<<"1. File Name - "<<filename<<endl;
cout<<"2. Load" <<endl;
cout<<"3. Main Menu" <<endl;
cout<<"----------------------------"<<endl;
string filecontent;
do
{
cout << "Choice? "<< endl;
cin >> choice5;
cin.clear();
cin.ignore(10000,'n');
} while(!cin.good());
switch(choice5)
{
case 1:
{
cout<<"What file would you like to load? ";
cin>>filename;
cout<<endl;
next = state_loadgamemenu;
}
break;
case 2:
{
inFS.open(filename.c_str());
if(!inFS.is_open())
{
next = state_savegamemenu;
break;
}
else
{
while(!inFS.eof())
{
inFS >> filecontent;
if(inFS.good())
{
cout << filecontent << endl;
}
}
}
inFS.close();
next = state_loadgamemenu;
}
break;
case 3:
next = state_Mmenu;
break;
default:
cout << "Bad Input!"<<endl;
next = state_loadgamemenu;
break;
}
cout <<endl;
return next;
}
所以我的问题是,在代码中,我试图用用户的cin
覆盖文件名。问题在于,当我刷新状态机时,文件名并未覆盖,并且仍然是" playerone.jak",我将其命名为int main((。如果有人知道为什么这将不胜感激。
此功能
state stateloadgamemenu(string fname, string lname, string filename,
ifstream inFS)
应该是
state stateloadgamemenu(string fname, string lname, string & filename,
ifstream inFS)
原因是,当您传递字符串或C 中的任何对象时,它将按值传递,这意味着该函数内部的任何更改都不会保存到原始变量。
通过引用传递它意味着相同的对象将将更改从调用函数回到呼叫者。
您可以裁判以获取更多详细信息通过C
相关文章:
- C++ 成员变量被非成员函数覆盖
- 使用CIN时,字符串变量不会被覆盖
- 通过使用全局变量的函数访问在给定范围内被覆盖的变量
- 每个方法调用上都覆盖了本地变量指针
- for 循环,允许变量相互覆盖
- C 的变量覆盖
- C 覆盖成员变量
- 覆盖运算符<< 对于控制台输出不适用于成员变量
- C 多重继承,虚拟方法覆盖问题和协变量返回类型
- 提取运算符 (>>) 可以覆盖变量吗?
- C++ 在超类构造函数中运行依赖于子类覆盖的大量变量的代码的正确方法是什么?
- 如何强制 gcc 查找自己的 c++ 头文件,而不覆盖 CXX 变量
- C++ fscanf() 返回 -1 并且不覆盖变量
- 为什么引用成员变量会相互覆盖
- 变量赋值覆盖其他变量的值
- c++变量在每次循环迭代期间被覆盖
- 使用new时将覆盖变量
- 为什么作用域解析对覆盖的变量不起作用?
- 模板类函数中的静态变量被全局静态变量覆盖
- 代码覆盖变量,尽管它不应该