c++ - 离开函数后代码跳过下一个 cin
问题描述
我正在开发一款基于文本的小型冒险游戏,这是我为自己的乐趣而开发的第一个项目,但遇到了问题。我有它要问你是否想参加比赛,你的名字是什么,然后当你尝试选择比赛时问题就开始了。当用户输入第一个字符时它工作得很好,但是当他们输入字符串时它会跳过性别和类 cin。我必须清除cin吗?还是我的代码错了??感谢您的任何帮助,您可以提供。
#include "pch.h"
#include <iostream>
#include <string>
#include <cctype>
#include <map>
using namespace std;
enum races { Human, Orc, Elf, Dwarf};
enum classes { Warrior, Mage, Archer, Assassin};
const std::map< char, string > race_map =
{ {'H', "human"}, {'O', "orc"}, {'E', "elf"}, {'D', "dwarf"} };
const std::map< char, string > class_map =
{ {'W', "warrior"}, {'M', "mage"}, {'Ar', "archer"}, {'A', "assassin"}
};
void gameIntro();
void gameStart();
void raceFunc(char race);
void playerClassFunc(char playerClass);
void gameIntro()
{
string playerName;
char race;
char sex;
char playerClass;
cout << "Enter your name: \n";
cin >> playerName;
cout << "\n";
cout << "Select a race (Human, Orc, Elf, Dwarf): \n";
cin >> race;
cout << "\n";
raceFunc(race);
cout << "Select Gender (M or F): \n";
cin >> sex;
cout << "\n";
cout << "Select a class (Warrior, Mage, Archer, Assassin): \n";
cin >> playerClass;
cout << "\n";
playerClassFunc(playerClass);
gameStart();
}
void raceFunc(char race)
{
race = toupper(race);
switch (race)
{
case 'H':
cout << "You chose Human!\n\n";
break;
case 'O':
cout << "You chose Orc!\n\n";
break;
case 'E':
cout << "You chose Elf!\n\n";
break;
case 'D':
cout << "You chose Dwarf!\n\n";
break;
default:
cout << "Please choose from the following. Program closing.\n";
system("pause");
exit(0);
}
}
void playerClassFunc(char playerClass)
{
playerClass = toupper(playerClass);
switch (playerClass)
{
case 'W':
cout << "You chose Warrior!\n";
break;
case 'M':
cout << "You chose Mage!\n";
break;
case 'Ar':
cout << "You chose Archer!\n";
break;
case 'A':
cout << "You chose Assassin!\n";
break;
default:
cout << "Please choose from the following. Program closing.\n";
system("pause");
exit(0);
}
}
void gameStart()
{
}
int main()
{
char answer;
cout << "Welcome to Dark Horse\n\n";
cout << "This is my fisrt ever actual program I made out of my own free
will lol.\n";
cout << "It is a Text-Based Adventure game. In this game you will make a
character,\n";
cout << "and explore the land of Spelet, battling enemies, leveling up,
getting loot,\n";
cout << "and learning skills! You do not need to capitalize anything but
your character\n";
cout << "name. If a question has (something like this) if you don't
enter whats inside\n";
cout << "the program will CLOSE, so please pay attention! Thank you for
trying it out!\n";
cout << "I really hope y'all enjoy it!\n\n";
do
{
cout << "Would you like to play?\n";
cin >> answer;
if (answer == 'Y')
{
gameIntro();
}
else if (answer == 'N')
{
system("pause");
return 0;
}
else if (answer != 'N' || 'Y' || 'exit')
{
cout << "Come on dog it's Y or N...yes or no...\n\n";
}
} while (answer == 'N' || 'Y');
system("pause");
return 0;
}
解决方案
“ cin, of class
istream
,是用于用户输入的标准输入通道。这个流对应于 C 的stdin
。通常,这个流由操作系统连接到键盘。” (约苏蒂斯,2012 年,第 745 页)Josuttis, N. (2016)。C++ 标准库:教程和参考第 2 版:Addison-Wesley
类型很重要。
char race;
std::cout << "Please enter your race:" << std::endl;
std::cin >> race;
如果用户输入“Human”,则标准输入流包含Human
并且race
变量现在具有H
(类型char
)的值。标准输入流现在包含uman
.
char gender;
std::cout << "Please enter your gender:" << std::endl;
std::cin >> gender;
调用>>
with从标准输入流(在这种情况下)中std::cin
获取另一个字符u
并将其存储在gender
. 标准输入流现在包含man
.
虽然似乎跳过了性别问题,但您现在可以看到情况并非如此。输入流仍然包含字符。如果您查看您的第一个屏幕截图,您会看到选择了“法师”。这是因为 的值与您输入playerClass
时m
相同。m
human
解决此问题的一种方法是使用std::string
而不是char
存储输入。这样,您在解析用户输入的内容时就有了更大的灵活性(例如,您可以允许H
or Human
)。
推荐阅读
- c# - 错误是 C# DateTime.Now 返回波斯日期
- java - volatile 对象中的非 volatile 字段是否与 volatile 原始变量具有相同的效果?
- javascript - 为什么我收到此 jquery 代码的 NaN 错误?
- bash - 如何更改 KDE 中 bash/shell 脚本中新选项卡的默认窗口?
- php - 使用变量访问路由/目录而不阻止其他路由
- javascript - 如何在 Laravel 刀片中的反应组件中获取 Laravel 验证错误
- javascript - Node.js - 定义了一个函数但引发错误
- c# - 无法在 ASP.NET Core 操作筛选器中获取正确的 DbContext 引用
- reactjs - 如何在 nextjs 的 getInitialProps stateles 组件中访问路由器参数
- node.js - 为 prometheus 检测 Google AppEngine 应用程序