首页 > 解决方案 > 离开函数后代码跳过下一个 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;
}

输入字符串时出现问题

输入字符时工作

标签: c++visual-studiovisual-c++

解决方案


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.

虽然似乎跳过了性别问题,但您现在可以看到情况并非如此。输入流仍然包含字符。如果您查看您的第一个屏幕截图,您会看到选择了“法师”。这是因为 的值与您输入playerClassm相同。mhuman

解决此问题的一种方法是使用std::string而不是char存储输入。这样,您在解析用户输入的内容时就有了更大的灵活性(例如,您可以允许Hor Human)。


推荐阅读